Vim uses two different highlight groups for matching patterns while searching in the command-line with
:substitute for example.
incsearchgroup for the match under the cursor
searchgroup for the others
When the command-line is left, the match under the cursor is set back to the
search group. I would like it to remain as it is while in command-line.
I actually wrote a function which changes the group of the match under the cursor. I mapped it to n and N. It also applies a different highlight for the first and last match.
" highlight current search and first/last search differently function! HLCurrent() abort if exists("currmatch") call matchdelete(currmatch) endif " 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 endfunction nnoremap <silent> n n:call HLCurrent()<cr> nnoremap <silent> N N:call HLCurrent()<cr>
It works well, but I still have the problem of the group being changed when leaving the command-line window.
Using my function, I expected this
autocmd to do the trick, but it does not work as expected:
augroup HLCurrentRightNow au! au CmdlineLeave,CmdwinLeave * call clearmatches() | call HLCurrent() augroup end
So, what would be a way to apply my function when exiting the command-line?