🌲 Treesitter for Neovim

Shaik Zahid
3 min readFeb 8, 2023

--

A better syntax highlighter for Neovim

Tree-sitter is a parser generator tool and an incremental parsing library. It can build a concrete syntax tree for a source file and efficiently update the syntax tree as the source file is edited. Tree-sitter aims to be:

  • General enough to parse any programming language
  • Fast enough to parse on every keystroke in a text editor
  • Robust enough to provide useful results even in the presence of syntax errors
  • Dependency-free so that the runtime library (which is written in pure C) can be embedded in any application

Requirements

  • Neovim ≥ 0.8

Installation

  • Reference github repository in plugins.lua
return {

-- Alpha (Dashboard)
{
"goolord/alpha-nvim",
lazy = true,
},

-- Bufferline
{
'akinsho/bufferline.nvim',
dependencies = {
'nvim-tree/nvim-web-devicons'
},
},

-- Colorscheme
{
'folke/tokyonight.nvim',
},

-- Hop (Better Navigation)
{
"phaazon/hop.nvim",
lazy = true,
},


-- Lualine
{
'nvim-lualine/lualine.nvim',
dependencies = {
'nvim-tree/nvim-web-devicons'
},
},

-- Nvimtree (File Explorer)
{
'nvim-tree/nvim-tree.lua',
lazy = true,
dependencies = {
'nvim-tree/nvim-web-devicons',
},
},

-- Telescope (Fuzzy Finder)
{
'nvim-telescope/telescope.nvim',
lazy = true,
dependencies = {
{'nvim-lua/plenary.nvim'},
}
},


-- Treesitter
-- Added this plugin to our initial config
{
"nvim-treesitter/nvim-treesitter",
},

-- Which-key
{
'folke/which-key.nvim',
lazy = true,
},

}
  • Summon Lazy(Package Manager) using Space + p command in Normal mode to install Treesitter. Use shift + i to install the plugin.

Configuration

  • Open File Explorer using Space + e then Create a new file treesitter-config.lua using a in your lua directory and add this code.
-- treesitter-config.lua

local configs = require("nvim-treesitter.configs")
configs.setup {
-- Add a language of your choice
ensure_installed = {"cpp", "python", "lua", "java", "javascript", },
sync_install = false,
ignore_install = { "" }, -- List of parsers to ignore installing
highlight = {
enable = true, -- false will disable the whole extension
disable = { "" }, -- list of language that will be disabled
additional_vim_regex_highlighting = true,

},
indent = { enable = true, disable = { "yaml" } },
rainbow = {
enable = true,
-- disable = { "jsx", "cpp" }, list of languages you want to disable the plugin for
extended_mode = true, -- Also highlight non-bracket delimiters like html tags, boolean or table: lang -> boolean
max_file_lines = nil, -- Do not enable for files with more than n lines, int
-- colors = {}, -- table of hex strings
-- termcolors = {} -- table of colour name strings
}
}
  • Make sure to include the programming language of your choice in a string inside ensure_installed variable so that Treesitter can parse that language.
  • Update main configuration
-- init.lua

require "options"
require "keymaps"
require "lazy-config"
require "alpha-config"
require "bufferline-config"
require "hop-config"
require "nvim-tree-config"
require "lualine-config"
require "telescope-config"
require "treesitter-config" -- Added this line to initial file
require "whichkey"
  • Treesitter Language Parsers will be installed when Neovim is restarted.

Neovim From Scratch

  • If you want a complete installation and configuration of Neovim from Scratch, then you can head over to my NEOVIM SERIES.
  • This series is updated regularly, with updates and inclusion of newer plugins which improves the wholesome IDE experience of Neovim.

--

--