Make Emacs Faster

Viagra for the grandad editor!

Make Emacs Faster
Share on:

๐Ÿ“ This guide is outdated. There exists an AUR package called emacs-nativecomp which you can install to achieve the same result.

Emacs is probably my favourite “text editor” to use on Linux, as it is so extensible. However, the one thing that puts many people off using it (ignoring its complexity), is the fact that it is only single threaded, and can at times feel a little sluggish when compared to other editors like Vim.

With Emacs 28.1, along with the libgccjit (Just-In-Time Compilation with GCC) library, we now have the ability to translate Emacs Lisp into machine code on demand - which will speed things up. With this, you may notice a drastic speed increase during your day-to-day use of Emacs.

๐Ÿ“ For this guide I’m using an Arch-based Linux distribution called EndeavourOS, and the DOOM Emacs config.

Uninstall Emacs

If you’ve already installed an older version of Emacs with your package manager - uninstall it:

yay -R emacs

Download Emacs

First we need to download the source code for the latest version of Emacs. For this example we’ll be using Emacs 28.2, but the same steps will apply for later versions.

cd
wget https://ftp.gnu.org/gnu/emacs/emacs-28.2.tar.xz

Unpack

Then we need to unpack it:

tar -xf emacs-28.2.tar.xz

Let’s navigate into the Emacs source folder:

cd emacs-28.2/

Install libgccjit

Now let’s install the required JIT compilation with GCC backend library:

yay -S libgccjit

Building

We can now build Emacs:

./configure --with-native-compilation

Now, we can make:

make -j$(nproc)

And lastly, install it for use:

sudo make install

# where is it?
whereis emacs
# > emacs: /usr/local/bin/emacs /usr/local/lib/emacs /usr/share/emacs

# check version
emacs --version
# > emacsclient 28.2

DOOM Emacs Users

Once you’ve compiled and installed Emacs with native compilation, ensure you run the following command to compile your Emacs packages:

doom sync

Emacs Client

I like to run the emacsclient rather than launching a single instance of Emacs when needed. By doing this I can launch into it faster, as the emacsclient runs continually in the background and will spawn new windows almost instantly..

In your application startup add this:

emacs --daemon &

If you use a window manager, you may wish to add a command to launch emacsclient in your autorun script, like I do, as shown here using awesomeWM.

In your hotkeys, you can add a command to launch Emacs in client mode. This is how I do it using sxhkd:

# ~/config/sxhkd/sxhkdrc

# Emacs
super + alt + m
    emacsclient -c -a emacs

Set Emacs As Default Editor

You can make Emacs your default text editor by setting up a few variables which your system will use when you log in. To do this, edit your .profile file in your $HOME and add the following:

# ~/.profile
export EDITOR="emacsclient -t"
export CODEEDITOR="emacsclient -c -a 'emacs'"
export VISUAL="emacsclient -c -a 'emacs'"
export SUDO_EDITOR="emacsclient -t"