Annoying vim search highlight "everyone" tries to get rid of
You have turned on hlsearch
for some reason in your vim config (well
obviously to highlight your searches) but it gets in your way, right?
Now you try to find a solution to this problem, like
-
spam random searches
/klsdf<CR>
-
do
:nohl
command or -
map a key to do it —
:nnoremap <C-l> :nohl<CR><C-l>
-
etc
My take to this problem is … not to use hlsearch
:
" highlight incremental searches
set incsearch
" to use with <tab> cnoremaps
set wildcharm=<C-z>
" tab to cycle search candidates
cnoremap <expr> <Tab> getcmdtype() =~ "[/?]" ? "<C-g>" : "<C-z>"
cnoremap <expr> <S-Tab> getcmdtype() =~ "[/?]" ? "<C-t>" : "<S-Tab>"
Or use it only when in /
or ?
because it is still useful to highlight occurences while typing so you might also add following:
"" highlight all occurrences of a term being searched/replaced
augroup hlsearch | au!
au CmdlineEnter /,\? :set hlsearch
au CmdlineLeave /,\? :set nohlsearch
augroup end
And it looks like this:
Interesting approach!
It inspired me to try something similar, but different. This script will clear the highlight after a short pause, and temporarily re-enable it when pressing
n
orN
or other search keys.temphlsearch.vim