Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deselecting matching strings after search-and-replace in Vim

Tags:

vim

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?

like image 574
simont Avatar asked May 08 '12 13:05

simont


People also ask

How do I search and replace in vim?

The simplest way to perform a search and replace in Vim editor is using the slash and dot method. We can use the slash to search for a word, and then use the dot to replace it. This will highlight the first occurrence of the word “article”, and we can press the Enter key to jump to it.

How do I go to a previous match in vim?

Basic searching Type ggn to jump to the first match, or GN to jump to the last.

How do you Ctrl Z in vim?

on linux, CTRL-Z in vi/vim/gvim mean escape to the console, or put this in the background. you then do whatever you want on the console and type fg (foreground) to bring you back into vim edit session.

What command is used to replace a string with another string in vi editor?

When you want to search for a string of text and replace it with another string of text, you can use the syntax :[range]s/search/replace/. The range is optional; if you just run :s/search/replace/, it will search only the current line and match only the first occurrence of a term.


3 Answers

: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.

like image 115
Chris Morgan Avatar answered Oct 17 '22 20:10

Chris Morgan


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

/poop 
like image 42
Chris Turley Avatar answered Oct 17 '22 20:10

Chris Turley


: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

like image 32
Tim Pote Avatar answered Oct 17 '22 18:10

Tim Pote