Sams Teach Yourself Emacs in 24 Hours


Hour 2: Using Emacs in Microsoft Windows

Previous HourNext Hour

Sections in this Hour:


Needed Directories

The first things to do are to make a few adjustments in your system. You need home, bin, and gnu directories.

Windows Notes - If you work with both Windows NT and a version of UNIX (or Linux), you might think about partially duplicating the UNIX file structure on your NT machine. Then you know where everything is, and you can move more easily from one to the other. For example, on Red Hat Linux, the Emacs executable is typically found at /usr/bin/emacs. The rest of the distribution is found under /usr/share/emacs. Home directories are found under /home. Each user has a private directory for executables, called bin, under his or her home directory. If you duplicate that structure on your Win32 computer, you have one fewer thing to remember when you shift from one computer to another.

If you plan to install the Cygwin utilities (also on the CD-ROM) you should duplicate the UNIX file structure because many UNIX utilities assume that file structure.


You should have a personal, or home, directory somewhere. Emacs does not require a separate directory, but it is a good idea. Emacs looks for your personal startup information in the home directory. It lets you keep personal files (such as your .emacs configuration file, your spelling dictionary file, and so on) all in one place, separate from system and application software such as Emacs itself. This is a convenience on a single-user system, but it is essential when two or more people share a machine.

Windows Notes - Use your .emacs file to customize Emacs as covered in Hour 1, "Introduction to Emacs." I will mention it again later in this hour. UNIX considers a filename with a leading period to be a hidden file, so it is .emacs on UNIX. Early versions of NT Emacs used a filename of _Emacs, so you will see that filename in email and documentation occasionally. Either one works, but .emacs ports handily to and from UNIX, so it is preferable.

In fact, all your Emacs data files can be used on UNIX as well as on NT, so if you use both NT and UNIX, you can port back and forth.

On NT 4.0 and Windows 2000, each user has a profile directory, which might be important if you share your computer with another person. On NT 4.0, it is under $WINDIR$\Profiles\<your login>, where <your login> is your NT login name. $WINDIR$ is typically c:\winnt40, but that can vary. Under the login, there is a directory, PERSONAL, which is a good place to make your home directory. For Windows 2000, $WINDIR might be C:\WINNT50. On Windows 95/98, multiuser login profiles are usually found under C:\WINDOWS\Profiles.

