ungleich-staticcms/content/u/blog/emacs-server-the-smart-way/contents.lr

144 lines
4.2 KiB
Markdown

title: Emacs server the fun way
---
pub_date: 2020-04-23
---
author: Nico Schottelius
---
twitter_handle: NicoSchottelius
---
_hidden: no
---
_discoverable: yes
---
abstract:
Let's dive into a very nice emacs feature
---
body:
Today I want to talk about how amazing emacs is. Not because it is the
most feature complete operating system out there or because
[it fully emulates vi/vim](https://github.com/emacs-evil/evil).
No, because emacs has a very nice feature called **emacs server**.
## What's an emacs server?
If emacs is not an operating system, at least emacs stands for
"**e**ight **m**egabytes **a**nd **c**onstantly **s**wapping", doesn't
it? (This is actually from times where 8 megabytes were quite a lot of
memory)
So why do people make fun of emacs and how is it related to the emacs
server?
Emacs, like any other operating system, loads a lot of things at
startup. In my case this is initialising
[org-mode](https://orgmode.org/), starting my mail client, calculating
my agenda view to tell me what to do today.
An emacs server creates a special emacs process that listens on a
socket for connecting to it. This way the initialisation is already
done before you connect to it and all
configurations are already loaded. This is the actual "slow" part of
emacs. And is a bit similar to starting python, which also needs to
load its libraries at start.
With the emacs server running, you can connect to it using the
**emacsclient** program.
As a matter of fact,
[rxvt-unicode](http://software.schmorp.de/pkg/rxvt-unicode.html) also
knows about a server mode (checkout the manpage, look for
**urxvtd -q -f -o**). For rxvt-unicode, you'd use **urxvtc** to
connect to it. So quite simlar.
## What is so cool about the emacs server?
Saving a lot of response time and making working with emacs **feel**
much faster is the obvious advantage. However, there is a much bigger
one:
With the emacs server, you can connect to it from the terminal **and** X
Window. Because the emacs server also manages the buffers ("open
files" for non-emacs users), you can view the same open file from the
terminal or an x window.
## Turning the notebook into a server
As you might know, we at ungleich are pretty much into IPv6. So all of
our devices are generally speaking world-wide reachable. Our work
notebooks are no exception from that. In fact, most notebooks even
have their own [/48 IPv6 network assigned via
VPN](/u/products/ipv6-vpn/).
So if I am away from my notebook, but need to check my open (and
potentially unsaved) notes or view my emails, I can use any other
computer, ssh to my notebook and type **emacslient -nw** in the
terminal.
While my regular emacs is running as an X11 window, I can select,
display and work in all buffers that I have previously opened in the
emacs server. In the terminal, on a remote computer.
## How to configure your system to use the emacs server
In my case I start the emacs server when I start X11 in my .xinitrc:
```
eval $(ssh-agent)
...
urxvtd -q -f -o
emacs --daemon
...
```
Instead of running emacs --daemon, you can also use **M-x
server-start** in a running emacs process.
And because I always want to have my mail client open, just after I start
i3, I have i3 launch the following command:
```
ssh-add </dev/null && emacsclient -c -e '(mu4e)'
```
The connection to my mailserver is tunneled via SSH to prevent
security issues from using SSL/TLS. Thus I need to add all my ssh keys
to the ssh-agent, before starting my mail client.
To actually open a new emacs windown (aka "frame" in emacs speech), I
use the following configuration in my **~/.i3/config**:
```
bindsym $mod+Tab exec emacsclient -c
```
## How does this look like?
Below you find a screenshot of my writing this article. The upper
window is the X11 window, the lower window is a terminal window (they
happen to be configured to have the same nice background colour).
![](emacs.png)
## Updates
### 2020-04-23, 17:23
[MutoShack](https://www.reddit.com/user/MutoShack/) pointed
out on
[reddit](https://www.reddit.com/r/emacs/comments/g6nqwv/emacs_server_the_fun_way/foazghz/)
that using
```
emacsclient -a "" -c
```
is actually smarter than just using
```
emacsclient -c
```,
because it will start the daemon if it is not already running. No day
that you don't learn something!