sponsor Vim development Vim logo Vim Book Ad

basic Tip #764: Unhighlighting searched-for text

 tip karma   Rating 101/50, Viewed by 3237 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   July 20, 2004 8:15      complexity:   basic
author:   alex.kahn@comcast.net      as of Vim:   5.7

If you have hlsearch on, you might find it bothersome that a string you've searched for stays highlighted until you make another search or type something like "/asothuse," searching for a random string of characters, as the FAQ recommends.

With a simple mapping, this is a breeze. Add this line to your .vimrc:

:map <F3> :let @/ = "" <CR>

This unsets the "last search pattern" register.

This should be a simpler solution to a common gripe.

 rate this tip  Life Changing Helpful Unfulfilling 

<< Drag Words with C-left/right. | Complete from filetype syntax file >>

Additional Notes

an@nymo.us, July 20, 2004 8:21
:noh  -> no hightlight
Anonymous, July 20, 2004 8:22
I like using

nnoremap <CR> :noh<CR>

So when I'm done, searching, I just hit CR and all highlighting turns off.  I can still use n/N to resume searching and it will highlight again!
alex.kahn@comcast.net, July 20, 2004 8:52
Ha! I guess this tip is pretty useless!
Anonymous, July 21, 2004 0:56
it is not useless. it was worth just to get the tip on the
nnoremap <CR> :noh<CR>

that's itself deserve a tip
Anonymous, July 21, 2004 6:37
Nice to find out this, the highlight is annoying when you don't need it.

Discovered CR will not work in command and search history window using "nnoremap <CR> :noh<CR "
, recommend CTRL-CR

nnoremap <c-cr> :noh<CR>
Anonymous, July 22, 2004 3:48
this is great.  i recommend the <CR> mapping, not sure of any negative side effects but it seems good so far.
Anonymous, July 22, 2004 7:41
The <CR> mapping interferes with the quickfix window.
I use grep.vim (http://www.vim.org/scripts/script.php?script_id=311) and the <CR> mapping prevents me from "jumping" to the file listed in the quickfix window.

I use the following mapping:
map <silent> <c-n> :silent nohl<cr>
alexanro@scicraft.org, July 24, 2004 6:47
I use

nnoremap <silent> <F12> :noh<CR>
Anonymous, July 25, 2004 6:44
I use

nmap <silent> <C-N> :silent noh<CR>

So when I'm done searching and n'ing between hits I press ctrl+n to turn of hilighting.
Anonymous, July 26, 2004 0:25
I use

noremap <C-L> :nohl<cr><C-L>
Anonymous, July 26, 2004 4:59
What about using the following mapping?

:nnoremap <CR> :noh<CR><CR>

disables highlighting with <CR> and keeps the old behavior of <CR>.
fredo@simple-nude.com, August 3, 2004 3:19
alex.kahn@comcast.net, July 20, 2004 8:52  wrote:

<< Ha! I guess this tip is pretty useless!  >>

Not at all! This tip (and the collection of followups) was very helpful to me,  a
brand-new vim user. There must be a good reason why there's no easy and
straightforward way of disablling highlighting, but this newb hasn't yet uncovered it.

-- fr
Anonymous, August 4, 2004 9:23
:map <F3> :let @/ = "" <CR> ^L is nice too.
Anonymous, August 6, 2004 18:56
:map <f12> :set invhls<CR>

Invert highlight setting - one keypress turn off the highlight, or turns it back on.
Anonymous, August 13, 2004 9:16
how about simply: /z<cr> ?  Replace "z" with any string not (common) in the buffer, and poof, your highlights disappear.
porjes@spamcop.net, August 19, 2004 1:57
I use:

:map <F9> :set hlsearch!<CR>

which toggles search highlighting.
v.online@gmail.com, March 8, 2005 18:49
:set invhls
:set hlsearch!
are same commands to toggle highlight searching. The pain in using this command is, you have to toggle everytime before the new search. so I would rather go for
:nnoremap <CR> :noh<CR><CR>
:nnoremap <F12> :noh<CR><CR>

Thanks for the tips.

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to vim@vim.org after searching the archive. Help Bram help Uganda.
Sponsored by Web Concept Group Inc. SourceForge.net Logo