Vi and Vim Asked by Biggybi on August 31, 2021
Vim uses two different highlight groups for matching patterns while searching in the command-line with /
or :substitute
for example.
incsearch
group for the match under the cursorsearch
group for the othersWhen 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?
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP