Emacsclient Setup with Desktop Integration
When using a typical text editor (e.g. gedit or vim) it is expected to be able
to quickly open and close files from your file manager or terminal. Emacs, while
a great editor, is not well suited for this workflow as typical users have long
configuration files that must be loaded slowing the start-up time. This is not a
problem if you start Emacs once and do everything inside of it, but if you are
frequently opening and closing windows you would be better off just starting
Emacs once and connect to it using the fast and lightweight emacsclient
.
Running Emacs as a daemon
emacs --daemon
This can be added to startup files that are sourced at login like your bash
profile (~/.profile
or ~/.bash_profile
) to automatically start the daemon at
login.
Using a systemd service
Alternatively, as of GNU Emacs 26.1 a systemd unit file is included, using
systemd allows for convenient management commands through the familiar systemctl
utility as used with other services.
Systemctl commands | Description |
---|---|
start | Start (activate) one or more units |
stop | Stop (deactivate) one or more units |
restart | Start or restart one or more units |
enable | Enable one or more unit files |
disable | Disable one or more unit files |
Emacs's unit file is a user service, meaning it doesn't require root access and
is started at login rather than boot. In order to control user services we must
preface our commands with --user
flag.
systemctl --user start emacs.service # Start emacs for the current session systemctl --user enable emacs.service # Enable emacs to be started at login
Open files from your graphical file browser
Now like any other graphical editor you will need a desktop icon in order to
open files from your graphical file browser. To do so create a desktop file for
emacsclient and place it in ~/.local/share/applications/emacsclient.desktop
1
[Desktop Entry] Name=Emacs Client GenericName=Text Editor Comment=Edit text 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=emacsclient -c %f Icon=emacs Type=Application Terminal=false Categories=Utility;TextEditor;
Note that some distributions ship with an Emacs client desktop icon by default, like Debian, so this step may be unnecessary unless you want to make use of the script below.
Open all files in one frame
This desktop file will result in each file being opened as a new Emacs frame (a new window). This will clutter up your desktop if you open many files. A better approach would be to open a new frame only if none exist otherwise open the file as a new buffer in the existing frame. This is accomplished with the following script 2
#!/bin/bash # if no args open new frame if [ $# -eq 0 ]; then emacsclient -c -n exit fi emacsclient -e "(frames-on-display-list \"$DISPLAY\")" &>/dev/null if [ $? -eq 0 ]; then emacsclient -n "$*" else emacsclient -c -n "$*" fi
Make the script executable:
chmod +x emacsclient-one-frame.sh
And place it somewhere accessible from your $PATH
. I use ~/bin
but if you would
like it hidden ~/.local/bin
is another option. Add both to your $PATH
by adding
the following to your ~/bashrc
:
PATH="$HOME/.local/bin:$HOME/bin:$PATH" export PATH
Change the Exec line in the desktop file to the script.
Exec=emacsclient-one-frame.sh %f
Set as default in Nautilus
In nautilus file browser find a text file type you wish to be associated with Emacs client. Right click it, select properties, move to the "Open With" tab choose Emacs Client and press "Set as default".
Add a command alias
If you frequently start Emacs from the commandline you'll want to
start using emacsclient to speed up your startup time. In your
~/.bashrc
consider adding one of the following aliases
alias emacs='emacsclient-one-frame.sh' alias ec='emacsclient -t' # Opens emacs inside terminal
Set emacsclient as your default terminal editor
In your ~/.profile
or ~/.bash_profile
add the following lines
VISUAL='emacsclient -t' export VISUAL
VISUAL
means full-screen or visual editor, this variable should be used over
EDITOR
. EDITOR
is a fallback used when full-screen editing features are not
available. 3
Comments
Email comments and corrections to comment@taingram.org.
Submissions may be posted publicly unless requested otherwise in your email.