2023-06-12 09:54:30 -06:00
< h1 align = "center" >
< img src = "https://github.com/nvim-treesitter/nvim-treesitter/assets/2361214/0513b223-c902-4f12-92ee-8ac4d8d6f41f" alt = "nvim-treesitter" >
< / h1 >
2020-11-21 22:46:24 +01:00
2023-06-12 09:54:30 -06:00
The `nvim-treesitter` plugin provides
1. functions for installing, updating, and removing [**tree-sitter parsers** ](SUPPORTED_LANGUAGES.md );
2025-05-18 10:37:50 +02:00
2. a collection of **queries** for enabling tree-sitter features built into Neovim for these languages;
3. a staging ground for [treesitter-based features ](#Supported-features ) considered for upstreaming to Neovim.
2020-06-21 17:39:58 +02:00
2025-05-05 11:00:16 +02:00
For details on these and how to help improving them, see [CONTRIBUTING.md ](./CONTRIBUTING.md ).
2025-05-18 10:37:50 +02:00
>[!CAUTION]
2026-03-31 11:14:37 +02:00
> This is a full, incompatible, rewrite: Treat this as a different plugin you need to set up from scratch following the instructions below. If you can't or don't want to update, specify the [`master` branch](https://github.com/nvim-treesitter/nvim-treesitter/blob/master/README.md) (which is locked but will remain available for backward compatibility with Nvim 0.11).
2025-05-18 10:37:50 +02:00
2020-04-22 17:17:21 +02:00
# Quickstart
## Requirements
2020-07-27 09:23:42 -05:00
2026-03-14 19:39:31 +01:00
- Neovim 0.12.0 or later (nightly)
2025-04-27 14:00:40 +02:00
- `tar` and `curl` in your path
2026-03-14 19:39:31 +01:00
- [`tree-sitter-cli` ](https://github.com/tree-sitter/tree-sitter/blob/master/crates/cli/README.md ) (0.26.1 or later, installed via your package manager, **not npm** )
2024-04-22 19:56:30 +02:00
- a C compiler in your path (see < https: // docs . rs / cc / latest / cc / #compile -time-requirements > )
2020-04-22 17:17:21 +02:00
2025-05-18 10:37:50 +02:00
>[!IMPORTANT]
2026-03-14 19:39:31 +01:00
> The current **support policy** for Neovim is
2026-03-31 11:14:37 +02:00
> * the _latest_ [stable release](https://github.com/neovim/neovim/releases/tag/stable),
2026-03-14 19:39:31 +01:00
> * the _latest_ [nightly prerelease](https://github.com/neovim/neovim/releases/tag/nightly).
2026-03-31 11:14:37 +02:00
> Other versions may work but are neither tested nor considered for fixes.
2025-05-18 10:37:50 +02:00
2020-04-22 17:17:21 +02:00
## Installation
2020-11-21 22:07:17 +01:00
You can install `nvim-treesitter` with your favorite package manager (or using the native `package` feature of vim, see `:h packages` ).
2020-04-22 17:17:21 +02:00
2025-05-05 11:00:16 +02:00
This plugin is only guaranteed to work with specific versions of language parsers** (as specified in the `parser.lua` table). **When upgrading the plugin, you must make sure that all installed parsers are updated to the latest version** via `:TSUpdate` .
2026-01-04 18:04:18 +01:00
It is strongly recommended to automate this; e.g., using the following spec with [lazy.nvim ](https://github.com/folke/lazy.nvim ):
2021-06-30 17:04:24 +05:30
2023-06-12 09:54:30 -06:00
```lua
2026-01-04 18:04:18 +01:00
{
2025-05-18 10:37:50 +02:00
'nvim-treesitter/nvim-treesitter',
lazy = false,
build = ':TSUpdate'
2026-01-05 14:15:36 +04:00
}
2020-11-21 20:32:24 +01:00
```
2023-02-15 14:56:45 +00:00
2025-05-05 11:00:16 +02:00
>[!IMPORTANT]
> This plugin does not support lazy-loading.
2020-04-22 17:17:21 +02:00
2023-06-12 09:54:30 -06:00
## Setup
2020-07-27 09:23:42 -05:00
2025-12-14 13:11:45 +01:00
`nvim-treesitter` can be configured by calling `setup` . **You do not need to call `setup` for `nvim-treesitter` to work using default values.**
2020-04-28 11:56:00 +02:00
2022-02-09 21:54:36 +01:00
```lua
2026-03-14 19:39:31 +01:00
require('nvim-treesitter').setup {
2025-12-14 13:11:45 +01:00
-- Directory to install parsers and queries to (prepended to `runtimepath` to have priority)
2023-06-12 09:54:30 -06:00
install_dir = vim.fn.stdpath('data') .. '/site'
}
2020-11-21 22:07:17 +01:00
```
2025-04-27 13:02:32 +02:00
Parsers and queries can then be installed with
```lua
2026-03-14 19:39:31 +01:00
require('nvim-treesitter').install { 'rust', 'javascript', 'zig' }
2025-04-27 13:02:32 +02:00
```
2025-05-16 15:44:26 +01:00
(This is a no-op if the parsers are already installed.) Note that this function runs asynchronously; for synchronous installation in a script context ("bootstrapping"), you need to `wait()` for it to finish:
2025-05-18 10:37:50 +02:00
2025-05-05 11:00:16 +02:00
```lua
2025-05-16 15:44:26 +01:00
require('nvim-treesitter').install({ 'rust', 'javascript', 'zig' }):wait(300000) -- wait max. 5 minutes
2025-05-05 11:00:16 +02:00
```
2025-04-27 13:02:32 +02:00
2020-11-21 22:46:24 +01:00
Check [`:h nvim-treesitter-commands` ](doc/nvim-treesitter.txt ) for a list of all available commands.
2020-11-21 22:07:17 +01:00
2020-11-21 22:46:24 +01:00
# Supported languages
2020-11-21 22:07:17 +01:00
2020-11-21 22:46:24 +01:00
For `nvim-treesitter` to support a specific feature for a specific language requires both a parser for that language and an appropriate language-specific query file for that feature.
2020-11-21 22:07:17 +01:00
2025-05-05 11:00:16 +02:00
A list of the currently supported languages can be found [on this page ](SUPPORTED_LANGUAGES.md ). If you wish to add a new language or improve the queries for an existing one, please see our [contributing guide ](CONTRIBUTING.md ).
2020-11-21 22:07:17 +01:00
2023-06-12 09:54:30 -06:00
# Supported features
2020-08-09 11:39:51 -05:00
2023-06-12 09:54:30 -06:00
`nvim-treesitter` provides queries for the following features. **These are not automatically enabled.**
2020-08-09 11:39:51 -05:00
2023-06-12 09:54:30 -06:00
## Highlighting
2020-08-09 11:39:51 -05:00
2023-06-12 09:54:30 -06:00
Treesitter highlighting is provided by Neovim, see `:h treesitter-highlight` . To enable it for a filetype, put `vim.treesitter.start()` in a `ftplugin/<filetype>.lua` in your config directory, or place the following in your `init.lua` :
2020-10-19 21:32:54 +02:00
2022-02-09 21:54:36 +01:00
```lua
2023-06-12 09:54:30 -06:00
vim.api.nvim_create_autocmd('FileType', {
pattern = { '< filetype > ' },
callback = function() vim.treesitter.start() end,
})
2020-10-19 21:32:54 +02:00
```
2023-06-12 09:54:30 -06:00
## Folds
2020-11-21 22:46:24 +01:00
2023-06-12 09:54:30 -06:00
Treesitter-based folding is provided by Neovim. To enable it, put the following in your `ftplugin` or `FileType` autocommand:
2020-08-09 11:39:51 -05:00
2024-07-17 18:03:09 +02:00
```lua
2025-12-14 13:11:45 +01:00
vim.wo[0][0].foldexpr = 'v:lua.vim.treesitter.foldexpr()'
vim.wo[0][0].foldmethod = 'expr'
2020-08-09 11:39:51 -05:00
```
2023-06-12 09:54:30 -06:00
## Indentation
2020-09-07 11:41:37 -05:00
2023-06-12 09:54:30 -06:00
Treesitter-based indentation is provided by this plugin but considered **experimental** . To enable it, put the following in your `ftplugin` or `FileType` autocommand:
2022-06-20 21:50:07 +01:00
2023-02-15 14:56:45 +00:00
```lua
2023-06-12 09:54:30 -06:00
vim.bo.indentexpr = "v:lua.require'nvim-treesitter'.indentexpr()"
2022-06-20 21:50:07 +01:00
```
2023-06-12 09:54:30 -06:00
(Note the specific quotes used.)
2022-06-20 21:50:07 +01:00
2023-06-12 09:54:30 -06:00
## Injections
2022-06-20 21:50:07 +01:00
2023-06-12 09:54:30 -06:00
Injections are used for multi-language documents, see `:h treesitter-language-injections` . No setup is needed.
2023-02-15 14:56:45 +00:00
2023-05-20 17:29:03 +02:00
## Locals
2025-05-04 10:51:09 +02:00
These queries can be used to look up definitions and references to identifiers in a given scope. They are not used in this plugin and are provided for (limited) backward compatibility.
2023-05-20 17:29:03 +02:00
2023-06-12 09:54:30 -06:00
# Advanced setup
2022-06-20 21:50:07 +01:00
2025-05-29 18:23:42 +02:00
## Adding custom languages
2020-11-21 21:00:38 +01:00
2020-11-21 22:46:24 +01:00
If you have a parser that is not on the list of supported languages (either as a repository on Github or in a local directory), you can add it manually for use by `nvim-treesitter` as follows:
2020-11-21 21:00:38 +01:00
2024-04-14 16:25:28 +02:00
1. Add the following snippet in a `User TSUpdate` autocommand:
2020-11-21 21:00:38 +01:00
2022-02-09 21:54:36 +01:00
```lua
2024-04-14 16:25:28 +02:00
vim.api.nvim_create_autocmd('User', { pattern = 'TSUpdate',
callback = function()
require('nvim-treesitter.parsers').zimbu = {
install_info = {
url = 'https://github.com/zimbulang/tree-sitter-zimbu',
revision = < sha > , -- commit hash for revision to check out; HEAD if missing
-- optional entries:
branch = 'develop', -- only needed if different from default branch
location = 'parser', -- only needed if the parser is in subdirectory of a "monorepo"
2025-05-05 11:00:16 +02:00
generate = true, -- only needed if repo does not contain pre-generated `src/parser.c`
2025-12-14 13:11:45 +01:00
generate_from_json = false, -- only needed if repo does not contain `src/grammar.json` either
2025-05-29 18:23:42 +02:00
queries = 'queries/neovim', -- also install queries from given directory
2024-04-14 16:25:28 +02:00
},
}
end})
2020-11-21 21:00:38 +01:00
```
2024-04-14 16:25:28 +02:00
Alternatively, if you have a local checkout, you can instead use
2025-05-18 10:37:50 +02:00
2024-04-14 16:25:28 +02:00
```lua
install_info = {
path = '~/parsers/tree-sitter-zimbu',
-- optional entries
2025-05-05 11:00:16 +02:00
location = 'parser',
generate = true,
generate_from_json = false,
2025-05-29 18:23:42 +02:00
queries = 'queries/neovim', -- symlink queries from given directory
2024-04-14 16:25:28 +02:00
},
```
This will always use the state of the directory as-is (i.e., `branch` and `revision` will be ignored).
2. If the parser name differs from the filetype(s) used by Neovim, you need to register the parser via
2022-02-06 19:10:46 +01:00
2022-02-09 21:54:36 +01:00
```lua
2023-06-12 09:54:30 -06:00
vim.treesitter.language.register('zimbu', { 'zu' })
2022-02-06 13:34:08 +01:00
```
2023-06-12 09:54:30 -06:00
If Neovim does not detect your language's filetype by default, you can use [Neovim's `vim.filetype.add()` ](<https://neovim.io/doc/user/lua.html#vim.filetype.add( )>) to add a custom detection rule.
2023-02-24 09:49:16 +00:00
2024-04-14 16:25:28 +02:00
3. Start `nvim` and `:TSInstall zimbu` .
2020-11-21 21:00:38 +01:00
2025-05-05 11:00:16 +02:00
>[!IMPORTANT]
2025-12-14 13:11:45 +01:00
> If the parser requires an external scanner, this must be written in C.
2021-08-08 05:23:11 +06:00
2024-04-14 16:25:28 +02:00
### Modifying parsers
You can use the same approach for overriding parser information. E.g., if you always want to generate the `lua` parser from grammar, add
2025-05-18 10:37:50 +02:00
2024-04-14 16:25:28 +02:00
```lua
vim.api.nvim_create_autocmd('User', { pattern = 'TSUpdate',
callback = function()
require('nvim-treesitter.parsers').lua.install_info.generate = true
end})
```
2020-11-21 22:46:24 +01:00
## Adding queries
2020-11-21 22:07:17 +01:00
2025-05-05 11:00:16 +02:00
Queries can be placed anywhere in your `runtimepath` under `queries/<language>` , with earlier directories taking precedence unless the queries are marked with `; extends` ; see [`:h treesitter-query-modelines` ](https://neovim.io/doc/user/treesitter.html#treesitter-query-modeline ).