Treesitter

From NixOS Wiki
Jump to: navigation, search

Tree sitter is a parsing system that can be installed as a Neovim plugin. It provides improved syntax highlighting than the default regex-based grammars built into Vim.

Installation

The easiest way to install Treesitter for Neovim is to use the Home Manager module.

 programs.neovim.plugins = [
   pkgs.vimPlugins.nvim-treesitter
 ];

More information on how to install Neovim plugins using Nix can be found here.

Grammar Packages

For each language, Treesitter needs a dedicated grammar package. On non-NixOS systems, it handles the installation by itself, however, because of the nature of Nix, it is recommended to install the grammar packages through Nix.

To install all grammar packages, you can use:

 programs.neovim.plugins = [
   pkgs.vimPlugins.nvim-treesitter.withAllGrammars
 ];

but you can also add specific grammar packages:

 programs.neovim.plugins = [
   (pkgs.vimPlugins.nvim-treesitter.withPlugins (p: [ p.c p.java ]))
 ];

Note that the upstream versions of tree-sitter grammars (available under tree-sitter.builtGrammars) are not necessarily compatible with neovim, as nvim-treesitter has its own lockfile to determine compatible grammars. These can be accessed with vimPlugins.nvim-treesitter.builtGrammars. :checkhealth can be used to ensure that grammars are working properly.

Installation without Nix

Installing grammar packages through the built-in command can lead to errors.

The following Neovim command will install syntax highlighting for the C programming language:

:TSInstall c

See also