How to get rid of search highlight in Vim

Vim

Vim Problem Overview


I have :set hlsearch as default value.

When I search for something, search terms get highlighted. However many times I want to get rid of the highlight, so I do :set nohlsearch. In this way I get rid of highlights for the time being.

However if I do a new search, then search terms are not highlighted.

I would like to hit ESC + ESC to get rid of highlights and then set back :set hlsearch.

Any suggestions?

Vim Solutions


Solution 1 - Vim

Try the :noh command.

vi/vim notes

Solution 2 - Vim

I use

/pleasedisablehighlightthanks

command. Or just

/qewrufhiqwe

But you should be carefult not to mix this with the following command!

/qewrufhiqew

Solution 3 - Vim

:noremap <silent> <c-l> :nohls<cr><c-l>

This would redraw the screen and clear any search terms with Control-L, handy :) easier than reaching up to the F keys.

Solution 4 - Vim

I have the following in my .vimrc:

map <silent> <C-N> :let @/=""<CR>

Solution 5 - Vim

Try this:

set hlsearch!
nnoremap <F12> :set hlsearch!<CR>

and hit F12 to clear when desired. Use :noh in command mode to clear.

Solution 6 - Vim

This might suit your needs:

nnoremap <esc> :noh<return><esc>

With a little tinkering you can make it work in insert mode.

Solution 7 - Vim

you could search for something not in the text file. Nothing will be highlighted in this case. (e.g. /349i5u9sgh)

Solution 8 - Vim

This solution toggles the search:

nnoremap <silent><expr> <c-l> (&hls && v:hlsearch ? ':nohls' : ':set hls')."\n" <BAR> redraw<CR>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRogerView Question on Stackoverflow
Solution 1 - VimMichael Krelin - hackerView Answer on Stackoverflow
Solution 2 - VimP ShvedView Answer on Stackoverflow
Solution 3 - Vimmeder omuralievView Answer on Stackoverflow
Solution 4 - VimjqnoView Answer on Stackoverflow
Solution 5 - VimJohn FeminellaView Answer on Stackoverflow
Solution 6 - VimMoshView Answer on Stackoverflow
Solution 7 - VimBen HughesView Answer on Stackoverflow
Solution 8 - VimSergioAraujoView Answer on Stackoverflow