Skip to main content

Treesitter

FtVim uses nvim-treesitter for advanced syntax highlighting, code navigation, and text objects.

What is Treesitter?

Treesitter is a parsing system that builds a syntax tree of your code. Unlike traditional regex-based highlighting, Treesitter:

  • Provides accurate syntax highlighting that understands code structure
  • Enables smart text objects (select function, class, parameter, etc.)
  • Supports intelligent indentation
  • Powers code folding based on syntax
  • Works across all supported languages consistently

Default Parsers

FtVim comes with these parsers pre-installed:

ParserLanguage
bashBash/Shell scripts
cC
luaLua
markdownMarkdown
pythonPython
vimVimscript
vimdocVim help files

Installing Parsers

Automatic Installation

When you open a file, Treesitter will offer to install the parser if it's not already installed.

Manual Installation

Install a specific parser:

:TSInstall javascript

Install multiple parsers:

:TSInstall typescript tsx json html css

Via Configuration

Add parsers to ensure_installed:

-- In your Neovim config (e.g., ~/.config/nvim/lua/plugins/treesitter.lua)
return {
{
"nvim-treesitter/nvim-treesitter",
opts = {
ensure_installed = {
-- Add your languages here
"javascript",
"typescript",
"tsx",
"html",
"css",
"json",
"yaml",
"rust",
"go",
},
},
},
}

Updating Parsers

Update all installed parsers:

:TSUpdate

Update a specific parser:

:TSUpdate python

Sync update (blocking):

:TSUpdateSync

Treesitter Commands

CommandDescription
:TSInstall <lang>Install a parser
:TSUpdateUpdate all parsers
:TSUninstall <lang>Remove a parser
:TSInstallInfoShow installed parsers
:TSModuleInfoShow module information

Text Objects

FtVim integrates Treesitter with mini.ai to provide smart text objects:

Text ObjectDescription
af / ifAround/inside function
ac / icAround/inside class
aa / iaAround/inside argument/parameter
ao / ioAround/inside block/scope

Usage Examples

  • daf - Delete a function
  • vif - Select inside function
  • caa - Change around argument
  • yic - Yank inside class

These work in any language with Treesitter support!

Treesitter Features

Syntax Highlighting

Enabled by default. Provides accurate, context-aware highlighting:

opts = {
highlight = { enable = true },
}

Smart Indentation

Automatic indentation based on syntax:

opts = {
indent = { enable = true },
}

Incremental Selection

Expand/shrink selection based on syntax nodes:

opts = {
incremental_selection = {
enable = true,
keymaps = {
init_selection = "<C-space>",
node_incremental = "<C-space>",
scope_incremental = false,
node_decremental = "<bs>",
},
},
}

Troubleshooting

Check Parser Status

:TSInstallInfo

Shows all available parsers and their installation status.

Check Health

:checkhealth nvim-treesitter

Diagnoses common Treesitter issues.

Common Issues

Highlighting not working:

  1. Ensure the parser is installed (:TSInstallInfo)
  2. Check that highlight is enabled (:TSModuleInfo)
  3. Try reinstalling the parser (:TSInstall <lang>)

Parser installation fails:

  1. Ensure you have a C compiler installed (gcc, clang)
  2. On Windows, ensure MSVC or MinGW is available
  3. Check network connectivity for downloading parsers

Slow performance on large files:

  1. Treesitter can be slow on very large files
  2. Consider disabling for large files:
opts = {
highlight = {
enable = true,
disable = function(lang, buf)
local max_filesize = 100 * 1024 -- 100 KB
local ok, stats = pcall(vim.loop.fs_stat, vim.api.nvim_buf_get_name(buf))
if ok and stats and stats.size > max_filesize then
return true
end
end,
},
}

Wrong highlighting:

  1. Update the parser (:TSUpdate <lang>)
  2. The parser may have bugs - check nvim-treesitter issues
  3. Some languages have multiple parsers (e.g., typescript vs tsx)

Here's a list of commonly used parsers:

CategoryParsers
Webhtml, css, javascript, typescript, tsx, json, yaml
Systemsc, cpp, rust, go, zig
Scriptingpython, lua, bash, ruby, perl
JVMjava, kotlin, scala
Functionalhaskell, ocaml, elixir
Datajson, yaml, toml, xml
Docsmarkdown, vimdoc, comment
Configdockerfile, make, cmake, nginx
Gitdiff, git_config, gitcommit, gitignore

Advanced Configuration

Custom Highlights

Override specific highlight groups:

vim.api.nvim_set_hl(0, "@function", { fg = "#7aa2f7" })
vim.api.nvim_set_hl(0, "@variable", { fg = "#c0caf5" })

Language-Specific Settings

Configure parsers individually:

return {
{
"nvim-treesitter/nvim-treesitter",
opts = {
highlight = {
enable = true,
-- Disable for specific languages
disable = { "latex" },
},
indent = {
enable = true,
-- Disable indent for these (sometimes buggy)
disable = { "python", "yaml" },
},
},
},
}