A Simple tree-sitter Config That Works

Recently, I’ve discovered a number of posts on Reddit — like tree-sitter-hl-mode does not do anything or How to use Emacs 29 Tree-sitter? — and decided to share my simple tree-sitter setup that seems to be working on my Emacs 28.2 like a charm.

Step 1. OS: Installing the tree-sitter app

First of all, I’ve downloaded the latest pre-build binary archive for my system from this GitHub repo, in my case that is tree-sitter-linux-x64.gz because I’m on Pop!_OS 20.10. Then I’ve unpack the archive, put the standalone binary in my $HOME/bin that is in my $PATH and therefore is accessible from anywhere, and changed its mode to executable by chmod +x tree-sitter.

The latest tree-sitter app is installed.

Step 2. Emacs

Step 2.1. Emacs: Installing tree-sitter packages

Then I’ve installed tree-sitter (the parsing system) and tree-sitter-langs (the grammar bundle) Emacs packages using package-list-packages.

The tree-sitter packages are installed.

Step 2.2. Emacs: Configuring tree-sitter

I’ve added the following lines to my ~/.emacs.

;; 2022-12-04
(require 'tree-sitter)
(require 'tree-sitter-langs)


(add-hook 'python-mode-hook #'tree-sitter-hl-mode)

That’s it. Quite simple, isn’t it?

Please pay attention to the last line. It seems that the tree-sitter highlighting mode provided by tree-sitter-hl-mode is not activated when you load a file into the buffer even if the global tree sitter mode is on. Adding the hook for the particular source code mode solves this problem.


Now I have a pretty looking Python code.

More on tree-sitter and its internals can be found at https://tree-sitter.github.io/tree-sitter/.

Happy tree-sittering!