Zed Lopez

Daemon Emacs

A nifty feature in the current development version of Emacs (now available in a pretest release candidate) is that you can start it as a daemon, to which graphical and terminal clients alike can attach. A not so nifty lack of feature is there being no easy standard way to do the obvious: launch a client if the daemon’s running; if it’s not, start the daemon, and then launch the client. So everyone cobbles together their own. Here’s mine, which cribs from here.

#!/bin/bash
if [ -z $DISPLAY ] ; then
OPT=“-t”
else
OPT=“-c”
fi

if [ -z “$*” ]; then
OPT=“$OPT -e (raise-frame)”
else
OPT=“$OPT $@”
fi
emacsclient $OPT 2>/dev/null || (
\t(emacs —daemon)
\tsleep 1
\temacsclient $OPT)

I have that as /usr/local/bin/editor. To stop emacs nicely, and get prompted to save unfinished business, I have another script with:

/usr/local/bin/editor -e ‘(save-buffers-kill-emacs)’