This guide explains how Treesitter works in this Neovim config and how to customize it for your needs.
Treesitter is a parser generator tool that builds syntax trees for source code. Unlike traditional vim syntax highlighting that relies on complex regular expressions, treesitter creates an actual parse tree of your code. This provides:
- Superior syntax highlighting - More accurate and context-aware colors, especially for deeply nested or complex languages
- Intelligent text objects - Better understanding of code structure for navigation and selection
- Incremental parsing - Fast updates as you type with minimal performance impact
- Language-agnostic approach - Consistent experience across all supported programming languages
- Enhanced code folding - Structure-aware code folding based on syntax
- Better indentation - Context-aware automatic indentation
Think of it as having a deep understanding of your code's structure, enabling much smarter editing features.
This config comes pre-configured with treesitter parsers for common languages including:
- Bash/Shell scripts
- CSS/SCSS/SASS
- Dockerfile
- Git files (commits, configs, etc.)
- HTML
- JavaScript/TypeScript
- JSON/JSONC
- Lua - for Neovim configuration
- Markdown
- Python
- Ruby
- SQL
- YAML/TOML
- Vim script
:TSInstallInfo- See which parsers are installed:TSUpdate- Update all installed parsers:TSBufToggle highlight- Toggle treesitter highlighting for current buffer
If you haven't already, create a personal scaffold and add a spec in there with the languages that you want to add, as well as any other configuration
-- EXAMPLE
{
"nvim-treesitter/nvim-treesitter",
opts = {
ensure_installed = {
"cue",
},
textobjects = {
move = {
goto_next_start = { ["]m"] = "@function.outer" }
},
},
},
},You can also install manually
- Install a parser:
:TSInstall <language>- Example:
:TSInstall gofor Go language support
- Example:
- Check installation:
:TSInstallInfo - Update if needed:
:TSUpdate <language>
or add the parser to the ensure_installed property in the setup function in the treesitter plugin config
ensure_installed = {
-- add more here
"bash",
"ruby",
"lua",
"dockerfile",
...
},For a complete list of available parsers, see the official parser list.
Popular parsers include:
- Ruby:
ruby - Java:
java - React JSX:
tsx - Go:
go - C/C++:
c,cpp
Current settings live in lua/plugins/treesitter.lua
Treesitter provides intelligent text objects for selecting code structures:
textobjects = {
select = {
enable = true,
keymaps = {
["ac"] = { query = "@class.outer", desc = "Select around class" },
["ic"] = { query = "@class.inner", desc = "Select inside class" },
["af"] = { query = "@function.outer", desc = "Select around function" },
["if"] = { query = "@function.inner", desc = "Select inside function" },
["al"] = { query = "@loop.outer", desc = "Select around loop" },
["il"] = { query = "@loop.inner", desc = "Select inside loop" },
["ab"] = { query = "@block.outer", desc = "Select around block" },
["ib"] = { query = "@block.inner", desc = "Select inside block" },
},
selection_modes = {
["@class.outer"] = "V",
},
},
move = {
enable = true,
goto_next_start = { ["]f"] = "@function.outer", ["]c"] = "@class.outer" },
goto_next_end = { ["]F"] = "@function.outer", ["]C"] = "@class.outer" },
goto_previous_start = { ["[f"] = "@function.outer", ["[c"] = "@class.outer" },
goto_previous_end = { ["[F"] = "@function.outer", ["[C"] = "@class.outer" },
},
},Once configured, you can use treesitter text objects for precise selections:
vaf- Select entire function (including definition)vif- Select function body onlydac- Delete entire classyic- Copy class contents
]f- Jump to next function[f- Jump to previous function]c- Jump to next class[c- Jump to previous class
- Check internet connection
- Try updating treesitter:
:TSUpdate - Check system dependencies (C compiler required)
- View installation logs:
:messages
- Check parser status:
:TSInstallInfo - Disable for large files:
:TSBufDisable highlight - Reduce parser features in config:
opts = {
highlight = { enable = true },
indent = { enable = false }, -- Disable if causing issues
fold = { enable = false }, -- Disable if causing issues
}- Check file extension is correct
- Force filetype:
:set filetype=<language> - Verify parser exists:
:TSInstallInfo
- Treesitter documentation:
:help treesitter - nvim-treesitter plugin: GitHub repository
- Available parsers: Tree-sitter parser list
- Text objects:
:help nvim-treesitter-textobjects
| Action | Command | Description |
|---|---|---|
| Install parser | :TSInstall <lang> |
Install parser for language |
| Update parsers | :TSUpdate |
Update all installed parsers |
| Check status | :TSInstallInfo |
See installed parsers |
| Toggle highlighting | :TSBufToggle highlight |
Enable/disable for current buffer |
| Toggle all features | :TSBufToggle |
Show available toggle options |
| View logs | :messages |
Debug installation issues |
| Playground | :TSPlaygroundToggle |
Interactive syntax tree explorer |