functions.vim 584 B

123456789101112131415161718192021
  1. " distinct highlight current, first and last match when searching
  2. function! HLCurrent() abort
  3. if exists("currmatch")
  4. call matchdelete(currmatch)
  5. endif
  6. " only on cursor
  7. let patt = '\c\%#'.@/
  8. " check prev and next match
  9. let prevmatch = search(@/, 'bWn')
  10. let nextmatch = search(@/, 'Wn')
  11. " if on first or last match
  12. if prevmatch == 0 || nextmatch == 0
  13. let currmatch = matchadd('EdgeSearch', patt, 101)
  14. else
  15. let currmatch = matchadd('IncSearch', patt, 101)
  16. endif
  17. redraw
  18. endfunction
  19. nnoremap <silent> n n:call HLCurrent()<cr>
  20. nnoremap <silent> N N:call HLCurrent()<cr>