Launch Emacs with Systemd

Menu

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:

Environment=SSH_AUTH_SOCK=%t/keyring/ssh=

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

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

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]
Name=EmacsClient
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 -a "emacs" %F
Icon=emacs
Type=Application
Terminal=false
Categories=Development;TextEditor;
StartupWMClass=Emacs
Keywords=Text;Editor;

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"