Werden wir Helden für einen Tag

Home | About | Archive

Advent of emacs #4: How I launch emacs

Posted on Dec 4, 2022 by Chung-hong Chan

emacs has a bad reputation for being “slow”. It is not that difficult to find derogatory expansions of the 5 characters e, m, a, c, and s. My favorite is “Eight Megabytes And Constantly Swapping”. It talks about how memory hungry emacs was. In the old days, running emacs could quickly fill up 8M of RAM and then the computer needs to swap (using harddisk as virtual memory). Swapping is slow. And 8M of RAM was a lot. In 1987, a 286 Unix machine with 4M of RAM costed USD 17,995 or USD 47,207 in 2022 money.

I did my research. Source: Computerworld (26 Oct 1987)

Use emacs like vi(m)?

Prejudice such as “Eight Megabytes And Constantly Swapping” does persist, but the technology doesn’t. The outdated iPhone 6S Plus on my table has 2G of RAM. That’s 250x of 8M. The modest laptop computer I am using now to type this blog post has 32G (4000x). Even the graphic card in it has 4G. At the moment I am writing this in emacs, emacs takes around 900MB of RAM. It sounds a lot. But the not-so-important Spotify playing music in the background takes 6.4G. The I-am-doing-fucking-nothing RStudio takes 3G 1. In comparison, emacs is very lean, consider the fact that I use it for many things and it has a million things running from within.

But I do agree, emacs can be slow sometimes, especially if one runs emacs like vi(m). What do I mean? You wants to edit file “a.txt”, you go to the shell and type: emacs a.txt (Or use your GUI to click the emacs icon) to launch emacs. After editing the file, you quit emacs like vim’s :wq. Now you have another file “b.txt” to edit, you go to the shell again and type emacs b.txt. Edit the file, quit. Launch emacs again, quit emacs, launch emacs again, quit emacs, launch emacs again, quit emacs…

For a long time, I used emacs like so. And then I realized I need to wait a few seconds every time for emacs to start. When I had more customization in my configuration file, this startup time prolonged. And then I became impatient. After some digging, I realized I don’t use emacs in an effective way.

I shouldn’t quit emacs after editing a file. I should just let it stay there. If I have another file to edit, I can just quick switch to emacs and edit the file. There is no need wait for the startup time. The same way you shouldn’t launch your web browser, visit one webpage, quit your web browser, launch the web browser, visit one webpage, quit….

But sometimes, I still accidentally pressed C-x C-c to quit emacs and I needed to relaunch emacs again. And that’s another few seconds.

Is there a better a way?

emacs as a daemon and emacsclient

My current setup is to use emacs as a daemon and emacsclient.

“Daemon” is the technical jargon of a program that runs as a background process. Using emacs as a daemon actually means that emacs is always running in the background, unless one explicitly quits it. To really use emacs, one connects to this background process with a client. And that client is called emacsclient.

Suppose you are using the blank-state emacs. You can quickly experiment with it by launching emacs as a daemon like so: emacs --daemon. emacs will start its startup cycle and then run in the background. And then you can run emacsclient to connect to the daemon. You should have a working emacs immediately with no delay.

On my Ubuntu machine, I run the emacs daemon as a systemd user service. The emacswiki has the detail. But it boils down to create the file ~/.config/systemd/user/emacs.service.

Description=Emacs text editor
Documentation=info:emacs man:emacs(1) https://gnu.org/software/emacs/

ExecStart=/usr/bin/emacs --daemon
ExecStop=/usr/bin/emacsclient --eval "(kill-emacs)"


and then

systemctl enable --user emacs
systemctl start --user emacs

For some cases, I really need to restart my emacs daemon. I can go to my shell and systemctl restart --user emacs.

How I launch emacs?

Let’s go back to the original question: How I launch emacs?

From now on, I launch emacs by: emacsclient -c (If I need graphic) or emacsclient --tty -c (If I want to use emacs in terminal). You may type emacsclient --help to see what -c and --tty mean. I also make aliases in my .*rc.

## why type emacs if I can just type e?
alias e='emacsclient --tty -c'
alias eg='emacsclient -c &'

Emacs macht alle Computer schnell

A lot of the above operations are in the shell. But how do I reach the shell? Well, I will talk about it tomorrow.

Coda: StumpWM

You can also make this as an alias for your window manager, especially key-board driven ones such as StumpWM.

I am using StumpWM. In case you don’t know, it is a window manager written entirely in Common Lisp. The configuration is also in Common Lisp. Some YouTubers claim that StumpWM is strange or it deserves in the buttom of their tier list. And I use it anyway.

The best feature of StumpWM is run-or-raise: It launches something if it is not yet running, brings it to the front otherwise. In my StumpWM configuration, I configure this way:

(defcommand emacs () ()
	    "Start emacs unless it is already running, in which case focus it."
	    (run-or-raise "emacsclient -c" '(:class "Emacs")))

And by default, StumpWM maps the key combination “prefix key and ‘e’” to this command emacs. By “prefix key”, the default value is “Ctrl t”. So, by default, you can “run or raise” emacs by pressing C-t e. On my machine, I remap “prefix key” to “Ctrl .” (fucking strange, don’t try this at home). So, I actually lanuch emacs by pressing C-. e. I also bring emacs to the front by pressing C-. e.

My StumpWM configuration file is available here.

  1. Similar to Spotify and RStudio, VS Code is also an Electron app. So… 

Powered by Jekyll and profdr theme, a fork of true minimal theme