Get rid of Vim's highlight after searching text

Vim

Vim Problem Overview


In VIM, after finding text with "/" command, that text remains highlighted.

What is the command to remove that? I don't want to remove highlighting capability at all, but don't want to have all those bright text spots once I've found what I need.

Thanks.

Vim Solutions


Solution 1 - Vim

Type this:

:noh

Solution 2 - Vim

You can toggle it with

:set hls!

Of course a quick and dirty alternative is to do another search for gibberish:

/asdsad

I usually bind a key to :set hls! to make this easy and use the gibberish approach when I'm in vim on some machine I don't have my profile installed on.

Solution 3 - Vim

Completely disable search highlights

:set nohlsearch

:nohlsearch

or :noh for short. This clears highlights until a new search is performed or n or N is pressed


Clear on pressing custom map

  • Clear highlights on hitting the ESC key

     nnoremap <esc> :noh<return><esc>
    
  • Clear highlights on pressing </kbd> (backslash) twice

     nnoremap \\ :noh<return>
    

Solution 4 - Vim

I'm lazy and type something like /asdf then slap the RETURN key.

Solution 5 - Vim

I have this in my .vimrc:

map <leader>h :set hlsearch!<cr>

So when I type:

\h

It toggles highlighting on/off.

Solution 6 - Vim

If you don't want to remove highlighting one of the best ways is to clear the search register, unless of course you need the search items later. This will prevent you from having to re-enable the highlighting and(Edit: noh does not permanently disable highlighting) prevent you from accidentally jumping around. This is how I have mine setup:

nmap <silent> ,/ :let@/=""<CR>

What this does is map the key sequence ,/ in normal mode to clear the search register @/ by setting it to an empty string. This is just an alternative to what has already been stated.

Solution 7 - Vim

In addition to “clear the search register”, you can even reset the search register to its previous value:

command! -nargs=* -range S
\ let atslash=@/|exe ':'.<line1>.','.<line2>.'s'.<q-args>|let @/=atslash

However:

  • this does not reset the previous status of :hls. I do not believe this to be possible in general.
  • this defines a new command, :S, to use in place of :s. You can use a cabbrev to map one to the other, but this will break when you add a range to the substitute command.

Solution 8 - Vim

:noh will get rid of highlighted text.

Solution 9 - Vim

is.vim plugin has been pretty handy for me. It automatically clears the searches in various scenarios, you just need to install it via any plugin manager you use and it will do the work for you, no need to do setup anything or change settings manually. It just works.

is.vim gif

(image taken from the official github repository)

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
QuestionMarkView Question on Stackoverflow
Solution 1 - VimAndrew ClarkView Answer on Stackoverflow
Solution 2 - Vimdenis phillipsView Answer on Stackoverflow
Solution 3 - VimSheharyarView Answer on Stackoverflow
Solution 4 - Vimthe Tin ManView Answer on Stackoverflow
Solution 5 - VimsashangView Answer on Stackoverflow
Solution 6 - VimTom MillerView Answer on Stackoverflow
Solution 7 - VimCirconflexeView Answer on Stackoverflow
Solution 8 - VimjiangdongziView Answer on Stackoverflow
Solution 9 - VimSergio G. Sánchez ValenciaView Answer on Stackoverflow