Skip to main content
Treesitter provides superior syntax highlighting, indentation, and code understanding through incremental parsing.

Configuration

Treesitter is configured in lua/user/treesitter.lua.

Language Parsers

The configuration automatically installs and maintains parsers for:
ensure_installed = { "lua", "markdown", "markdown_inline", "bash", "python" }
You can add more languages to the ensure_installed list or use "all" to install all available parsers.

Features

Syntax Highlighting

Treesitter provides context-aware syntax highlighting that understands code structure:
highlight = {
  enable = true,
  disable = { "css" },  -- Disable for specific languages if needed
}

Smart Indentation

Automatic indentation based on code structure:
indent = { 
  enable = true, 
  disable = { "python", "css" } 
}
Python indentation is disabled because it uses significant whitespace, and CSS is disabled to avoid conflicts.

Auto-pairs Integration

Treesitter integrates with nvim-autopairs for context-aware bracket pairing:
autopairs = {
  enable = true,
}

Context Commentstring

Intelligent comment detection based on language context:
context_commentstring = {
  enable = true,
  enable_autocmd = false,
}
This enables proper commenting in files with mixed languages (like HTML with embedded JavaScript).

Installing Language Parsers

Manual Installation

Install a specific parser:
:TSInstall javascript
:TSInstall typescript
:TSInstall rust

Update Parsers

Update all installed parsers:
:TSUpdate

View Installed Parsers

:TSInstallInfo

Supported Languages

Treesitter supports over 100 languages. Common ones include:
  • JavaScript/TypeScript
  • Python
  • Rust
  • Go
  • C/C++
  • Java
  • Ruby
  • PHP
  • HTML/CSS
  • Markdown
  • JSON/YAML
  • And many more

Usage

Highlighting

Treesitter highlighting is automatic once parsers are installed. No additional configuration needed.

Folding

Treesitter can be used for code folding:
vim.opt.foldmethod = "expr"
vim.opt.foldexpr = "nvim_treesitter#foldexpr()"

Troubleshooting

Highlighting Issues

If syntax highlighting isn’t working:
  1. Check if the parser is installed: :TSInstallInfo
  2. Try reinstalling: :TSInstall! <language>
  3. Restart Neovim

Performance

For very large files, you may want to disable Treesitter:
:TSBufDisable highlight
Some language parsers may conflict with existing syntax plugins. Disable the old plugin if you experience issues.

Benefits

Treesitter provides accurate syntax highlighting even for complex nested structures, unlike regex-based highlighting.
  • Accurate: Understands code structure, not just patterns
  • Fast: Incremental parsing updates only changed parts
  • Extensible: Foundation for other features like refactoring
  • Consistent: Same highlighting across different file types

Build docs developers (and LLMs) love