262

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mbac32768
  • 11,453
  • 9
  • 34
  • 40

14 Answers14

383

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

TankorSmash
  • 12,186
  • 6
  • 68
  • 106
Lee H
  • 5,147
  • 4
  • 19
  • 22
  • 1
    @kmarks2 see you next leap year, I guess? – user1610406 Mar 19 '16 at 00:33
  • 13
    Just clarifying @steamer25's comment. `:set nohlsearch` turns off search highlighting completely `:nohlsearch` or `:noh` will clear the current highlight, but leave you in hlsearch mode for the next search. – Hovis Biddle Mar 25 '16 at 16:53
197

Just put this in your .vimrc

" <Ctrl-l> redraws the screen and removes any search highlighting.
nnoremap <silent> <C-l> :nohl<CR><C-l>
Lucas S.
  • 13,391
  • 8
  • 46
  • 46
91

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

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
jon
  • 2,520
  • 1
  • 14
  • 6
39
" Make double-<Esc> clear search highlights
nnoremap <silent> <Esc><Esc> <Esc>:nohlsearch<CR><Esc>
Andy Lester
  • 91,102
  • 13
  • 100
  • 152
20

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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Zsolt Botykai
  • 50,406
  • 14
  • 85
  • 110
16

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

set nohlsearch
Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
Daniel Bruce
  • 11,269
  • 4
  • 30
  • 28
10
            *: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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Kent Fredric
  • 56,416
  • 14
  • 107
  • 150
  • On a side note, it's weird that one has to :noh to turn off search highlighting while instead of :hlsearch one has to set hlsearch to turn it on again. (I guess I should't be surprised at vim "weirdiness"...) – Marcello Romani Feb 12 '15 at 13:21
5

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Daniel Miessler
  • 161
  • 1
  • 6
4

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

nnoremap <silent> _ :nohl<CR>
3

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Max Cantor
  • 8,229
  • 7
  • 45
  • 59
3

I think this answer in "Vim clear last search highlighting" is better:

:let @/ = ""
Community
  • 1
  • 1
nocache
  • 1,805
  • 16
  • 18
3

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
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
0

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>/
Guillaume
  • 8,741
  • 11
  • 49
  • 62
-1

I add the following mapping to my ~/.vimrc

map e/ /sdfdskfxxxxy

And in ESC mode, I press e/

Aman Jain
  • 10,927
  • 15
  • 50
  • 63