How can I make running emacsclient from the unity launcher reuse an existing emacs window

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.


Solution 1:

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]
Version=1.0
Name=Emacsclient
GenericName=Text Editor
Comment=View and edit files
MimeType=text/english;text/plain;text/x-makefile;text/x-c++hdr;text/x-c++src;text/x-chdr;text/x-csrc;text/x-java;text/x-moc;text/x-pascal;text/x-tcl;text/x-tex;application/x-shellscript;text/x-c;text/x-c++;
Exec=/usr/bin/emacsclient -c %F
Icon=/usr/share/icons/hicolor/scalable/apps/emacs23.svg
Type=Application
Terminal=false
Categories=Utility;Development;TextEditor;
StartupWMClass=Emacs
EOF

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

Solution 2:

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.