Testing for Windows in Emacs

I basically use the same Emacs setup on my work (Windows) and personal computers (Linux). I always wondered if there was a way to have one init.el file to rule them all. But the problem is that while most of the settings in the init.el file are platform independent, there are a few that are not:

  • file locations of certain programs (like aspell)
  • programs that are too much of a pain to install in Windows (like w3m)
  • Windows-specific hacks (like the location of find)

You could have a different file for each computer or you could just test to see if you are running Emacs. Here’s how to do that:

(defvar mswindows-p (string-match "windows" (symbol-name system-type)))

Then when you need to execute Windows-specific code, use an if conditional:

(if mswindows-p 
    ; if in Windows
    (setq-default ispell-program-name "c:\\Program Files\\Aspell\\bin\\aspell.exe")
  ; otherwise
  (setq-default ispell-program-name "/usr/bin/aspell")

What if you need to execute a block of code? Use progn:

(if mswindows-p
      ;;if in Windows run this block
    ;;otherwise run this block

Thanks to


4 thoughts on “Testing for Windows in Emacs

  1. w3m is too much of a pain on windows, as is latex support and a few other packages who linger like the dull memory of a long past pain, and while the work done to get emacs to work on windows is admirable, and the package manager gives me some hope, it is at best a gateway drug for a full blown life destroying Gnu/Linux addiction.

    1. I agree with w3m but I disagree with LaTeX. The MikTeX version pretty much allows me to do everything I do in Linux—create and edit LaTex files in emacs and preview math snippets in Emacs. Maxima mode even works with MikTeX! I even dabbled with ConTeXt for a while. *However*, beyond the snippet previews, I don’t really use LaTex.

    1. Thanks for the link. It may be worth documenting all those OS environment variables. Some of us are unfortunately forced to work with Windows for whatever reason. I also noticed variables for consoles so it may be useful beyond us Windows users.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s