How do I view the treesitter syntax tree for the current buffer in Neovim?
Answer
:InspectTree
Explanation
Opens an interactive split window showing the treesitter parse tree for the current buffer. Moving the cursor in either window highlights the corresponding node in the other, making it indispensable when writing treesitter queries, debugging syntax highlighting, or building plugins.
How it works
:InspectTree is a built-in Neovim command (0.9+) that requires a treesitter parser for the current filetype. It opens a read-only buffer displaying the full concrete syntax tree in S-expression format:
- Each node is shown with its type and source range (line/column)
- Named nodes appear in regular text; anonymous nodes (like punctuation) appear differently
- Moving your cursor in the tree highlights the matching text in the source window
- Moving your cursor in the source window highlights the matching tree node
Example
For a Python file with x = 1 + 2, :InspectTree shows:
(module ; [0, 0] - [1, 0]
(expression_statement ; [0, 0] - [0, 9]
(assignment ; [0, 0] - [0, 9]
left: (identifier) ; [0, 0] - [0, 1]
right: (binary_operator ; [0, 4] - [0, 9]
left: (integer) ; [0, 4] - [0, 5]
right: (integer))))) ; [0, 8] - [0, 9]
The node names (identifier, binary_operator, etc.) are exactly what you'd use in treesitter queries.
Tips
- Use
:Inspect(noTree) to show which treesitter captures and highlight groups apply under the cursor - Node names from
:InspectTreemap directly to query patterns:(binary_operator)in aqueries/python/highlights.scmfile - If the buffer shows
(ERROR ...)nodes, the parser encountered syntax it couldn't parse - Requires a parser to be installed for the filetype (via
:TSInstall langwith nvim-treesitter, or built-in parsers)