Skip to content

Tree-sitter

Tree-sitter is a parser — a program that reads source code and builds a syntax tree, a structured map in which every piece of the program is a labelled node: this is a function declaration, inside it a parameter, inside that a name. Classic editors highlight code with pattern matching, and patterns guess wrong — a word that is a keyword in one place is a variable name in another, and the guesser cannot tell. A tree knows. Neovim builds Tree-sitter in: hand it a parser for a language (one small compiled library per language) and the editor gains highlighting driven by the real structure, plus folding, indentation and navigation that all read the same tree.

What ships in the box

Neovim 0.12 bundles parsers for the languages its own machinery is made of. The lab's :checkhealth report (transcript neovim-lab/transcripts/t05-checkhealth.txt) lists them, each marked OK:

- ✅ OK Parser: c                         ABI: 15, path: .../lib/nvim/parser/c.so
- ✅ OK Parser: lua                       ABI: 15, path: .../lib/nvim/parser/lua.so
- ✅ OK Parser: markdown                  ABI: 15, path: .../lib/nvim/parser/markdown.so
- ✅ OK Parser: markdown_inline           ABI: 15, path: .../lib/nvim/parser/markdown_inline.so
- ✅ OK Parser: query                     ABI: 15, path: .../lib/nvim/parser/query.so

— with vim and vimdoc completing the set of seven. Everything else, from Python to Rust to the language you are probably thinking of, is installed separately, which is the second half of this chapter.

First, proof the bundled half works. The lab opened a small Lua file (transcript neovim-lab/transcripts/t06-treesitter.txt):

local function greet(name)
  local message = 'Hello, ' .. name .. '!'
  print(message)
  return message
end

greet('reader')

and asked Tree-sitter for the tree:

language: lua
root node type: chunk
top-level nodes: function_declaration, function_call
highlighter started: true
highlighter active: true

The root of every Lua file is a chunk; its children are exactly the two things the file does at top level — declares a function and calls one. Highlighting, when you open the file interactively, is painted from nodes like these by queries — small pattern files that say colour function_declaration's name as a function. The tree is the fact; the query is the opinion about how facts should look. That split is why Tree-sitter highlighting can be both accurate and themed.

Installing more parsers

Parsers are distributed as source and compiled on your machine, and the standard tool for fetching and building them is itself a plugin: nvim-treesitter. The lab cloned it and installed a Python parser. The journey had two failures in it, both kept in transcript neovim-lab/transcripts/t07-tsinstall.txt because both are the failures you are most likely to meet:

Failure one: the command is gone. Guides and forum posts written before 2025 tell you to run :TSInstallSync for a scripted install. The current nvim-treesitter — rewritten, like much of this ecosystem — has no such command:

Error in command line:
E492: Not an editor command: TSInstallSync python

The modern spellings are the :TSInstall command for interactive use, and for scripts the Lua form require('nvim-treesitter').install({ 'python' }), whose returned task a script can wait for. Chapter 11 has more to say about an ecosystem that rewrites itself; the practical habit is to trust the installed plugin's own :help over any article, this one included.

Failure two: the builder is another program. With the modern command in hand, the install downloaded the grammar and then stopped:

[nvim-treesitter/install/python]: Downloading tree-sitter-python...
[nvim-treesitter/install/python]: Compiling parser
[nvim-treesitter/install/python] error: Error during "tree-sitter build": vim/_core/system:324: ENOENT: no such file or directory (cmd): 'tree-sitter'

The rewritten nvim-treesitter compiles by calling the tree-sitter command line tool — a separate program from the parsers themselves. The lab fetched its official release binary, put it on the PATH, and ran the same install again:

[nvim-treesitter/install/python]: Downloading tree-sitter-python...
[nvim-treesitter/install/python]: Compiling parser
[nvim-treesitter/install/python]: Installing parser
[nvim-treesitter/install/python]: Language installed
installed parsers: python

A python.so now sits beside the bundled parsers in the data folder, and the same question the lab asked of Lua answers for Python — root node module, the Python grammar's name for a whole file. The general shape of the lesson: on Linux and macOS the requirements are a C compiler and, now, that CLI; both are one install each, and :checkhealth nvim-treesitter is where the plugin reports which of its requirements it cannot find on your machine.