Hacking emacsclient in cygwin

This post was originally published at sinewalker.wordpress.com on 18 April 2006.


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:

(server-start)

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.