sponsor Vim development Vim logo Vim Book Ad

basic Tip #93: if you use 'highlight search' feature, map a key to :noh

 tip karma   Rating 492/179, Viewed by 11305 

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

created:   August 3, 2001 9:50      complexity:   basic
author:   Leo Lipelis      as of Vim:   6.0

It is very convenient to use 'hlsearch' option.  However it can be annoying to have the highlight stick longer than you want it.  In order to run it off you have to type at least 4 keystrokes, ":noh".  So, it's a good idea to map this to a key.  I like to map it to control-n.  This is the line I use in my .vimrc file to do it:

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

 rate this tip  Life Changing Helpful Unfulfilling 

<< Reducing 'doc' directory size | Questions & Answers about using tags with Vim >>

Additional Notes

Anonymous, August 3, 2001 9:56
"In order to run it off..." should read "In order to turn it off...".  And just in case it's not obvious from my explanation, after you add the mapping to your .vimrc file, you can turn off search highlights by hitting control-n, or whatever other key you mapped ":noh" to.
Brandels_Mikesh@web.de, August 4, 2001 15:10
Or you could add a simple function to toggle Search highlighting on or off and map to that instead:

function ToggleHLSearch()
    if &hls
        set nohls
    else
        set hls
    endif
endfunction

nmap <silent> <C-n> :silent call ToggleHLSearch()<CR>
dominique.pelle@free.fr, August 4, 2001 19:10
Instead of a function, how about toggling settings with:
   map <F4> :set nohls!<CR>:set nohls?<CR>
wookie@pill.com, August 7, 2001 0:21
I always search for nonsense to get rid of the highlights, ergo:

/sldkjfz<CR>

Now it's dark...
Marcel.van.der.Laan@ict.nl, September 17, 2001 0:08
Just to be pedantic:
   map <F4> :set hls!<CR>:set hls?<CR>

(Uhuh, it's 4 characters shorter... It can be even shorter by using ":se" instead of ":set", but that's pushing it...)
bindu@wavell.net, November 11, 2001 13:56
Since we seem to have a competition going here, check this out:

     :map <F4> :let &hls=!&hls<CR>

It 7 characters shorter than the last one :)
bindu@wavell.net, November 11, 2001 14:00
You can nix 6 more characters with the following:

    :map <F4> :set hls!<CR>
lifeburst@hotmail.com, February 20, 2002 15:57
Which is the same as the previous version except it doesn't tell the user which status the toggle has..
Anonymous, March 27, 2002 7:03
How would you change these scripts to automatically turn highlighting back on when you do another search?
Anonymous, March 27, 2002 7:56
Oh wait... that's what :noh does... my bad!
ncr, June 14, 2002 5:11
For ppl who don't like to toggle :hls but want to get rid of highlighting can do this
map <F4> :let @/ = ""<CR>

-ncr
bgins@hotmail.com, October 20, 2002 11:55
:map <silent> <C-N> :se invhlsearch<CR>

will toggle the feature on and off.  I like this since n is used anyway to find the next occurrence.
Mohit Kalra @nothanks.com, March 6, 2003 0:59
I have modified this mapping to:

map <F12> :let @/=""<CR>  :echo "Highlights Cleared"<CR>

(Added the extra message after cleaning the screen.)

arunrecw@yahoo.com, March 15, 2003 12:02
what I do is very simple, press "/" and put a new search of a word that is certainly to be not there. in that case the highlighted search of earlier item will be removed to nothing. Do it when you have idea of content of file. the new search string must not be there, in order to remain at the same place and removing the highlighted portion. cheerios!!!
alienmind@gmx.net, June 8, 2003 16:24
wookie@pill.com: i always search for nonsense too :)
mails@anand76.ind.in, June 23, 2003 0:50
If you people want to turn this irritating feature (atleast for me!) off, then put set nohls in your .vimrc and forget about it.

Anonymous, November 15, 2003 12:22
I am tired of searching for nonsense to clear the results... this tip
was very helpful. :)

My new .vimrc:

map <silent> \ :let @/=""<cr>
map <silent> H :set !hls<cr>

So, I can type \ and "undo" the highlighting caused by /,
or I can say "screw that" and type H to disable highlighting.
Anonymous, November 15, 2003 12:24
Whoops, that exclamation mark should go on the other side.
Anonymous, May 29, 2004 20:42
Thank you.
Right to the point.  Now I don't have to be reminded by hightlighted "r" characters what a n00b I am!
LOL.
Anonymous, June 30, 2004 23:19
hehe I didnt even know :noh existed. =} this definitely made my day. No longer will I have to /asdf;lkj
Anonymous, July 29, 2004 9:36
I use:
nnoremap <silent> <BS> :noh<CR>
shaggy, December 7, 2004 12:02
Combo set/display since ":set" allows more than one:

  :map <F4> :set hls! hls?<CR>
Anonymous, December 22, 2004 8:12
How about doing :noh automatically when you're entering insert mode?
Anybody knows how to do it?
Anonymous, January 23, 2005 19:24
> How about doing :noh automatically when you're entering insert mode?
> Anybody knows how to do it?

Try this:
:nnoremap i :noh<CR>i
jrcapa@gmail.com, August 22, 2006 7:02
The problem with remapping 'i' is that I also start inserting with o, O, or pasting, etc...
It would be a nice and clean behavior, thought...
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 the maillist. Help Bram help Uganda.
   
SourceForge.net Logo