From 762089cbee44881d92844205b2eff5fb4e6b6e65 Mon Sep 17 00:00:00 2001 From: Julian Prein Date: Mon, 21 Nov 2022 15:46:50 +0100 Subject: [PATCH] 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. --- .config/vim/vimrc.d/80-autocommands.vim | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/.config/vim/vimrc.d/80-autocommands.vim b/.config/vim/vimrc.d/80-autocommands.vim index add03bb..2fe9d9a 100644 --- a/.config/vim/vimrc.d/80-autocommands.vim +++ b/.config/vim/vimrc.d/80-autocommands.vim @@ -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('') != '') - exec 'match CursorColumn /\V\<' . escape(expand(''), '/\') . '\>/' + let w:cword_match_id = matchadd('CursorColumn', + \ '\V\<' . escape(expand(''), '/\') . '\>') 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