Skip to main content
  1. Posts/

Highlight Groups Cleared after Starting Neovim

··201 words·1 min·

I want to use a custom highlight group for yank highlighting. Initially, I use the predefined highlight group IncSearch. I thought it a bit dim and want something brighter. So I defined the following highlight group:

highlight YankColor ctermfg=59 ctermbg=41 guifg=#34495E guibg=#2ECC71

Then, I use it in function vim.highlight.on_yank{}:

augroup highlight_yank
 autocmd!
 au TextYankPost * silent! lua vim.highlight.on_yank{higroup="YankColor", timeout=700}
augroup END

However, when I started Neovim, the custom yank highlight did not work. The output of :hi YankColor shows:

YankColor      xxx cleared

After digging up a bit, I find that it is because the colorscheme I use (a variant of gruvbox) will use :hi clear command to clear all custom highlight and reset highlighting to the defaults (see here for example).

So my custom highlight does not work anymore. A solution is to use a special event ColorScheme, which is triggered whenever we change the colorscheme. After changing the color scheme, we redefine the custom highlight group:

augroup custom_highlight
  autocmd!
  au ColorScheme * highlight YankColor ctermfg=59 ctermbg=41 guifg=#34495E guibg=#2ECC71
augroup END

After using this autocmd, everything works as expected.

Ref:

Related

Migrating from Packer.nvim to Lazy.nvim
··651 words·4 mins
Pylsp setup for Neovim in 2023
··1020 words·5 mins
Work with JSON File in Neovim
·299 words·2 mins