Treesitter
For decades, Vim's syntax highlighting relied on regular expressions -- clever pattern matching that could approximate your code's structure but never truly understand it. Treesitter changes the game by building a real parse tree of your source code, giving Neovim a deep, structural understanding of what you've written. The result is more accurate highlighting, smarter indentation, and powerful text objects that operate on actual language constructs like functions, classes, and arguments.
Treesitter is a parsing library that builds a concrete syntax tree of your code. In Neovim, it powers better syntax highlighting, code navigation, and text objects -- all language-aware.
What you'll learn in this chapter:
Understand how Treesitter differs from regex-based syntax highlighting
Install and manage Treesitter parsers for your languages
Use language-aware text objects to select, change, and delete functions, classes, and parameters
Navigate code structurally with Treesitter motions
Inspect the syntax tree to debug queries and understand code structure
Why Treesitter?
Traditional syntax highlighting in Vim is based on regular expressions. This is fast but imprecise — it can't truly "understand" code structure. Treesitter builds a parse tree, giving Neovim actual understanding of your code.
Syntax highlighting
Pattern matching
Structure-aware
Indentation
Heuristic
Accurate
Text objects
Generic (iw, ip)
Language-aware (if = inner function)
Folding
Manual / indent-based
AST-based
Performance
Fast
Fast (incremental parsing)
Setup
The following plugin spec uses Lua (see Chapter 11: Neovim with Lua for Lua configuration basics):
Managing Parsers
Treesitter Text Objects
This is where Treesitter truly shines. With nvim-treesitter-textobjects, you get language-aware text objects:
Example Workflows
With these text objects, you can:
daf— Delete an entire functioncif— Change the function bodyvac— Select an entire classyia— Yank a function parameter]f— Jump to the next function[c— Jump to the previous class<leader>sa— Swap a parameter with the next one
Tip: These text objects work across all languages that have Treesitter parsers. Write Python, Go, Rust, JavaScript -- the same keybindings work everywhere.
Incremental Selection
Treesitter can expand your selection based on the syntax tree:
Press Ctrl+Space to start selection on the current node
Press Ctrl+Space again to expand to the parent node
Press Backspace to shrink selection back
This is much smarter than viw → vis → vip because it follows the actual code structure.
Treesitter Playground
To see the syntax tree for your code, use the built-in inspector:
This opens a split window showing the AST (Abstract Syntax Tree) of your code. As you move your cursor, the corresponding tree node is highlighted. This is invaluable for:
Understanding how Treesitter sees your code
Debugging text object queries
Learning about AST structures
Note: Treesitter handles syntax-level understanding (highlighting, text objects, indentation), while LSP (see Chapter 13: LSP) handles project-level intelligence (go-to-definition, diagnostics, refactoring). They are complementary features and work best when both are enabled.
Summary
Treesitter gives Neovim a structural understanding of your code by building a real parse tree rather than relying on regex patterns. This enables accurate syntax highlighting, smart indentation, and powerful language-aware text objects that let you operate on functions, classes, parameters, and other constructs across any supported language.
Key takeaways:
Treesitter replaces regex-based syntax highlighting with structure-aware parsing, resulting in more accurate and consistent highlighting.
The
nvim-treesitter-textobjectsplugin unlocks text objects likeaf(a function),ic(inner class), andia(inner argument) that work uniformly across all languages with Treesitter parsers.Incremental selection (
Ctrl+Space/Backspace) expands and shrinks selections based on the actual syntax tree, not just word or paragraph boundaries.:InspectTreeis an essential debugging tool for understanding how Treesitter parses your code and for writing custom queries.
Exercises
Install Treesitter parsers -- Add the Treesitter plugin to your configuration and install parsers for at least three languages you use. Verify they are installed by running
:TSInstallInfo.Use Treesitter text objects -- Open a source file that contains functions and classes. Practice using
daf(delete a function),cif(change inner function),vac(select a class), andyia(yank an inner argument). Observe how they operate on actual language constructs.Navigate with Treesitter motions -- Configure the
movemodule ofnvim-treesitter-textobjects. Use]fto jump to the next function and[cto jump to the previous class in a source file.Explore the syntax tree -- Open a source file and run
:InspectTree. Move your cursor through the code and observe how the AST nodes are highlighted. Identify the node types for a function definition, a parameter, and a string literal.Swap function parameters -- Configure the
swapmodule and use<leader>sato swap a function parameter with the next one. Test it on a function call with multiple arguments.
Last updated
Was this helpful?