Search code examples
vimcolor-schemevim-syntax-highlighting

Is there a way in vim to change search text highlight color in visual mode only?


(I am using the nord-vim color scheme, but this is an issue with all color schemes in vim that I've tried, and I am looking for what settings need to be changed to accomplish the desired goal)

I have a problem in vim where if I do a search the highlight text color for the search matches is the same as the visual mode background color, so if I search for some text and then visually select some lines of text containing one or more search results, the text disappears. It does so because visual mode is changing the background color but not the text color, which is the desired behavior, except in the one case of search results, which I would like to change the text color of but only in visual mode.

Is this possible in vim?

EXAMPLE:

(do a search for some text, and it is highlighted) enter image description here

(visually select some lines of text containing search results) enter image description here

I would like it if the search results changed text color to some other distinguishable color, to indicate that they are search results, but only in visual mode.


Solution

  • It is sadly not documented but it happens so that some highlight groups, like Visual, have some sort of priority over others, like Search:

    hi Visual cterm=NONE ctermbg=cyan   ctermfg=black
    hi Search cterm=NONE ctermbg=yellow ctermfg=black
    

    1

    AFAIK, the only way to explicitly increase the priority of a highlight group is to set its cterm/gui attribute to reverse and swap the *fg and *bg attributes:

    hi Visual cterm=NONE    ctermbg=cyan  ctermfg=black
    hi Search cterm=reverse ctermbg=black ctermfg=yellow
    

    2

    But even then, you will notice that it's only the reverse bit that has some effect when interacting with Visual, with the color attributes of Search totally ignored, which may or may not be satisfactory.

    This gist explains how best to override highlight groups.