Your home directory is a convenient personal spot to put files you edit often because it is easy to get to. You can specify the home directory in Emacs by typing ~/ (or ~\, Emacs doesn't care). So you can specify your .emacs file with ~/.emacs. If you share your computer with other people, placing your home directory in your profile directory lets each person have his or her own .emacs, diary, and other files.

Windows Notes - While I'm at it, let me offer a short digression on command-line syntax. UNIX uses the forward slash (/) to separate directory names in paths. Win32 uses backward slashes (\). UNIX specifies command-line options with a dash or hyphen (-). Windows and its predecessor MS-DOS use both forward slashes (/) and hyphens (-) to indicate command-line options. Because the hyphen command-line option is common to both environments, I use the hyphen for command-line options. So grep -i foo *.runs identically on both Windows NT and Linux. Because Emacs accepts either slash in a path, I find myself using either, but mostly I use the UNIX forward slash. This is because I use Linux a lot, and on Windows NT I often use the Cygnus port of Bash.

Also, in elisp source code, as in C source, forward slashes are escape characters. So in an elisp string such as //server/deskjet, back slashes must be doubled. Forward slashes are more readable.

If you aren't on a multiuser system, you can put your home directory anywhere you want. You can use your own initials or anything else you want. If you are following UNIX custom, put it at c:\home\<login-name>. If you want to stick to the Windows NT custom, put it at $WINDIR\Profiles\<your login>. I recommend you put in on the C:\ drive. If you add a new hard drive or CD-ROM drive to Windows 95/98, your drive letters are at the mercy of the system BIOS, and C: is the least likely to be moved, so it is the least likely to be disrupted by adding a new hard drive. (Windows NT users can use the Disk Administrator to reassign drive letters.)

If you are on a multiuser system, make the home directory private to the individual user.


bin is short for binary, and is UNIX's traditional name for directories that contain executables. Use bin for your own executables, such as programs you have on your computer for your own use. Put that under your home directory, wherever it is. If you follow UNIX custom, it will be at c:\home\<login-name>\bin. You'll put it into your path in a minute.

Emacs also has a bin directory, for its executable files. But you'll put that where everyone who uses your computer can get at it.


I use the GNU directory as a handy place for Free Software Foundation software (of which I use a lot). So I put all my FSF software under C:\GNU. You can call it anything you want, of course. Some programs have problems dealing with spaces in filenames, so you should not bow to Redmond and use c:/program files/gnu.

Tip - Normally, Windows (and UNIX) treats spaces as delimiters, so spaces indicate to Windows the end of one command-line argument and the beginning of the next. To help avoid problems with spaces in filenames, always specify arguments to command lines (such as in short cuts, associations, and batch files) by quoting them. For example, later I will show you how to install gnuclientw. When you create associations to gnuclientw, put the command-line argument in quotes:

gnuclientw "%1"

This encloses in quotes the filename that the association passes to gnuclientw, and ensures that the association properly handles paths with spaces in them.

To see how this works, try the following on the command line:

dir c:\program files

dir "c:\program files"

Installing to Hard Disk from the CD-ROM

Now is the time to insert the CD-ROM into the CD-ROM drive and install Windows NT Emacs. It runs its own setup program on Windows, so all you have to do is follow the installation program. If you are running Windows 95 or Windows 98, you will have to reboot to complete the installation.

Congratulations! you should now have a working NT Emacs! Go to the GNU Emacs entry in the Start menu, and click the Emacs entry in it. In a few seconds (depending on how fast your computer is) you should have Emacs running on your desktop.

You now have a minimal installation of NT Emacs (if "minimal installation of Emacs" is not an oxymoron). There are a number of things you can do to improve your installation, and I'll cover those next.

Postinstallation Bug Fixes

NT Emacs has a problem with font locking. Font locking is the Emacs mode that displays programs in color, making them easier to read. Put the following into your .emacs file to work around the font locking bug:

;; Begin work-around for Emacs 20.3 bug in lazy-lock. See the FAQ under 
;; upgrading from 19.34.6 to 20.3.1. When doesn't work on 19.34.6 
(if (>= emacs-major-version 20) 
(when (and (= emacs-major-version 20) (= emacs-minor-version 3)) 
       (let ((emacs-minor-version 2)) 
  (require 'lazy-lock)))) 
;; End work-around for Emacs 20.3 bug in lazy-lock 

Postinstallation Add-ons

Emacs on UNIX and other operating systems depends on a number of external programs to do some of its work. For example, you can compile programs from within Emacs, but that requires a shell program such as cmd.exe (Windows NT's command-line shell). Win32 operating systems don't come with many of these programs, so you have to get them elsewhere and install them yourself. I'll look at ispell for spelling, gnuclient for filename associations, and VisEmacs for working with Microsoft Visual C++ and Microsoft Visual Studio.

None of these add-ons is required for Emacs to run. They are conveniences for some people and necessities for others. These add-ons bring NT Emacs up to the level of usefulness that UNIX Emacs users already enjoy.

ispell for Spelling

If you are interested in Emacs only for working with source code, and imagine you aren't interested in spell-checking, think again. Many users judge a program by its user interface, and how well your text messages are spelled is part of how they will judge your program. One thing Emacs lets you do is block out a text message in your source code and spell-check it.

ispell is on the CD-ROM that accompanies this book. Run the CD-ROM and install ispell from it.

gnuclient for Associations

In the Win32 world, it is possible to associate a file extension (the last three or four characters of a filename, separated from the rest of the filename by a period or dot [.]) with a particular application. You can click a filename, and Windows launches the associated application and loads that file into it. Often *.txt is associated with Notepad or WordPad. *.html is associated with a Web browser such as Lynx or Netscape.

In a directory window (such as the one you get when you click My Computer), select the View pull-down menu and the Options entry. Click the tab File Types. You can select a file type, and edit its association there. You could associate a file type with runemacs.exe directly if you wanted to, but that has a major problem: Each time you select a file, Windows will launch a new instance of Emacs to edit it. Emacs is a bit of a memory hog; you don't want to do that. Instead, get gnuclient.

gnuclient is a package of four programs: gnuserv, gnuclient, gnuclientw, and gnudoit. The one in which you are most interested is gnuclientw. It lets you send filenames to Emacs and have Emacs open them for editing them.

Getting gnuclient

gnuclient is on the CD-ROM that accompanies this book. If you installed NT Emacs from the CD-ROM or are running it live, gnuclient is already installed on your computer.

Using gnuclient

Put the following into your .emacs file to start the gnuserv server:

;; start gnuserv on Windows 
(if (or (eq window-system 'w32) (eq window-system 'win32)) ;.span class=compcode>Windows NT/95 
      (require 'gnuserv) 
      (setq server-done-function 'bury-buffer 
      gnuserv-frame (car (frame-list))) 
      ;;; open buffer in existing frame instead of creating new one... 
      (setq gnuserv-frame (selected-frame)) 
      (message "gnuserv started."))) 

To associate file types with Emacs, go back to the File Types window and edit a suitable association, say for batch files. The file type to edit is MS-DOS Batch File. Highlight that file type, and click Edit. Under Actions, select Edit, and then click the Edit button. Either use browse to locate gnuclientw.exe or type the full path to it. Include the extension, or Windows will reject the association. When you insert the command-line parameter, remember to put it in quotes:

gnuclientw "%1"

If you are unable to edit the existing Edit action, create a new one, Edit with Emacs, and go from there.

One of the most powerful things you can do is provide a link to gnuclientw in your SendTo folder. This lets you right-click a file and send it to Emacs to open, whether you have associated it with Emacs or not.

Open the SendTo folder on your computer. On NT, there is a SendTo folder for each profile. On Windows 95/98, there is one under the c:\windows directory. There, create a shortcut pointing to gnuclientw.exe, with the parameter in quotes.

Insert Illo: emacs.prop.gif

VisEmacs for Visual C++

You can find VisEmacs on the CD-ROM. If you are using a non-Intel processor, you should be able to compile from the extracted source. If you are accustomed to working with the Microsoft Visual products, you should have no problem following Christopher Payne's installation instructions in the file ReadMe.txt.

There are further notes in the FAQ on integrating Visual C++ and NT Emacs. They are unfortunately slightly out of date, as they were written for Emacs 19.34.1 on Windows 95, using Visual C++ 4.0 Standard Edition. Emacs is now up to 20.3.1, and Visual C++ up to Version 6.

Sams Teach Yourself Emacs in 24 Hours


Hour 2: Using Emacs in Microsoft Windows

Previous HourNext Hour

Sections in this Hour: