Hacking emacsclient in cygwin

I don’t know why I haven’t ever got around to this, but here—finally—is a hack to have Cygwin only ever start one instance of emacs (especially important in cygwin, because Win32 does not appear to share program texts between processes… )

First, have Emacs start it’s server whenever Emacs is started. Put this in your .emacs file:


Next, add the following function to your .bashrc file

function emacs(){
  emacsclient $* || /usr/bin/emacs $*

Now, whenever you type emacs somefile, instead of loading a fresh emacs process (expensive operation in Windows/cygwin), emacsclient will be run, and attempt to connect to a running emacs server to edit somefile. If there is no server (e.g. emacs has not been started), then emacsclient will fail. In this case, emacs will be started instead to edit the file, and the .emacs file makes sure that the server is ready for emacsclient next time.

2 thoughts on “Hacking emacsclient in cygwin

  1. Are you backgrounding your instance of emacs after you are done editing a file? If so, I didn’t think emacsclient was able to launch a backgrounded instance. Or are you simply running multiple terminal windows to get this to work?

  2. Yes, I am. Actually, that makes me think: I should have the shell function fire up /usr/bin/emacs& $* instead… whoops.

    So long as you’ve started emacs’ server (using (server-start)), then emacsclient will connect on the socket that emacs’ server listens and instruct emacs to visit the file. emacsclient can do all sorts of tricks, even fire up a connection to a remote running emacs instance and display it’s “frame” on a local X11 display (using ssh). Check out the wiki: http://www.emacswiki.org/emacs/EmacsClient

Comments are closed.