views:

2728

answers:

5

After you do a search in vim you get all the occurrences highlighted, how can you 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?

+25  A: 

to disable

set nohlsearch

or to switch

set hlsearch!

nnoremap <F3> :set hlsearch!<CR>
Mykola Golubyev
thanks ... this was what i was looking for
solomongaby
or just use type :nohl in cmd mode
Mikeage
:noh is enough :)
Leonardo Constantino
I know :) I like explicit things.
Mykola Golubyev
+26  A: 

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.

Shaun Bouckaert
It will disable it forever but not temporary.
Mykola Golubyev
This doesn't disable the search, it clears the pattern.
Shaun Bouckaert
...which would be a better solution to his problem as I understand it.
Shaun Bouckaert
thanks for the answer ... it was not what i was looking for ... but its a nice info
solomongaby
@Shaun: read a question again.
Mykola Golubyev
Your answer does indeed answer his question better, by turning the highlight on and off. However, if the issue was only that the last search stayed highlighted after you found what you were looking for, which is how I initially understood the question, then my method allows you too clear the search.
Shaun Bouckaert
Voted Up. This might not answer his question, but I often find myself wanting to do exactly what your answer does. Thanks
skinp
@Andy: It is not the answer for the question. How can it be better?
Mykola Golubyev
Ditto skinp, I didn't know about this. Helpful.
Brian Carper
@Mykola: I agree that you answered the question perfectly correctly, but your solution requires extra key-presses / steps. e.g. Search, turn off highlighting, turn on highlighting, search again.Shaun's solution will only require me to Search, clear highlighting, search again.
Andy
@Mykola: Clarification to the above - it's working from the mindset that after I've searched and modified some text, I want to get rid of the search entirely. With my personal workflow I won't want to turn off the search results until I've finished with them.
Andy
As I understood "then reenable it when nedeed again" is important part of the question. If not that I'd use let @/=""
Mykola Golubyev
@Mykola I actually thought that he only asked how to disable and re-enable it again because he wasn't sure you could clear the search, so he was looking for the next best option.
Shaun Bouckaert
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
+12  A: 

Just to help with completeness, you can also do

:noh
Matt McMinn
thanks, this was helpful ... any command for returning the highlight ?
solomongaby
:nohs just shuts off the current highlighting. If you have :set hlsearch then it will continue to highlight your searches.
greyfade
+5  A: 

I found this answer years ago on vim.org:

Add the following to your .vimrc:

"This unsets the "last search pattern" register by hitting return
nnoremap <CR> :noh<CR><CR>

Thus, after your search, just hit return again in command mode, and the highlighting disappears.

I think this is a *much* better solution than the one that's actually been accepted -- either this one or the other one where it's mapped to <ESC> instead.
Stewart Johnson
+4  A: 

From http://twitter.com/jonbho/status/2194406821

nnoremap <esc> :noh<return><esc>
Baruch Even
I think this is a *much* better solution than the one that's actually been accepted -- either this one or the other one where it's mapped to <CR> instead.
Stewart Johnson
Just to be clear, this lets you clear the search highlighting by pressing the Escape key
Gavin Miller