Search code examples
vimfull-text-searchvim-syntax-highlighting

Keeping syntax color foreground in vim search highlight


In Vim, is it possible to get the foreground text in a highlighted search match to keep the colors specified by my colorscheme? If possible, I'd like to customize just the background color of the match (hi Search ctermbg=BLAH) and keep the foreground colors the same as they were before the match. I suspect that this isn't possible with the built-in hlsearch, but I figured I'd check.


Solution

  • That's actually exactly how it works, if you clear the Search highlight group beforehand. Following the ideas outlined in this gist by romainl, you could do the following:

    augroup MyColors
        autocmd!
        autocmd ColorScheme * highlight clear Search
                          \ | highlight Search ctermbg={blah}
    augroup END
    colorscheme foo
    

    Note that in order for the autocmd to take effect you must set your colorscheme after your autocmd.