Emacs, Chrome, and Daemons

I  really liked the Chrome extension that lets you edit multiline text edits in Chrome with emacs (or, I guess any other editor — as though there are any other editors). However, it has to have a server running. You can run a python server or one in e-lisp. But the lisp one requires emacs to be running already.

Here’s how I set mine up.

1) Autostart emacs –daemon using your choice of methods (autostart, Xsession, whatever)

2) In your .emacs you need this:

(if (and (daemonp) (locate-library "edit-server"))

 (progn
(require 'edit-server)
; can add options here like (setq edit-server-new-frame-mode-line t) (setq edit-server-new-frame-minibuffer t)
(edit-server-start)))

 3) Then in your .bashrc put this:
if [ -z "$DISPLAY" ]
then
  alias emacs='emacsclient -t'

   export EDITOR='emacsclient -t'
else
   alias emacs='emacsclient -c -n'
   export EDITOR='emacsclient -c -n'

 fi

 Works great. Of course, you need to start the daemon manually THIS TIME (and execute the script lines) or log out and log back on for this to work.

By the way, once you set up the alias, you can’t easily restart the emacs daemon. Remember you can override an alias with quotes: ’emacs’ –daemon

Update: you can set ALTERNATE_EDITOR in your environment to the empty string and emacsclient will start a daemon if one is not running. So that means you could skip step 1 if you add:


 export ALTERNATE_EDITOR=""

to step 3. This is nice because if you kill the server (to reload your .emacs, for example) or it dies (unlikely) then things till work without the user having to reload the daemon manually. Of course, you can still keep step 1 as well which makes your initial load a little faster, and still protects you against a dead daemon.

Leave a Reply