0

When I search in Vim (in the terminal), I sometimes find it really hard to see the matches. I'm trying to configure vim to color the matches in a way that stands out more. I added the row below to $HOME/.vimrc:

hi Search term=none ctermbg=red ctermfg=white guifg=green guibg=Purple

Although the configuration I added in .vimrc appears when I run :hi Search in vim, the search results coloring remains as before (reverse coloring on the first letter in the match). What am I doing wrong? Why is my configuration being ignored, and how can I fix it?

:hi Search returns the following line:

Search         xxx ctermfg=15 ctermbg=9 guifg=green guibg=Purple

I'm running vim 9.1:

VIM - Vi IMproved 9.1 (2024 Jan 02, compiled Jan 16 2025 20:13:18)
Included patches: 1-16, 647, 678, 697
On ubuntu 24 in foot.

Stackexchange suggested the question below which is similar to mine and now I'm wondering if the same goes for the Search highlight: Why is `:highlight Cursor ...` ignored?

2
  • 2
    Read :help colorscheme-override or search for it on this site. If you run your :hi command as an :autocmd, it will work.
    – Friedrich
    Commented Feb 13 at 14:49
  • I discovered that adding set hlsearch in ~/.vimrc solves my problem since it makes all the search results stand out, but I can't configure what colors they have. Commented Feb 13 at 14:50

1 Answer 1

2

Looks like I misread the question.

The command in your vimrc is perfectly executed, as :hi Search shows. Your red is translated internally as 9 and your white is translated as 15, so there is nothing to see, here.

But, if you really only have the following two lines in your vimrc:

hi Search term=none ctermbg=red ctermfg=white guifg=green guibg=Purple
set number

then you shouldn't get any highlighting at all, either of the entire match or of its first character (which is something Vim has no built-in capacity to do anyway), because that feature is disabled by default. Your settings are not ignored, it is just that you didn't enable the highlighting of search result.

To highlight search matches, you must add the following to your vimrc:

set hlsearch

which should give you exactly what you want:

white on red

Without your hi line, you would get the default colors, which are absolutely unusable:

white on yellow

FWIW, the proper way to override parts of a colorscheme is described under :help colorscheme-override:

augroup MyColors
    autocmd ColorScheme default hi Search term=none ctermbg=red ctermfg=white guifg=green guibg=Purple
augroup END
colorscheme default

Note that it might be a good idea to try the various colorschemes that come with Vim, the most recent of which are very finely crafted like (for example)…

  • :colorscheme habamax:

    habamax

  • :colorscheme lunaperche:

    lunaperche

  • :colorscheme zaibatsu (by me):

    zaibatsu

YMMV.

1
  • My only addition to .vimrc apart from what's in the question is set number Commented Feb 13 at 14:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.