Monday, April 25, 2011

vim clear last search highlighting

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?

From stackoverflow
  • to disable

    set nohlsearch
    

    or to switch

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

    Mykola Golubyev : It will disable it forever but not temporary.
    Shaun Bouckaert : 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.
    solomongaby : thanks for the answer ... it was not what i was looking for ... but its a nice info
    Mykola Golubyev : @Shaun: read a question again.
    Shaun Bouckaert : 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.
    skinp : Voted Up. This might not answer his question, but I often find myself wanting to do exactly what your answer does. Thanks
    Mykola Golubyev : @Andy: It is not the answer for the question. How can it be better?
    Brian Carper : Ditto skinp, I didn't know about this. Helpful.
    Andy : @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.
    Mykola Golubyev : As I understood "then reenable it when nedeed again" is important part of the question. If not that I'd use let @/=""
    Shaun Bouckaert : @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.
    claytron : 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.
  • Just to help with completeness, you can also do

    :noh
    
    solomongaby : thanks, this was helpful ... any command for returning the highlight ?
    greyfade : :nohs just shuts off the current highlighting. If you have :set hlsearch then it will continue to highlight your searches.
  • 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.

    Stewart Johnson : 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 instead.
  • From http://twitter.com/jonbho/status/2194406821

    nnoremap <esc> :noh<return><esc>
    
    Stewart Johnson : 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 instead.
    Gavin Miller : Just to be clear, this lets you clear the search highlighting by pressing the Escape key

0 comments:

Post a Comment