Categories
full-text-search highlight vim

Vim clear last search highlighting

2215

After doing a search in Vim, I get all the occurrences highlighted. How can I disable that? I now do another search for something gibberish that can’t be found.

Is there a way to just temporarily disable the highlight and then re-enable it when needed again?

6

  • 7

    :set invhlsearch will disable the highlighting if its already highlighted and enable it if it isn’t. You can map it to say Shift-H.

    – puffadder

    Jan 3, 2011 at 9:47

  • 1

    @StewartJohnson doing that in my .vimrc files seems to interfere/break with :set mouse=a. Any ideas why? This error only happens in iTerm2

    Jun 16, 2014 at 13:54

  • I have typed so much gibberish over the years to clear that highlight (really inefficient for multi-GB text files). The gibberish search is essentially a second search which wastes compute time. Great question!

    – Megatron

    Nov 26, 2014 at 21:16


  • 24

    @StewartJohnson Be careful! Your top-rated comment with mapping cause errors and unexpected behaviour as described in the comments below to stackoverflow.com/a/1037182/1698467

    – skywinder

    Feb 19, 2015 at 18:42


  • 9

    The nnoremap <esc> :noh<return><esc> solution suggested by @StewartJohnson works nicely in GUI vim, but causes problems with arrow keys and other ESC-encoded keys when running vim in a terminal. Don’t put it in your ~/.vimrc without wrapping it in if has('gui_running')end.

    – jbyler

    Sep 28, 2017 at 18:16


1937

To turn off highlighting until the next search:

:noh

Or turn off highlighting completely:

set nohlsearch

Or, to toggle it:

set hlsearch!

nnoremap <F3> :set hlsearch!<CR>

7

  • 196

    This is not clear from this answer and comments : note that set nohlsearch will disable the highlighting for next searches as well. The behaviour of :noh is very different : the highlighting will be automatically reenabled when you perform a new search or even when you do something related to the current search (e.g. when you type n to go to the next item).

    Dec 20, 2012 at 14:42

  • 5

    I map it to <Ctrl-L> because that’s my go-to keystroke for cleaning up the screen. Here is my mapping: nmap <silent> <C-L> <C-L>:nohlsearch<CR>:match<CR>:diffupdate<CR>

    Oct 8, 2018 at 8:54

  • @Rainning, is this what you would put in .vimrc? If not, what can you put in .vimrc to permanently enable :noh after every search?

    Apr 13, 2021 at 3:02

  • 7

    For reader in 2021: nnoremap <nowait><silent> <C-C> :noh<CR>.

    – VimNing

    Apr 13, 2021 at 3:14

  • @LiamClink: I’ve updated the command, should be nnoremap. And yes you should put it in your .vimrc. I don’t understand your question, since the next time you hit / on your key board and enter a new keyword to search, to old ones are all replaced, so you only need to cleanup by <C-C> yourself after every search.

    – VimNing

    Apr 13, 2021 at 3:18


773

From the VIM Documentation

To clear the last used search pattern:

:let @/ = ""

This will not set the pattern to an empty string, because that would match
everywhere. The pattern is really cleared, like when starting Vim.

21

  • 137

    …which would be a better solution to his problem as I understand it.

    Mar 18, 2009 at 9:46

  • 9

    This is what I was looking for! I voted it up, the question is slightly vague. This led me to think this is what he wanted: “I now do another search for something gibberish that can’t be found”. Because that is what I was doing to clear the search, but not disable it so the next search would highlight again.

    – claytron

    Aug 31, 2010 at 18:42

  • 54

    This is what I thought the question was about… here is a command I made to quicken clearing of the search string (and therefore removing all highlights): :command C let @/="" Using this allows you to type :C to clear the search string… very quick and doesn’t affect future searching or highlighting.

    – Jason

    Mar 9, 2011 at 13:31


  • 13

    @jcreamer898, see vim help on “registers” (:he registers). Vim has several different built-in registers that hold text, sometimes for yanked data, sometimes for last search, etc. When you do a search, vim puts the pattern in the “/” register, which you reference using @/. You can also assign values to registers using @regname=value where regname is the is the name of the register. So, @/="" is simply setting register “/” to an empty string (except that for the “/” register vim will clear the last search if it contains an empty string).

    – Ben Davis

    Jun 6, 2014 at 22:32

  • 9

    Add this to your .vimrc to get Ctrl+/ to clear the last search: noremap <silent> <c-_> :let @/ = ""<CR>

    – angrydust

    Sep 15, 2014 at 21:46


632

+50

You can do

:noh

or :nohlsearch to temporarily disable search highlighting until the next search.

5

  • 3

    thanks, this was helpful … any command for returning the highlight ?

    Mar 19, 2009 at 13:47

  • 20

    :nohs just shuts off the current highlighting. If you have :set hlsearch then it will continue to highlight your searches.

    – greyfade

    Mar 19, 2009 at 15:55

  • 6

    I think this is what the OP was searching for (although Shaun’s answer works too). I’m upvoting this for being shorter. I know he accepted the nohls answer, but setting it will disable all search highlighting, even if another search is tried afterwards.

    Apr 16, 2012 at 14:18


  • 5

    This is probably what the OP wanted in the first place and is the shortest, best answer IMHO.

    – user195488

    May 31, 2012 at 15:05

  • Perfection is attainable even in this mortal realm! I will use this more than :help, and don’t want to pollute <esc>: nnoremap <F1> :noh<CR>

    Oct 3, 2012 at 8:24