Running Multiple Emacs Servers

Tycho Garen has an interesting use case. Because he uses a single laptop to work on many projects, he wanted a way to have separate Emacs instances so that his buffers don’t get mixed up between projects. That’s easy enough to do by simply invoking multiple Emacs instances but then if his X-Windows dies he loses everything. As Sacha told us, the way to avoid that is to run Emacs in daemon mode. But how can you run multiple Emacs instances in daemon mode?

Garen has the answer. His solution involves having emacsclient connect to the server with TCP. Follow the link for the details. One of the commenters points out that this can be a security problem if external users can reach the socket. Stefan Monnier replied that there’s a secret key in the server file so the security problem isn’t as bad as it sounds but in any event you can still use local Unix sockets by specifying --socket-name rather than --server-file as an option to emacsclient. This is explained (sort of) in the emacsclient options section of the Emacs manual.

Garen shows another nice trick in his post. I often see people starting the Emacs daemon at boot time by messing with systemd or whatever their system uses to control the boot process. Garen simply makes use of the fact that crontab has a @reboot “time” that will perform the action when the system boots. That solves the problem nicely and easily. Again, see his post for the details.

Most of us probably won’t have occasion to need multiple Emacs instances but it’s nice to know how to do it in case the need does arise.

This entry was posted in General and tagged . Bookmark the permalink.