Vim search – setting different colors for text under cursor

colorssearchvim

Suppose vim search finds 5 matches in my file. I see all 5 of these matches highlighted in yellow. I cycle between these by pressing n. But, as my cursor moves from one match to the next, the highlights all remain the same color. This makes it difficult to see what match my cursor is on. Is there a way to tell vim to highlight all matches with one color, and if visiting a match using n, then color the currently visiting match differently?

Best Answer

There is no built-in option that allows to highlight the current in a different way. While creating the search, you have the 'incsearch' option, but that's it.

You could use another highlighting on top of the search highlighting, e.g. with :match; the special \%# regular expression atom restricts the match to the current cursor position:

:execute 'match IncSearch /\%#' . @/ . '/'

The problem with this is that the highlighting needs to be adapted whenever the current search pattern (@/) changes (oh, and it doesn't handle search offsets like /foo/e-1). So, you'd need to overwrite all search-related commands (/, *, etc.) to hook the :match command into them. I would not recommend this.

Related Question