Deselecting matching strings after search-and-replace in Vim
Asked Answered
R

5

9

I've got a file (LaTeX) which contains lines I wish to comment out.
The regex that I use after visually-selecting the relevant block is :s/^/%/g, which works fine. However, vim then highlights every matching occurrence of the first part of the regular expression used in the replace, (highlights the first character on the beginning of every line).

The selection changes if I do another search, or another search-and-replace, but I can't work out how to turn it off without doing a 'useless' search.

It's particularly annoying if I search for whitespace (because having every '' highlighted in a text file is visually annoying).

How do I de-select the matching strings after the search-and-replace has been completed?

Refute answered 8/5, 2012 at 13:44 Comment(1)
You could also do V:norm I%<CR> to comment and V:norm ^x<CR> to uncomment without highlighting anything. Also, the g flag at the end of your command is used to perform the substitution on all instances in a same line. Since you are only doing the substitution once par line it's useless.Bringhurst
C
20

:nohlsearch will stop highlighting it but keep it as the active search pattern. (It will start being highlighted on n etc.)

:let @/="" will clear the search pattern register (so that n etc. won't work).

A common thing I've seen in Vim is map <Leader><Space> :noh<CR>; this has the result that (assuming the default leader, backslash) \Space will stop highlighting the current match.

Canfield answered 8/5, 2012 at 13:47 Comment(2)
It might be worth noting that, if you decide to go with a key mapping, you might want to set your mapping to toggle highlighting (with :set hlsearch!) instead of simply disabling it.Tatiana
@TimPote: the mapping is typically done with :noh, not 'nohls'. Thus as soon as you search for something new, it'll start highlighting again.Canfield
E
11

Just search for a string that is not on the page:

/poop 
Egret answered 25/8, 2015 at 13:48 Comment(3)
This does answer the question in the easiest way; but in practice, @ChrisMorgan's answer is much simpler when you're talking about tens of searches per editing session.Shipment
Yes, this does answer the question. I've been using this solution for years because it is easy to remember and it works.Dimity
This is what I do too. Just came here to see if there's something built into vim for this..Argali
T
5

:nohlsearch will remove highlighting from the current search. Highlighting will return on your next search.

:set nohlsearch will disable highlighting for your current vim session.

If you want to disable highlighting completely, add :set nohlsearch to your .vimrc

Tatiana answered 8/5, 2012 at 13:47 Comment(1)
It might be worth while emphasising the difference between :nohlsearch and :set nohlsearch... it's easy to not quite notice.Canfield
A
0

I know this is from a while ago, but I didn't feel like remaping, so what i've been doing is / tab because I use spaces, but if u don't use spaces u can do the opposite. If you use both, stop

Antechoir answered 3/4 at 17:4 Comment(0)
C
-1

Add that to your vimrc, and once done - press in my case <,> + enter to stop highlighting

     map <silent> <leader><cr> :noh<cr>
Claw answered 20/8, 2016 at 10:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.