Launch Emacs with Systemd


Emacs can be started and run as a server using systemctl (see Emacs Server and Managing Emacs Server as Systemd Service) to reduce startup time and help enforcing the execution of a single instance of the editor (so that, for instance, the same file does not happen to be edited at the same time in two different Emacs instances).

cp /usr/share/emacs/27.2/etc/emacs.service ~/.config/systemd/user
systemctl --user enable emacs

Note. If you run into issues with the authentication agent you can try uncommenting the line:


Alternatively, a server on a X11 instance can be obtained using an emacs-lisp function:

(require 'server)
(unless (server-running-p)

Even is the server is started, I find myself invoking other instances of Emacs, rather than reusing the existing instance. In such cases, it might be useful to add an icon for Emacs client. This can be done in Linux by adding a file to ~/.local/share/applications (see the section Deskttop Client of the Emacs Wiki):

cat ~/.local/share/applications/emacs.desktop
[Desktop Entry]
GenericName=Text Editor
Comment=Edit text
Exec=emacsclient -c -a "emacs" %F

If the file is named emacs.desktop it will shadow the system entry. You can use another name for the file, if you prefer to keep an entry also for Emacs

Finally, I also define a few aliases in my .bashrc to prefer emacsclient over emacs:

export EDITOR="emacsclient -t --alternate-editor /usr/bin/emacs" 
export VISUAL="emacsclient -c --alternate-editor /usr/bin/emacs"

export TEXEDIT="emacsclient -t +%d %s -a /usr/bin/emacs"

# Override Emacs... although not always a good idea
alias emacs="emacsclient -c -a /usr/bin/emacs"

alias emacs_gui="emacsclient -c -a /usr/bin/emacs"
alias emacs_cli="emacsclient -t -a /usr/bin/emacs"