98

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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Mark
  • 1,751
  • 3
  • 14
  • 14
  • 4
    possible duplicate of [How to get rid of search highlight in vim](http://stackoverflow.com/questions/1352242/how-to-get-rid-of-search-highlight-in-vim) – Conner Aug 15 '12 at 19:52
  • 4
    possible duplicate of [vim clear last search highlighting](http://stackoverflow.com/questions/657447/vim-clear-last-search-highlighting) – the Tin Man Aug 08 '14 at 15:40

10 Answers10

150

Type this:

:noh
Andrew Clark
  • 202,379
  • 35
  • 273
  • 306
  • 11
    ...which is short for `:nohlsearch`, which should help you remember it! – Cascabel Dec 07 '10 at 01:43
  • 46
    I have `:noh` mapped as such: `nnoremap :noh` I find that hitting escape to be a natural vim way of clearing the search highlight. – Peter Rincker Dec 07 '10 at 15:22
  • 8
    It might be useful to note that this *doesn't* disable the search highlighting, it simply clears the current highlight (which is what I want). – wchargin Jul 02 '14 at 14:51
  • 2
    @PeterRincker It's [said](https://vi.stackexchange.com/a/2615/26169) to not map `Esc`. [A problem I encountered doing this.](https://vi.stackexchange.com/questions/34647/mapping-esc-makes-vim-open-in-replace-mode) – Amir Shabani Sep 28 '21 at 18:21
34

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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
denis phillips
  • 12,550
  • 5
  • 33
  • 47
  • 1
    Search for gibberish has some important downside: it adds garbage to search history. This may be inconvenient for some users the same way redundant highlighting distracts from work that has to be done. – Victor Yarema Sep 12 '18 at 16:23
  • If you happen to have the `'incsearch'` option on (which is a great option to have on), then searching for gibberish will make your screen jump at random places while typing said gibberish. Binding some normal-mode key to `:nohlsearch` is just so much better on all aspects. – Maëlan Sep 14 '21 at 17:55
27

Completely disable search highlights

:set nohlsearch

Clear until next search

: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 \ (backslash) twice

    nnoremap \\ :noh<return>
    
Sheharyar
  • 73,588
  • 21
  • 168
  • 215
  • 1
    I try to keep my ~/.vimrc as minimal as possible, however this nice double-backslash trick found a place there -- thanks. – solr May 09 '18 at 20:53
16

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

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
12

I have this in my .vimrc:

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

So when I type:

\h

It toggles highlighting on/off.

sashang
  • 11,704
  • 6
  • 44
  • 58
10

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.

Tom Miller
  • 820
  • 6
  • 17
3

:noh will get rid of highlighted text.

jiangdongzi
  • 383
  • 1
  • 2
  • 10
2

This might not be the most proper method, but control-L redraws the screen, clearing any highlighting in the process. This saves you from having to create another binding.

Galladite
  • 83
  • 6
1

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.

Circonflexe
  • 351
  • 1
  • 9
0

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)

Sergio G
  • 101
  • 1
  • 6
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/29814228) – ryanttb Sep 13 '21 at 22:22