How do you make Vim unhighlight what you searched for?

SearchVimHighlight

Search Problem Overview


I search for "nurple" in a file. I found it, great. But now, every occurrence of "nurple" is rendered in sick black on yellow. Forever.

Forever, that is, until I search for something I know won't be found, such as "asdhfalsdflajdflakjdf" simply so it clears the previous search highlighting.

Can't I just hit a magic key to kill the highlights when I'm done searching?

Search Solutions


Solution 1 - Search

:noh (short for nohighlight) will temporarily clear the search highlight. The next search will still be highlighted.

Solution 2 - Search

Just put this in your .vimrc

" <Ctrl-l> redraws the screen and removes any search highlighting.
nnoremap <silent> <C-l> :nohl<CR><C-l>

Solution 3 - Search

/lkjasdf has always been faster than :noh for me.

Solution 4 - Search

" Make double-<Esc> clear search highlights
nnoremap <silent> <Esc><Esc> <Esc>:nohlsearch<CR><Esc>

Solution 5 - Search

Then I prefer this:

map  <F12> :set hls!<CR>
imap <F12> <ESC>:set hls!<CR>a
vmap <F12> <ESC>:set hls!<CR>gv

And why? Because it toggles the switch: if highlight is on, then pressing F12 turns it off. And vica versa. HTH.

Solution 6 - Search

Append the following line to the end of your .vimrc to prevent highlighting altogether:

set nohlsearch

Solution 7 - Search

:noh :nohlsearch
:noh[lsearch]		Stop the highlighting for the 'hlsearch' option.  It
is automatically turned back on when using a search
command, or setting the 'hlsearch' option.
This command doesn't work in an autocommand, because
the highlighting state is saved and restored when
executing autocommands |autocmd-searchpat|.
Same thing for when invoking a user function.

I found it just under :help #, which I keep hitting all the time, and which highlights all the words on the current page like the current one.

Solution 8 - Search

I search so often that I've found it useful to map the underscore key to remove the search highlight:

nnoremap <silent> _ :nohl<CR>

Solution 9 - Search

I think the best answer is to have a leader shortcut:

<leader>c :nohl<CR>

Now whenever you have your document all junked up with highlighted terms, you just hit , + C (I have my leader mapped to a comma). It works perfectly.

Solution 10 - Search

There is hlsearch and nohlsearch. :help hlsearch will provide more information.

If you want to bind F12 to toggle it on/off you can use this:

map     <F12>   :nohlsearch<CR>
imap    <F12>   <ESC>:nohlsearch<CR>i
vmap    <F12>   <ESC>:nohlsearch<CR>gv

Solution 11 - Search

I have this in my .vimrc:

nnoremap ; :set invhlsearch<CR>

This way, ; will toggle search highlighting. Normally, the ; key repeats the latest t/T/f/F command, but I never really used that functionality. I find this setting much more useful, because I can change search highlighting on and off very quickly and can easily get a sense of where my search results are, at a glance.

Solution 12 - Search

Solution 13 - Search

Also, if you want to have a toogle and be sure that the highlight will be reactivate for the next time you search something, you can use this

nmap <F12> :set hls!<CR>
nnoremap / :set hls<CR>/

Solution 14 - Search

I add the following mapping to my ~/.vimrc

map e/ /sdfdskfxxxxy

And in ESC mode, I press e/

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
Questionmbac32768View Question on Stackoverflow
Solution 1 - SearchLee HView Answer on Stackoverflow
Solution 2 - SearchLucas S.View Answer on Stackoverflow
Solution 3 - SearchjonView Answer on Stackoverflow
Solution 4 - SearchAndy LesterView Answer on Stackoverflow
Solution 5 - SearchZsolt BotykaiView Answer on Stackoverflow
Solution 6 - SearchDaniel BruceView Answer on Stackoverflow
Solution 7 - SearchKent FredricView Answer on Stackoverflow
Solution 8 - SearchjeffdalyView Answer on Stackoverflow
Solution 9 - SearchDaniel MiesslerView Answer on Stackoverflow
Solution 10 - SearchJeffrey VannesteView Answer on Stackoverflow
Solution 11 - SearchMax CantorView Answer on Stackoverflow
Solution 12 - SearchnocacheView Answer on Stackoverflow
Solution 13 - SearchGuillaumeView Answer on Stackoverflow
Solution 14 - SearchAman JainView Answer on Stackoverflow