If you create an emacsclient launcher by copying a .desktop file (e.g. the Emacs one), it does not behave in the standard Unity launcher manner. It always launches a new window; it doesn't recognize that existing Emacsclient windows exist to jump to them. How can I make it behave?

As background, this is a very useful usage mode for Emacs, where you run it as a daemon: emacs --daemon, and then you summon an emacs window with emacsclient. A good way to do this for habitual emacs users is to put emacs --daemon into your Startup Applications.


The solution is to create an emacsclient.desktop file including a directive to it the class of the window using the StartupWMClass settings. Without it, Unity can't tell that that window came from Emacsclient. This will create one based on the contents of the Emacs one (/usr/share/applications/emacs23.desktop):

mkdir -p ~/.local/share/applications
cat > !$/emacsclient.desktop <<EOF
[Desktop Entry]
GenericName=Text Editor
Comment=View and edit files
Exec=/usr/bin/emacsclient -c %F

Place the emacsclient in the launcher (e.g. tap Super, type "emacsclient", drag it on), and log out/in.

  • In my .bashrc, I define TMPDIR=${HOME}/tmp so temporary files are under my encrypted $HOME. emacs, invoked from the launcher, doesn't see this definition, but emacsclient, invoked from a shell does. emacs (after server-start) opens a socket in /tmp, emacsclient tries to communicate with emacs over a socket in ${HOME}/tmp, and does very poorly. Can I use emacsclient.desktop to pass the right value for TMPDIR?
    – waltinator
    Sep 28 '11 at 19:40
  • I moved my question to a thread of its own, and got answered. See askubuntu.com/questions/64005/launcher-doesnt-respect-tmpdir/…
    – waltinator
    Nov 6 '11 at 23:20
  • Unity totally ignored my emacsclient.desktop for some reason. I eventually got around this by creating the launcher in alacarte (the "Main Menu" GUI) instead.
    – Brian Z
    Mar 7 '15 at 9:50

Solution by james.ferguson works well. What I wanted was to link emacsclient to so some file types as the "preferred application". For this, I found james's script with a new "Exec" line

Exec=/usr/bin/emacsclient --alternate-editor emacs24 --no-wait %F

works better for me: it runs a new emacs if none is running, does not open a new window and does not make emacs complain about clients still waiting for stuff to happen.

Now I can click on files and they are opened in a running emacs, if one exists, otherwise emacs starts.


Another way is to use a script:

if [ "$(pidof emacs)" ] ; then
    emacsclient "$@" &
    emacs -mm "$@" &

So you always invoke the same command.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.