2

When I search for something with / I get a series of finds, and as I press n for next, it highlights the next one. I always think I'm on the wrong one. Maybe my eyes see green and red, and they think I'm on green. I don't know, but I have to adjust those two colors.

How do I do that? Code is also in the screenshot below...

enter image description here

I can only edit the green background by changing Search but I can't seem to change the red background which signifies the item I'm currently on.

  • 1
    Is this after you press enter? Are you using any plugin (beside your colorscheme) which could change highlights? – Biggybi Apr 17 at 2:38
  • Yes, after I press enter. – Costa Apr 17 at 17:48
  • Yes I am : ( searchant. – Costa Apr 17 at 17:48
  • Without the plugin, my search results stay highlighted forever. 'Esc' doesn't remove them. – Costa Apr 17 at 18:16
  • 1
    I don't know about searchant, but it could be a nice plugin to have. Use :nohl if you want to clear your highlights. – Biggybi Apr 17 at 23:04
1

The current search color is set by the IncSearch highlight group.
That's the color you'll see, while typing your search, on the current item (btw you can use <c-t> and <c-g> while typing to select previous / next item).

But once you hit enter, vim should display only one color (from the search group), so I guess you are using a plugin to display a different color for the current item.

EDIT

That was driving me crazy. I wrote a function that highlights the match in a different color. Well, it's probably not the way to go, since a mapping is needed on n and N (and anything that starts a search, really), and it keeps the 'search hit bottom' from disappearing until the screen is moved.

Besides this caveat, it works quite fine.

function! HLCurrentMatch() abort
    call matchadd('IncSearch', '\c\%#'.@/, 101)
    redraw
endfunction
nnoremap <silent> n n:call HLCurrent()<cr>
nnoremap <silent> N N:call HLCurrent()<cr>

My vimscript skills are poor, but here's the main things to make it more viable in my opinion:

  • Check if we're back to the first match, in order to, say, use a different match, which would even be better than the 'back to first match' message
  • Enable it right when a new match is defined (with /, *, #...)

FINAL

Here it is. This will use different highlight group for the current match, and another different one for the edge matches. Courtesy to @Zorzi for his help on this question

function! HLCurrent() abort
    if exists("currmatch")
        call matchdelete(currmatch)
    endif
    " match only on cursor
    let patt = '\c\%#'.@/
    " check prev and next match
    let prevmatch = search(@/, 'bWn')
    let nextmatch = search(@/, 'Wn')
    " if on first or last match
    if prevmatch == 0 || nextmatch == 0
        let currmatch = matchadd('EdgeSearch', patt, 101)
    else
        let currmatch = matchadd('IncSearch', patt, 101)
    endif
    redraw
    echo("")
endfunction
nnoremap <silent> n n:call HLCurrent()<cr>
nnoremap <silent> N N:call HLCurrent()<cr>

You'll need to define a "EdgeSearch" highlight group, for example:

highlight EdgeSearch guibg=green ctermbg=black

As for the "search hit bottom" message, it's not needed anymore. It can be disabled with set shortmess+=s.

| improve this answer | |
  • 1
    Er, i think (i have to go check!) that with hlsearch on there are multiple colors (one for the active and one for all the others) – D. Ben Knoble Apr 17 at 13:49
  • Okay, so removed the plugin. Things seem to work fine now, but... is there a way to affect the look of my current selection? – Costa Apr 17 at 18:36
  • 1
    As I mentioned, and this is unfortunate, IncSearch does not seem to have any effect once the pattern is validated - but I may be doing something wrong. – Biggybi Apr 17 at 23:03
  • 1
    @Costa you'll love it now – Biggybi May 1 at 23:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.