Words in Boxes

Nouns, verbs, and occasionally adjectives.

Tuesday, September 21, 2010

Making Emacs Server Play Nice With Windows

Some people like to use Emacs for everything, even Google Maps. That's way too much for me, but I do like to have Emacs available with a minimum of fuss. Here are two tricks that I use to make that happen: (1) creating a right-click "Edit in Emacs" menu item in Windows Explorer, and (2) open the current file in Visual Studio in Emacs. I originally attempted this a while ago, hit a wall, and gave up. I just got it working, so I wanted to write up what I did.

If Emacs were a normal Windows program, this would be easy, but it predates Windows by over a decade so I guess it gets to play by it's own rules. In order to open a file in an already running instance of Emacs, you have to use the Emacs client executable, and not the regular Emacs executable. The client connects to a server running in Emacs, which you have to explicitly start.

(The following instructions are all a distillation of the instructions available from the Emacs Wiki.)

First, add this to your .emacs:

(require 'server) 
(server-start)

This will start the server when Emacs starts. To make things even more fun, this sometimes fails on Windows 7. See here for a solution.

Windows Explorer

We're going to add a right-click menu to Windows Explorer labeled "Open with Emacs," which does exactly what it says it does.

The next step is to edit the registry. Here's how:

1. Open up the Registry Editor (Windows-R, type "regedit").
2. Create a new key [HKEY_CLASSES_ROOT\*\Shell\Edit in Emacs\Command].
3. Set the value of the command to "C:\Program Files\emacs-23.2\bin\emacsclientw.exe" -na "C:\Program Files\emacs-23.2\bin\runemacs.exe" "%1" (adjusting the paths as necessary).

registry

And that's it. This first attempts to open the file with a running Emacs server, but if there isn't one (i.e. Emacs isn't running), then it opens a completely new instance (that's what the "-na" option is for). If you normally launch Emacs with a batch file (which is common), the –na argument can point to that instead.

Visual Studio

Sometimes when editing a file in Visual Studio, I want to punt the file to Emacs for a bit and work on it there. Fortunately, it's really easy to call the Emacs client from what's called an External Tool in Visual Studio. You can even assign it a keyboard shortcut, such as C-k,C-e, and bam, the file is opened in Emacs.

The theory is the same as before, but the details are a bit different. Go to Tools > External Tools, and Add a new tool with the following configuration:

Title: Edit In Emacs
Command: C:\Program Files\emacs-23.2\bin\emacsclientw.exe
Arguments: –na "C:\Program Files emacs-23.2\bin\runemacs.exe" "$(ItemPath)"

visual_studio_external_tools

Now, when you go to Tools > Edit in Emacs, the current file should appear in Emacs.

visual_studio_menu

To assign this to a keyboard shortcut, go to Tools > Options, then Environment > Keyboard. In the "Show commands containing" text box, type in "externalcommand." You'll see a bunch of options that look like this:

visual_studio_keyboard

As you can tell, this is more complicated than it needs to be. Each "Tools.ExternalCommandX" entry corresponds to an external tool. The number matches the position on the external tools list. In other words, if I made "Edit in Emacs" the first external tool, then I would use "Tools.ExternalCommand1." Choose that, put your cursor in "Press shortcut keys," type your keyboard shortcut (C-k, C-e), and press "Assign." That's it.

I'm James Sulak, a software developer in Houston, Texas.

You can also find me on Twitter, or if you're curious, on my old-fashioned home page. If you want to contact me directly, you can e-mail comments@wordsinboxes.com.