John Bokma

Emacsclient on Ubuntu

August 10, 2016

Over the past few days I have been using emacsclient to open files in an already running emacs in daemon (server) mode. Despite having been using Emacs for years I never got around to trying this out for a few days. And I really like it. So today I decided to switch my workflow to using emacsclient the easy way.

First, I added the following lines to my ~/.emacs.d/init.el:


;; Filename or buffer name in frame title
(setq frame-title-format
      '((:eval (if (buffer-file-name)
		   (abbreviate-file-name (buffer-file-name))

(set-face-attribute 'default t :font "Monospace-9")

Note the additional code to set the frame-title-format. Without this I ended up with --create-frame in the title bar of the frame. Somehow the version of Emacs I use considers this switch (see below) the invocation-name.

Originally, I used an alias, gemacs, which called emacs with the font I desired. But since emacsclient has no --font option I (finally) added this to my init.el as well; as shown above.

Next, I added the following lines to the end of my ~/.bashrc:

# Emacs
export ALTERNATE_EDITOR=emacs EDITOR=emacsclient VISUAL=emacsclient

If emacs is not running, and hence emacsclient can't connect to it, the alternate editor is tried. The emacsclient program does have a --alternate-editor option, but when I tried this I got a backtrace which ended with:

Error: server did not start correctly
Error: Could not start the Emacs daemon

Finally, I added the following to my ~/.bash_functions so I can always use emacs to either start a daemon or connect to it via emacsclient:

function emacs {
    emacsclient --create-frame $* >/dev/null 2>&1

Note the redirect of both stderr and stdout to /dev/null. This prevents Gtk warnings, like given below, from flooding the terminal:

(--create-frame:19621): Gtk-WARNING **: Attempting to add a widget with type Gtk
Box to a GtkMenuItem, but as a GtkBin subclass a GtkMenuItem can only contain on
e widget at a time; it already contains a widget of type GtkAccelLabel

Don't forget to use C-x # when done with editing a buffer, not C-x C-c.


home - blog - email

I am a freelance Perl programmer for hire, download my up-to-date resume (PDF).