Start Emacs In Ubuntu The Right Way
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)).
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.
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
- systemd/User https://wiki.archlinux.org/index.php/Systemd/User#Basic_setup
- EmacsAsDaemon https://www.emacswiki.org/emacs/EmacsAsDaemon
- https://www.freedesktop.org/software/systemd/man/systemd.service.html
- Using emacsclient to Speed up Editing
- How to activate tap to click touchpad's feature in Ubuntu in 4 stepsMarch 4, 2021
- Difference between suspend and hibernate in Ubuntu and how to execute them from command lineApril 12, 2020
- Solving Google Chrome's gpu-process error message in Ubuntu LinuxJanuary 7, 2019
- Solving Google Chrome's secret service operation error message in Ubuntu LinuxJanuary 7, 2019
- Start Emacs In Ubuntu The Right Way
Articles
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
·