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


and use it for vim.highlight.on_yank{} function:

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 does 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 color scheme. 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: