Using server and clients for instantaneous startup

If you use an advanced emacs configuration like my recommended set up prelude, then the startup time for emacs can be several seconds or longer. To avoid this, and to get other benefits I strongly suggest running emacs with a server and clients.

The way this works is that the first time you start emacs, you start a server, and then any other emacs sessions you start just connect to that server. This means they start instantly, and also have the same buffer list as every other emacs window (frame in emacs terminology) that you have open.

To do this we need to make a simple shell script

#! /bin/tcsh -f

# start emacs server - replace with path to your emacs
/Applications/ --daemon

# start emacs client - replace with path to your emacsclient
# this is usually in the same place as your emacs executable
/Applications/ -c $argv

and save it somewhere sensible like ~/scripts/emacs_daemon.csh

Then set up an alias for your emacs command in your ~/.cshrc or similar:

# alias to start emacs in client mode
# starts server if one is not already started
# replace with path to your emacsclient
alias em '/Applications/ --alternate-editor "~/scripts/emacs_daemon.csh" -c'

Now you can use the command em to start emacs every time. The first time it will take as long as usual, but after that it will be instant. Running em file1 file2 or similar will open a new emacs window with the named files as you would expect.

One last point to note is that since your emacs windows are now clients of an emacs server that is running in the background, closing the window will not stop the server. If you want to close the entire emacs session and stop the server (e.g. so you can start a fresh emacs session after changing your config file), you can use M-x save-buffers-kill-emacs, which I have bound to C-x c for convenience. To do this, add the following to your emacs config file

;; set shortcut to kill whole emacs session
(global-set-key (kbd "C-x c") 'save-buffers-kill-emacs)
  • This is intriguing. As Emacs is single threaded, could you ensure that network-bound applications like ERC are running in the client, and not the server, so that they don’t appear to freeze the session when things go pear-shaped?

    • Ben Maughan

      To be honest I don’t know! The Emacs stack exchange would be a good place to ask this.

  • Derek

    I keep getting this error:

    Debugger entered–Lisp error: (error “Unknown option `–alternate-editor=~/.emacs.d/emacs_start.csh'”)

    signal(error (“Unknown option `–alternate-editor=~/.emacs.d/emacs_start.csh'”))

    error(“Unknown option `%s'” “–alternate-editor=~/.emacs.d/emacs_start.csh”)

    command-line-1((“–alternate-editor=~/.emacs.d/emacs_start.csh” “-c”))



  • Derek

    nvm, I forgot to use emacsclient instead of emacs in the alias