67

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?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Roger
  • 1,843
  • 3
  • 20
  • 22
  • 3
    possible duplicate of [vim clear last search highlighting](http://stackoverflow.com/questions/657447/vim-clear-last-search-highlighting) –  Jul 27 '10 at 09:56

8 Answers8

114

Try the :noh command.

vi/vim notes

Ludovic Kuty
  • 4,868
  • 3
  • 28
  • 42
Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
  • How would you map :noh to but keep 's redraw behavior intact? – meder omuraliev Aug 29 '09 at 20:12
  • Not sure if that was part of the assignment, but if you insist, why not add `:redraw` to the mapping? – Michael Krelin - hacker Aug 29 '09 at 20:23
  • 11
    Thanks, used vim for years but was not aware of that one. I just wonder if it is possible for my fingers to unlearn /lkjlkj ? – codeape Aug 29 '09 at 20:56
  • 6
    it is my experience that fingers are very eager to learn new things, it just takes them ages to forget old ones ;-) – Michael Krelin - hacker Aug 29 '09 at 21:43
  • Thanks for the tip. this isn't much different than doing /lkj tho...and actually takes more keypresses. But does do exactly what we wanted and does not leave you with a warning at the bottom of the screen. ;) – Metagrapher Jan 08 '12 at 15:14
  • It is different from `/lkj` in that you can use `n`, for instance, to go to the next match. In short, it doesn't alter the state, but hides the highlight. Of course, `/lkj` is often appropriate solution as well. The important part is to know know the difference ;-) – Michael Krelin - hacker Jan 09 '12 at 16:57
  • 2
    Why am i the only one to use `/asd` for this? – 0xc0de Apr 05 '12 at 12:21
  • @MichaelKrelin-hacker, nopes :( either my keyboard loves my left hand more or my typing has evolved in a different way.. – 0xc0de Apr 06 '12 at 06:08
  • 2
    @0xc0de, well, another hypothesis: some people want to alternate hands (`/` and then `asd`), some prefer to do the whole thing with one (`/` and then `lkj`). To tell you the truth, I have no idea to which category I belong, I use `:noh`. – Michael Krelin - hacker Apr 06 '12 at 12:07
46

I use

/pleasedisablehighlightthanks

command. Or just

/qewrufhiqwe

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

/qewrufhiqew
P Shved
  • 96,026
  • 17
  • 121
  • 165
  • It would have the same effect like that of `:noh`, but you have to be careful not to hit something that can be found ;-) – Michael Krelin - hacker Aug 29 '09 at 20:26
  • This is the technique I've used - but the :noh turns of the highlights too (leaving the next search in highlight mode), so it is 'better'. (All else apart, I end up with a 'Pattern not found: kkk' error message at the bottom, which has to be cleaned up to. – Jonathan Leffler Aug 29 '09 at 20:44
  • I just use /asdf, it's shorter :) –  Jan 14 '10 at 16:44
  • I am sure down-voters took it seriously and tried it... Upvote. – Cbhihe Sep 07 '15 at 13:57
17
: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.

meder omuraliev
  • 183,342
  • 71
  • 393
  • 434
10

I have the following in my .vimrc:

map <silent> <C-N> :let @/=""<CR>
jqno
  • 15,133
  • 7
  • 57
  • 84
  • Can someone walk me throug what's happening in this code. I don't get it. – Roger Aug 29 '09 at 22:34
  • 1
    `@/` is the register that contains the latest search term. This command clears that register, which has the effect that the search highlights disappear. It's sort of equivalent to `:nohls`. – jqno Aug 30 '09 at 11:29
  • 1
    I have the same :let mapped to -- its a comfortable reach up to \ – zen Dec 11 '09 at 07:33
  • 1
    @jqno no it’s not equivalent, as it clears your search term, thus making :norm n (find next match) unavailable. :noh just turn off search hilite temporarily. – GergelyPolonkai May 03 '14 at 11:02
6

This might suit your needs:

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

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

Mosh
  • 2,458
  • 3
  • 18
  • 19
5

Try this:

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

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

John Feminella
  • 303,634
  • 46
  • 339
  • 357
1

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

Ben Hughes
  • 14,075
  • 1
  • 41
  • 34
0

This solution toggles the search:

nnoremap <silent><expr> <c-l> (&hls && v:hlsearch ? ':nohls' : ':set hls')."\n" <BAR> redraw<CR>
SergioAraujo
  • 11,069
  • 3
  • 50
  • 40