Introduction
nvim-treesitter brings Tree-sitter parsing to Neovim, replacing regex-based syntax highlighting with incremental, grammar-based parsing. It provides a unified interface for installing language parsers and configuring modules like highlighting, indentation, and incremental selection across 200+ languages.
What nvim-treesitter Does
- Installs and manages Tree-sitter language parsers for 200+ languages
- Provides syntax highlighting based on real abstract syntax trees
- Enables incremental selection that expands to the next syntactic node
- Offers indentation computed from the parse tree rather than heuristics
- Supports code folding using Tree-sitter node boundaries
Architecture Overview
nvim-treesitter acts as a configuration and abstraction layer on top of Neovim's built-in Tree-sitter integration. It ships query files (highlights.scm, indents.scm, folds.scm) for each supported language, manages parser compilation and installation via :TSInstall, and exposes a module system where each feature (highlight, indent, incremental_selection) can be independently enabled or configured.
Self-Hosting & Configuration
- Install via any Neovim plugin manager (lazy.nvim, packer, vim-plug)
- Run :TSUpdate after installation to compile parsers
- Configure modules in your init.lua with require('nvim-treesitter.configs').setup({})
- Set ensure_installed to a list of languages or "all" for automatic parser installation
- Enable specific modules: highlight = { enable = true }, indent = { enable = true }
Key Features
- Grammar-based highlighting that handles nested languages correctly
- Incremental selection expanding from cursor to enclosing AST nodes
- Automatic parser installation and updates via :TSInstall / :TSUpdate
- Query-based text objects for selecting functions, classes, and parameters
- Playground module for inspecting the syntax tree interactively
Comparison with Similar Tools
- Regex-based syntax — Traditional Vim highlighting uses regex patterns that break on edge cases; nvim-treesitter uses real parsers for accuracy
- vim-polyglot — Bundles many filetype plugins but still uses regex; nvim-treesitter provides structural parsing
- Tree-sitter CLI — The standalone CLI parses files; nvim-treesitter integrates parsing directly into the editor
- LSP semantic tokens — Complements treesitter by adding type-aware highlighting from the language server
FAQ
Q: Does nvim-treesitter replace LSP? A: No. Tree-sitter handles syntax-level features (highlighting, folding, text objects) while LSP provides semantic features (go-to-definition, diagnostics, rename). They work together.
Q: How do I add a language parser? A: Run :TSInstall inside Neovim. The parser is compiled locally and cached.
Q: Does it slow down Neovim? A: Tree-sitter uses incremental parsing, so only changed portions of the buffer are re-parsed. Performance is comparable to or better than regex highlighting on large files.
Q: Can I write custom queries? A: Yes. Place custom query files in after/queries// in your Neovim config directory to extend or override default queries.