Start Emacs In Ubuntu The Right Way

Published:
Last modified:

Overview

Emacs can be slow to start up, if you use it constantly this can be a problem and probably end up using some other lightweight editor instead.

Fortunately, there is a daemon version of Emacs that you can start once per session, loading the initialization file, and then connect to the running process with emacsclient really fast.

--daemon Start Emacs as a daemon, enabling the Emacs server and disconnecting from the terminal. You can then use the emacsclient command to connect to the server (see emacsclient(1)).

Emacs man page

From Ubuntu 15.04, systemd has become the default init system.

systemd is a suite of basic building blocks for a Linux system. It provides a system and service manager that runs as PID 1 and starts the rest of the system.

In this tutorial we will create a new systemd service to handle the Emacs server so we will be able to start, stop, restart, enable and disable the service as any other Linux service.

1. Install

From the snap service we install the latest stable Emacs version with snap install emacs –classic

2. systemd Unit

To turn Emacs into a systemd service that can be started automatically during system startup we will create a service to start the daemon.

systemd can manage services under the user’s control with a per-user systemd instance, enabling users to handle their own units.

User services like this one, should be placed in ~/.config/systemd/user/ so then we will be able to run them with systemctl –user enable service.


$ mkdir -p ~/.config/systemd/user/

--user Talk to the service manager of the calling user, rather than the service manager of the system.

The resources that systemd knows how to manage are called units.

A unit configuration file whose name ends in .service encodes information about a process controlled and supervised by systemd.

*systemd** --user instance is a per-user process, and not per-session.

We will create the unit at `~/.config/systemd/user/emacs.service**.

IMPORTANT: Latest Emacs versions already has this file delivered at /snap/emacs/current/usr/lib/systemd/user/emacs.service so we can just copy it like cp /snap/emacs/current/usr/lib/systemd/user/emacs.service ~/.config/systemd/user/ .

In ~/.config/systemd/user/emacs.service:

[Unit]
Description=Emacs Daemon

[Service]
Type=notify
Type=notify
ExecStart=/snap/emacs/current/usr/bin/emacs --fg-daemon
ExecStop=/snap/emacs/current/usr/bin/emacsclient --eval "(kill-emacs)"
Restart=on-failure

[Install]
WantedBy=default.target

Older Emacs version which don’t has the emacs --fg-daemon parameter, should use --daemon instead.

3. Enable and Start server

Now we enable the unit to be started at login with systemctl –user enable emacs.service and start the service for the current session systemctl –user start emacs.service:


$ systemctl --user enable emacs.service
Created symlink ~/.config/systemd/user/default.target.wants/emacs.service β†’ ~/.config/systemd/user/emacs.service.
$ tree .config/systemd/user/
.config/systemd/user/
β”œβ”€β”€ default.target.wants
β”‚Β Β  └── emacs.service -> ~/.config/systemd/user/emacs.service
└── emacs.service

1 directory, 2 files
$ systemctl --user list-unit-files|grep emacs
emacs.service                           enabled
$ systemctl --user status emacs.service
● emacs.service - Emacs Daemon
   Loaded: loaded (~/.config/systemd/user/emacs.service; enabled; vendor preset: enabled)
   Active: inactive (dead)
$ systemctl --user start emacs.service
$ systemctl --user status emacs.service
● emacs.service - Emacs text editor
     Loaded: loaded (~/.config/systemd/user/emacs.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2021-03-03 22:48:19 -03; 17min ago
       Docs: info:emacs
             man:emacs(1)
             https://gnu.org/software/emacs/
   Main PID: 1890 (emacs)
     CGroup: /user.slice/user-1000.slice/user@1000.service/emacs.service
             β”œβ”€1890 /snap/emacs/current/usr/bin/emacs --fg-daemon
             └─2017 /usr/bin/aspell -a -m -d en --encoding=utf-8

mar 03 22:48:08 scarone emacs[1890]: Loading ~/.emacs.d/helm-adaptive-history...
mar 03 22:48:08 scarone emacs[1890]: Loading ~/.emacs.d/helm-adaptive-history...done
mar 03 22:48:15 scarone emacs[1890]: [yas] Prepared just-in-time loading of snippets successfully.
mar 03 22:48:19 scarone emacs[1890]: Starting Emacs daemon.
mar 03 22:48:19 scarone systemd[1117]: Started Emacs text editor.

Final tweaks

Command aliases

I’ve found these aliases helpful to have at hand:

emacsclient -t
open a new Emacs frame on the current terminal (similar to emacs -nw)
emacsclient -c -a emacs
-c create a new frame instead of trying to use the current Emacs frame
-a –alternate-editor=EDITOR, if the Emacs server is not running, run the specified editor instead.
alias emax="emacsclient -t"
alias semac="sudo emacsclient -t"
alias emacsc="emacsclient -c -a emacs"           # new - opens the GUI with alternate non-daemon

Default editor

Finally, set the EDITOR and VISUAL environment variables so emacs is your default editor:

EDITOR
The name of the lightweight text editor
VISUAL
command to run the full-fledged editor

In .bashrc:

export ALTERNATE_EDITOR=""
# $EDITOR should open in terminal
export EDITOR="emacsclient -t"
# $VISUAL opens in GUI with non-daemon as alternate
export VISUAL="emacsclient -c -a emacs"

Conclusion

We created a script to run like a native Ubuntu service, then added some aliases and defined environment variables. Now we have a better experience in Ubuntu using Emacs with faster loading times and more integrated to the OS.

Changelog

  • 2021-03-03: Tested using Emacs 27.1 from snap with Ubuntu 20.04

Reference

Uruguay
Marcelo Canina
I'm Marcelo Canina, a developer from Uruguay. I build websites and web-based applications from the ground up and share what I learn here.
comments powered by Disqus


We explore how to have Emacs as the default editor in Ubuntu running as a service unit and also have useful aliases and environment variables.

Clutter-free software concepts.
Translations English EspaΓ±ol

Except as otherwise noted, the content of this page is licensed under CC BY-NC-ND 4.0 . Terms and Policy.

Powered by SimpleIT Hugo Theme

·