From 6260c84f3a7f4b9e4134a75975c922727059904f Mon Sep 17 00:00:00 2001 From: Julian Prein Date: Sun, 24 Mar 2024 16:35:49 +0100 Subject: [PATCH] vim:aucmd: Clear highlights on exiting visual mode When exiting visual mode I want to clear the highlights of the visual selection. As this is done automatically now, does not need to do it (and i had problems with keys.vim executing ClearHighlights because of the script variables). This reverts commit b3f1a469db9c ("vim:keys: Clear highlights on normal-mode Esc"). --- .config/vim/vimrc.d/40-keys.vim | 4 ++-- .config/vim/vimrc.d/80-autocommands.vim | 13 ++++++++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/.config/vim/vimrc.d/40-keys.vim b/.config/vim/vimrc.d/40-keys.vim index b026528..ac52b55 100644 --- a/.config/vim/vimrc.d/40-keys.vim +++ b/.config/vim/vimrc.d/40-keys.vim @@ -1,7 +1,7 @@ " vim: set foldmethod=marker: " Keybindings """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" -" Clear all kinds of highlights when pressing Escape in normal mode -nnoremap :nohlsearch call ClearHighlights() +" Clear search result highlights when pressing Escape in normal mode +nnoremap :nohlsearch " Indentation jump " https://vim.fandom.com/wiki/Move_to_next/previous_line_with_same_indentation diff --git a/.config/vim/vimrc.d/80-autocommands.vim b/.config/vim/vimrc.d/80-autocommands.vim index 7195d18..09e563b 100644 --- a/.config/vim/vimrc.d/80-autocommands.vim +++ b/.config/vim/vimrc.d/80-autocommands.vim @@ -1,4 +1,10 @@ " Autocommands """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" + +" Bitfield for highlight_current augroup +const s:CLEAR_HIGHS_CWORD = 1 +const s:CLEAR_HIGHS_VISUAL = 2 +const s:CLEAR_HIGHS_ALL = 3 + " Terminal if (has('nvim')) " Disable spellcheck @@ -52,13 +58,13 @@ augroup termdebug_bindings augroup END " Highlight word under cursor -function! ClearHighlights() - if exists('w:cword_match_id') +function! ClearHighlights(what = s:CLEAR_HIGHS_ALL) + if and(a:what, s:CLEAR_HIGHS_CWORD) && exists('w:cword_match_id') call matchdelete(w:cword_match_id) unlet w:cword_match_id unlet w:old_cword endif - if exists('w:visual_match_ids') + if and(a:what, s:CLEAR_HIGHS_VISUAL) && exists('w:visual_match_ids') for l:pairs in w:visual_match_ids let l:id = l:pairs[0] let l:win = l:pairs[1] @@ -117,6 +123,7 @@ augroup highlight_current \ endif au CursorMovedI * call HighlightCurrentWord() au WinLeave * call ClearHighlights() + au ModeChanged [vV\x16]*:* call ClearHighlights(s:CLEAR_HIGHS_VISUAL) augroup END " When switching focus to another window, keep the cursor location underlined.