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
function! HighlightCurrentWord()
if exists('w:cword_match_id')
call matchdelete(w:cword_match_id)
unlet w:cword_match_id
endif
if (expand('<cword>') != '')
exec 'match CursorColumn /\V\<' . escape(expand('<cword>'), '/\') . '\>/'
let w:cword_match_id = matchadd('CursorColumn',
\ '\V\<' . escape(expand('<cword>'), '/\') . '\>')
endif
endfunction
augroup highlight_current_word
au!
au CursorHold * call HighlightCurrentWord()
au CursorMoved * match
au CursorMoved * call HighlightCurrentWord()
augroup END
" Do not mark input from stdin as modified