Tips for using emacsclient and server-mode on OSXPosted: August 11, 2009
Emacs is a great editor, but has one setback – it is slow to startup if you have loads of packages to load. On my G4 iBook, it takes around 45 seconds before Emacs becomes usable (OK – I use lots of packages).
However, Emacs is not meant to be restarted every so often. The canonical usage is to start Emacs once, invoke the server mode (see EmacsClient) and then use the emacsclient from the command line to invoke the editor instance when needed. I.e., the classic client/server model for editing!
Note that by default, this uses local sockets and hence the clients can connect only to the local machine’s server. However, there is an alternate mechanism to use standard TCP/IP sockets, which allows connections to remote Emacs servers. See the emacsclient info page for details.
(Note: With the latest 23.1 version of Emacs, there is an alternate mechanism to start Emacs in a server mode – will post this in a future article).
In order to enable this feature, server-mode needs to be run within an active Emacs session (V22 and above). This can be achieved by:
Alternatively, you can add the following line at end of your .emacs file:
Note that a running Emacs session needs to be present before the clients can connect.
The next step is to invoke emacsclient from the command line (or from a script) with the file name to edit.
$ emacsclient <file to edit>
This will cause a new file buffer with the requested file to be opened in the running Emacs session. The emacsclient that invoked the edit will block till the running Emacs returns control via ‘C-x #’ (server-edit)
You can use the -n option to the emacsclient invocation to prevent the blocking.
On my system, I have aliased the ‘emacsclient’ binary to be ‘ec’, which is much simpler to type.
I.e., in my `.bashrc’, I have
alias ec emacsclient
I have also defined $EDITOR to use ‘emacsclient’ when possible, and ‘vi’ otherwise. However, instead of directly assigning to the $EDITOR environment variable, I have a small shell-script to define the default editor I want to use. This script is then assigned to the $EDITOR variable.
The code for the default-editor shell-script (named ‘default-editor’) is:
#!/bin/bash # Set up a default editor for the shell # Use Emacs if already open, and vi in other cases (including fallback) # Note that this will set the default emacsclient, which may not be what you want # if multiple versions of emacs are available. In that case, you can put the absolute path # of the emacsclient that needs to be used. # E.g., the Cocoa 23.1 emacsclient on OSX is actually located at: # /Applications/Emacs.app/Contents/MacOS/bin/emacsclient EMACS_EDITOR=`which emacsclient` if [ -x "$EMACS_EDITOR" ]; then $EMACS_EDITOR -a vi "$@" else vi "$@" # Never fails! fi
And then, in the .bashrc file, I have:
if [ -x $HOME/bin/default_editor ]; then export EDITOR=$HOME/bin/default_editor else export EDITOR=`which vi` fi
BTW, the code above assumes that you use bash as the primary shell in your terminal. You can however trivially modify the scripts above for your specific shell.