Minimal Tree-sitter Setup in Emacs 31
Emacs 31.1 adds treesit-enabled-modes, treesit-auto-install-grammar, and built-in grammar recipes inside each standard *-ts-mode.
You can enable Tree-sitter with one or two lines in init.el, without hand-writing major-mode-remap-alist and treesit-language-source-alist as in Emacs 29 or 30, and without installing treesit-auto.
I verified the minimal setup in GNU Emacs 31.1 and lisp/treesit.el, covering automatic grammar installation, the setq pitfall, and prebuilt environments such as NixOS.
For other Emacs 31 changes, see Seven fun new features in Emacs 31; for the Elisp Tree-sitter API reference, see Tree-sitter Major Modes and Language Grammar on emacs-doc.
Minimal config and what changed from Emacs 29 and 30
To enable all built-in Tree-sitter modes in Emacs 31.1, one line in init.el is enough.
Add the second line for treesit-auto-install-grammar if you want Emacs to compile missing grammars with no prompt.
(setopt treesit-enabled-modes t)
;; Default is 'ask (prompt before installing); set 'always to install with no prompt
(setopt treesit-auto-install-grammar 'always)
Pass a list of mode symbols instead of t to opt in per language.
(setopt treesit-enabled-modes
'(python-ts-mode rust-ts-mode typescript-ts-mode
tsx-ts-mode json-ts-mode yaml-ts-mode))
Three built-in mechanisms make this short config work in Emacs 31.1.
First, treesit-enabled-modes defines a :set function that reads the built-in 26-entry treesit-major-mode-remap-alist and populates major-mode-remap-alist when you configure it via setopt.
For languages with no classic built-in major mode (such as .rs or .ts), dispatchers such as rust-ts-mode-maybe check treesit-enabled-modes and switch into *-ts-mode.
Second, built-in mode files such as rust-ts-mode, python.el, and markdown-ts-mode append pinned grammar recipes to the tail of treesit-language-source-alist upon load.
rust-ts-mode even selects a different pinned commit depending on whether (treesit-library-abi-version) is below 15 or at least 15, and because Emacs appends built-in entries, your own overrides in init.el win.
Third, each *-ts-mode calls treesit-ensure-installed on startup.
If the shared grammar library is missing, Emacs installs it according to treesit-auto-install-grammar, so you no longer run M-x treesit-install-language-grammar by hand for each language.
| Feature | Emacs 29 and 30 built-in | With treesit-auto | Emacs 31.1 built-in |
|---|---|---|---|
| Classic-to-TS mode remap | Hand-written major-mode-remap-alist |
Automatic | treesit-enabled-modes populates 26 built-in pairs |
| Grammar download recipes | Hand-written treesit-language-source-alist |
Bundled in package | Bundled with pinned commits in each built-in *-ts-mode |
| Auto-installing missing grammars | Manual treesit-install-language-grammar |
Prompt or auto-install on file open | Controlled by treesit-auto-install-grammar (default ask) |
| Fallback when a grammar is missing | Varies by mode | Falls back to classic mode | Keeps the remapped *-ts-mode (some modes signal an error) |
Choosing treesit-auto-install-grammar and install paths
treesit-auto-install-grammar accepts four values.
| Value | Behavior when opening a file with a missing grammar | Best fit |
|---|---|---|
ask (default) |
Prompts in the minibuffer and builds on y |
Standard desktop setups installing languages on demand |
always |
Clones with Git and compiles with no prompt | Fast setup on a fresh machine |
ask-dir |
Prompts for confirmation and an install directory | Splitting grammar directories by project or environment |
never |
Skips auto-install | NixOS, Guix, or any setup providing prebuilt grammars |
Emacs writes compiled shared libraries into the first writable directory in treesit-extra-load-path, which is now a customizable user option.
If treesit-extra-load-path is nil, Emacs falls back to tree-sitter/ inside user-emacs-directory.
Auto-building requires git, a C compiler, a C++ compiler for some grammars, and a linker on exec-path.
To override one language with a different repository or commit, use the new keyword syntax in treesit-language-source-alist.
You can name :revision, :commit, :source-dir, :cc, :c++, and :copy-queries instead of padding positional arguments with nil.
(with-eval-after-load 'treesit
(add-to-list 'treesit-language-source-alist
'(json "https://github.com/tree-sitter/tree-sitter-json"
:commit "4d770d3")))
As listed in the Tree-sitter section of etc/NEWS, Emacs 31.1 also adds define-treesit-generic-mode in treesit-x.el, shipping gitattributes-generic-ts-mode and liquid-generic-ts-mode out of the box.
Pitfalls and cases that still need manual config
Testing Emacs 31.1 with emacs --batch -Q turned up three gotchas.
First, configure treesit-enabled-modes with setopt (or Customize), not setq.
Because treesit-enabled-modes updates major-mode-remap-alist inside its :set function, evaluating (setq treesit-enabled-modes t) in Emacs 31.1 left major-mode-remap-alist at 0 entries.
Switching to (setopt treesit-enabled-modes t) registers all 26 entries right away.
Second, watch the combination of treesit-auto-install-grammar set to never and treesit-enabled-modes set to t.
In emacs -Q with no grammars installed, opening .py, .rs, .json, and .toml under that pair opened python-ts-mode and rust-ts-mode without parsers, whereas json-ts-mode raised File mode specification error: (error "Tree-sitter for JSON isn’t available").
If you supply grammars through NixOS treesit-grammars.with-grammars and set 'never, bundle all matching grammars or list only installed modes in treesit-enabled-modes.
Third, markdown-ts-mode and external *-ts-mode packages sit outside treesit-major-mode-remap-alist.
Emacs 31.1 ships markdown-ts-mode, yet has no built-in non-Tree-sitter Markdown mode, so treesit-major-mode-remap-alist omits it and .md maps to nil in the default auto-mode-alist.
Add one line for Markdown, and keep individual activation config for external modes such as leaner-ts-mode.
(add-to-list 'auto-mode-alist '("\\.md\\'" . markdown-ts-mode))
Closing
In Emacs 31.1, setting treesit-enabled-modes with setopt covers 26 mode remaps, pinned grammar recipes, and on-demand compilation with built-in options alone.
Use ask or always on a regular desktop, and pair never with an explicit mode list on NixOS or other prebuilt setups.
For option definitions and the full change list, see Emacs 31.1 etc/NEWS and Parsing Program Source on emacs-doc.