Emacs Config From Scratch, Part 3: LSP & Tree-sitter
In this third part of my Emacs Config From Scratch series, I configure LSP and Tree-sitter. · Frankfurt, GermanyThis is Part 3 of my series, Emacs Config From Scratch1, where I create my perfect editor using Emacs. In this post, I’ll do some housekeeping, set up LSP2, language modes for Rust, Go, TypeScript and Zig, and add search.
Table Of Contents
Housekeeping
The first thing I want to do is make the UI titlebar match the Emacs theme and hide the file icon:
The next thing is automatically loading $PATH
from my shell using
exec-path-from-shell—this
is important so Emacs can find our installed binaries, e.g. for language servers:
Another problem I ran into was writing square brackets when on the MacBook, as it
would interpret the keys as Meta-5
/Meta-6
.
I fixed that by updating the keybindings from
Part 1:
I like to keep most of my code at 80 characters, so let’s add a ruler:
Finally, we want to store backup files in ~/.saves
instead of next to the file
we’re saving:
LSP
Let’s install company-mode first, for auto-completion:
Now we configure the built-in LSP package eglot:
This runs eglot-ensure
in languages we have language servers installed for.
It also sets up SPC l r
to rename a symbol and SPC l a
to prompt for code
actions.
Tree-sitter
We’ll use treesit-auto to automatically install and use tree-sitter major modes:
This is handy because it doesn’t require us to think about using e.g.
zig-ts-mode
instead of zig-mode
, it handles everything for us.
Language support
Next, we install all language modes we need:
I’m using SPC m
to change based on major-mode, e.g. means SPC m t
means test
in most programming modes, but won’t exist in markdown-mode
.
Search
Sometimes we don’t know what file we’re looking for, so let’s add rg.el to help us find it:
This opens a Magit-like menu and allows you to search in various modes (dwim, regex, literal, etc.).
Wrapping up
Opening a Zig project now looks like this3; see also the final
init.el
:
I’m going to switch to Emacs as my primary editor and tune it further in the coming weeks. In the next part, I want to add support for Org-mode, show a dashboard on startup, enable font ligatures and fix all the small things that I’ll find.
Subscribe to the RSS feed so you don’t miss Part 4, and let me know what you think!