092152.md
「Minimal Tree-sitter Setup in Emacs 31」の 092152.md です。「そのまま開く」はファイルをそのまま返します。
# Minimal Tree-sitter Setup in Emacs 31
Date: 2026-12-05
Updated: 2026-12-05
Tags: emacs, tree-sitter, emacs-31, tutorial
URL: https://s.conao3.com/blog/2026/092152
Author: conao3 (Naoya Yamashita)
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](https://github.com/renzmann/treesit-auto).
I verified the minimal setup in GNU Emacs 31.1 and [lisp/treesit.el](https://github.com/emacs-mirror/emacs/blob/emacs-31.1/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](https://s.conao3.com/blog/2026/26ed0f); for the Elisp Tree-sitter API reference, see [Tree-sitter Major Modes](https://emacs-doc.conao3.com/elisp/Tree_002dsitter-Major-Modes) and [Language Grammar](https://emacs-doc.conao3.com/elisp/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.
```elisp
(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.
```elisp
(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.
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
<colgroup>
<col class="org-left" />
<col class="org-left" />
<col class="org-left" />
<col class="org-left" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">Feature</th>
<th scope="col" class="org-left">Emacs 29 and 30 built-in</th>
<th scope="col" class="org-left">With treesit-auto</th>
<th scope="col" class="org-left">Emacs 31.1 built-in</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left">Classic-to-TS mode remap</td>
<td class="org-left">Hand-written <code>major-mode-remap-alist</code></td>
<td class="org-left">Automatic</td>
<td class="org-left"><code>treesit-enabled-modes</code> populates 26 built-in pairs</td>
</tr>
<tr>
<td class="org-left">Grammar download recipes</td>
<td class="org-left">Hand-written <code>treesit-language-source-alist</code></td>
<td class="org-left">Bundled in package</td>
<td class="org-left">Bundled with pinned commits in each built-in <code>*-ts-mode</code></td>
</tr>
<tr>
<td class="org-left">Auto-installing missing grammars</td>
<td class="org-left">Manual <code>treesit-install-language-grammar</code></td>
<td class="org-left">Prompt or auto-install on file open</td>
<td class="org-left">Controlled by <code>treesit-auto-install-grammar</code> (default <code>ask</code>)</td>
</tr>
<tr>
<td class="org-left">Fallback when a grammar is missing</td>
<td class="org-left">Varies by mode</td>
<td class="org-left">Falls back to classic mode</td>
<td class="org-left">Keeps the remapped <code>*-ts-mode</code> (some modes signal an error)</td>
</tr>
</tbody>
</table>
# Choosing treesit-auto-install-grammar and install paths
`treesit-auto-install-grammar` accepts four values.
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
<colgroup>
<col class="org-left" />
<col class="org-left" />
<col class="org-left" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">Value</th>
<th scope="col" class="org-left">Behavior when opening a file with a missing grammar</th>
<th scope="col" class="org-left">Best fit</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left"><code>ask</code> (default)</td>
<td class="org-left">Prompts in the minibuffer and builds on <code>y</code></td>
<td class="org-left">Standard desktop setups installing languages on demand</td>
</tr>
<tr>
<td class="org-left"><code>always</code></td>
<td class="org-left">Clones with Git and compiles with no prompt</td>
<td class="org-left">Fast setup on a fresh machine</td>
</tr>
<tr>
<td class="org-left"><code>ask-dir</code></td>
<td class="org-left">Prompts for confirmation and an install directory</td>
<td class="org-left">Splitting grammar directories by project or environment</td>
</tr>
<tr>
<td class="org-left"><code>never</code></td>
<td class="org-left">Skips auto-install</td>
<td class="org-left">NixOS, Guix, or any setup providing prebuilt grammars</td>
</tr>
</tbody>
</table>
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`.
```elisp
(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](https://github.com/emacs-mirror/emacs/blob/emacs-31.1/etc/NEWS#L1079), 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](https://s.conao3.com/blog/2026/7a84c4).
```elisp
(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](https://github.com/emacs-mirror/emacs/blob/emacs-31.1/etc/NEWS#L1054) and [Parsing Program Source on emacs-doc](https://emacs-doc.conao3.com/elisp/Parsing-Program-Source).