Rainbow Parenthesis and Indentation in Neovim
4 min readFeb 21, 2023
Even though Treesitter provides syntax highlighting for our files. It does not provide highlighting for braces and indentation. These highlights comes in handy in larger code bases.
Requirements
- Neovim ≥ 0.5
- Indent Blankline
- Nvim-TS-Rainbow
- Rainbow Indentation
- Rainbow braces
Installation
- Add references of their repositories to the plugins.lua file and install them using
shift + i
key.
return {
-- Alpha (Dashboard)
{
"goolord/alpha-nvim",
lazy = true,
},
-- Auto Pairs
{
"windwp/nvim-autopairs"
},
-- Bufferline
{
'akinsho/bufferline.nvim',
dependencies = {
'nvim-tree/nvim-web-devicons'
},
},
-- Colorscheme
{
'folke/tokyonight.nvim',
},
-- Hop (Better Navigation)
{
"phaazon/hop.nvim",
lazy = true,
},
-- Added these two plugins
-- Indentation Highlighting
{
"lukas-reineke/indent-blankline.nvim",
},
-- Rainbow Highlighting
{
"HiPhish/nvim-ts-rainbow2",
},
-- Lualine
{
'nvim-lualine/lualine.nvim',
dependencies = {
'nvim-tree/nvim-web-devicons'
},
},
-- Language Support
{
'VonHeikemen/lsp-zero.nvim',
lazy = true,
branch = 'v1.x',
dependencies = {
-- LSP Support
{'neovim/nvim-lspconfig'}, -- Required
{'williamboman/mason.nvim'}, -- Optional
{'williamboman/mason-lspconfig.nvim'}, -- Optional
-- Autocompletion
{'hrsh7th/nvim-cmp'}, -- Required
{'hrsh7th/cmp-nvim-lsp'}, -- Required
{'hrsh7th/cmp-buffer'}, -- Optional
{'hrsh7th/cmp-path'}, -- Optional
{'saadparwaiz1/cmp_luasnip'}, -- Optional
{'hrsh7th/cmp-nvim-lua'}, -- Optional
-- Snippets
{'L3MON4D3/LuaSnip'}, -- Required
{'rafamadriz/friendly-snippets'}, -- Optional
}
},
-- Nvim-tree (File Explorer)
{
'nvim-tree/nvim-tree.lua',
lazy = true,
dependencies = {
'nvim-tree/nvim-web-devicons',
},
},
-- Nvim-Surround (Manipulating Surroundings)
{
"kylechui/nvim-surround",
config = function()
require("nvim-surround").setup({
-- Configuration here, or leave empty to use defaults
})
end
},
-- Telescope (Fuzzy Finder)
{
'nvim-telescope/telescope.nvim',
lazy = true,
dependencies = {
{'nvim-lua/plenary.nvim'},
}
},
-- Treesitter
{
"nvim-treesitter/nvim-treesitter",
},
-- Toggle Term
{
'akinsho/toggleterm.nvim',
config = true
},
-- Undo-Tree
{
"jiaoshijie/undotree",
dependencies = {
"nvim-lua/plenary.nvim",
},
},
-- Which-key
{
'folke/which-key.nvim',
lazy = true,
},
}
Configuration
1. Indentation Highlighting
- Create a new file named indentline-config.lua and add the following code.
-- indentline-config.lua
local status_ok, indent_blankline = pcall(require, "indent_blankline")
if not status_ok then
return
end
vim.g.indent_blankline_buftype_exclude = { "terminal", "nofile" }
vim.g.indent_blankline_filetype_exclude = {
"help",
"startify",
"dashboard",
"packer",
"neogitstatus",
"NvimTree",
"Trouble",
}
vim.g.indentLine_enabled = 1
-- vim.g.indent_blankline_char = "│"
vim.g.indent_blankline_char = "▏"
-- vim.g.indent_blankline_char = "▎"
vim.g.indent_blankline_show_trailing_blankline_indent = false
vim.g.indent_blankline_show_first_indent_level = true
vim.g.indent_blankline_use_treesitter = true
vim.g.indent_blankline_show_current_context = true
vim.g.indent_blankline_context_patterns = {
"class",
"return",
"function",
"method",
"^if",
"^while",
"jsx_element",
"^for",
"^object",
"^table",
"block",
"arguments",
"if_statement",
"else_clause",
"jsx_element",
"jsx_self_closing_element",
"try_statement",
"catch_clause",
"import_statement",
"operation_type",
}
-- HACK: work-around for https://github.com/lukas-reineke/indent-blankline.nvim/issues/59
vim.wo.colorcolumn = "99999"
vim.cmd [[highlight IndentBlanklineIndent1 guifg=#E06C75 gui=nocombine]]
vim.cmd [[highlight IndentBlanklineIndent2 guifg=#E5C07B gui=nocombine]]
vim.cmd [[highlight IndentBlanklineIndent3 guifg=#98C379 gui=nocombine]]
vim.cmd [[highlight IndentBlanklineIndent4 guifg=#56B6C2 gui=nocombine]]
vim.cmd [[highlight IndentBlanklineIndent5 guifg=#61AFEF gui=nocombine]]
vim.cmd [[highlight IndentBlanklineIndent6 guifg=#C678DD gui=nocombine]]
indent_blankline.setup({
-- show_end_of_line = true,
-- space_char_blankline = " ",
-- show_current_context = true,
-- show_current_context_start = true,
char_highlight_list = {
"IndentBlanklineIndent1",
"IndentBlanklineIndent2",
"IndentBlanklineIndent3",
"IndentBlanklineIndent4",
"IndentBlanklineIndent5",
"IndentBlanklineIndent6",
},
})
2. Braces Highlighting
- In the case of Braces highlighting, We don’t need to create any new file, just the updation of treesitter-config.lua file is required.
- Add this code to treesitter-config.lua file
rainbow = {
enable = true,
-- list of languages you want to disable the plugin for
-- disable = { "jsx", "cpp" },
-- Which query to use for finding delimiters
query = 'rainbow-parens',
-- Highlight the entire buffer all at once
strategy = require 'ts-rainbow.strategy.global',
-- Do not enable for files with more than n lines
max_file_lines = 3000
}
- The updated treesitter-config.lua file looks like this…
-- 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,
-- list of languages you want to disable the plugin for
-- disable = { "jsx", "cpp" },
-- Which query to use for finding delimiters
query = 'rainbow-parens',
-- Highlight the entire buffer all at once
strategy = require 'ts-rainbow.strategy.global',
-- Do not enable for files with more than n lines
max_file_lines = 3000
}
}
Main Configuration
- Source indentline-config.lua file to main configuration init.lua file.
-- init.lua
require "options"
require "keymaps"
require "lazy-config"
require "alpha-config"
require "autopairs-config"
require "bufferline-config"
require "hop-config"
require "indentline-config"
require "lualine-config"
require "lsp-config"
require "nvim-tree-config"
require "telescope-config"
require "toggleterm-config"
require "treesitter-config"
require "undotree-config"
require "whichkey"
- Restart Neovim to observe the differences in indentation and brace highlighting.
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.