vim:autocmd: Use matchadd to Highlight cword

Use `matchadd` instead of `match` as it has no limit on the number of
highlights.

Also call the function every time the cursor moved instead of only when
holding. This makes it snappier while also keeping the highlight when
moving inside the cword.
This commit is contained in:
2022-11-21 15:46:50 +01:00
parent 7a4e8e6df2
commit 762089cbee

View File

@@ -53,14 +53,18 @@ augroup END
" Highlight word under cursor " Highlight word under cursor
function! HighlightCurrentWord() function! HighlightCurrentWord()
if exists('w:cword_match_id')
call matchdelete(w:cword_match_id)
unlet w:cword_match_id
endif
if (expand('<cword>') != '') if (expand('<cword>') != '')
exec 'match CursorColumn /\V\<' . escape(expand('<cword>'), '/\') . '\>/' let w:cword_match_id = matchadd('CursorColumn',
\ '\V\<' . escape(expand('<cword>'), '/\') . '\>')
endif endif
endfunction endfunction
augroup highlight_current_word augroup highlight_current_word
au! au!
au CursorHold * call HighlightCurrentWord() au CursorMoved * call HighlightCurrentWord()
au CursorMoved * match
augroup END augroup END
" Do not mark input from stdin as modified " Do not mark input from stdin as modified