Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vim: search and replace for "&"

Tags:

vim

In order to replace each occurrence of "&" to "&&" in the range from the current line to 30 more lines on, I issue :.,+30s/\\&/\\&\\&/g but Vim says "Pattern not found". I'm escaping the special character "&" by double backslash "\".

like image 323
Ilonpilaaja Avatar asked May 17 '12 15:05

Ilonpilaaja


People also ask

How do you change multiple words in Vim?

Change and repeat Search for text using / or for a word using * . In normal mode, type cgn (change the next search hit) then immediately type the replacement. Press Esc to finish. From normal mode, search for the next occurrence that you want to replace ( n ) and press . to repeat the last change.

How do I select and replace in vi?

Press y to replace the match or l to replace the match and quit. Press n to skip the match and q or Esc to quit substitution. The a option substitutes the match and all remaining occurrences of the match. To scroll the screen down, use CTRL+Y , and to scroll up, use CTRL+E .

What does %s mean in Vim?

Use :s to substitute entire lines in Vim based on a single parameter. For example, substitute every line starting with Do you with Learn more about Vim! using this command: :%s/^Do you .*/ Learn more about Vim!/g. The ^ (caret) symbol matches the beginning of a line, and * matches the rest of the line.


1 Answers

Try not escaping everything - :.,+30s/&/&&/g seems to work for me.

like image 179
twalberg Avatar answered Nov 11 '22 07:11

twalberg