How to replace '&=' with '=&' in Vim? - vim

I tried to replace '&=' with '=&' in Vim, but the command :%s/&=/=&/g
replaced '&=' with '=&='.
Why did that happen ?
What command should I use ?

Just escape the ampersand.
:%s/&=/=\&/g
You need to escape it because & is a special character in the replacement that will be replaced with the entire matched pattern.

Related

Find and replace '\' in vim [duplicate]

For instance, if I wanted to a find and replace with strings containing backward or forward slashes, how would this be accomplished in vim?
Examples
Find & Replace is: :%s/foo/bar/g
what if I wanted to find all occurrences of <dog/> and replace it with <cat\>
Same way you escape characters most anywhere else in linuxy programs, with a backslash:
:%s/<dog\/>/<cat\\>
But note that you can select a different delimiter instead:
:%s#<doc/>#<cat\\>#
This saves you all typing all those time-consuming, confusing backslashes in patterns with a ton of slashes.
From the documentation:
Instead of the / which surrounds the pattern and replacement string, you
can use any other single-byte character, but not an alphanumeric character,
\, " or |. This is useful if you want to include a / in the search
pattern or replacement string.
%s:<dog/>:<cat>
You can replace the / delimiters if they become annoying for certain patterns.
Quote them with a backslash. Also, it often helps to use another delimiter besides slash.
:%s#<dog/>#<cat\\>#
or if you have to use slash as the substitute command delimiter
:%s/<dog\/>/<cat\\>/
I was looking for something similar, to search for register values containing the / character (to record a macro). The solution was to search using the ? token instead of the /.
The syntax is:
:%s/<dog\/>/<cat\\>/g
backslash slash backslash star
/(<- the prompt)\/\*
so after you type it looks like
/\/\*

Vim Search and Replace to a single backslash

I am trying to replace all "\\" into "\" in a selection.
I tried using :'<,'>s/\\\\/\\/g to do the substitution, but vim would replace "\\" with "\\".
If I use :'<,'>s/\\\\/\/g instead, vim replaces "\\" with "/g".
Is there a way to specify the substitute string is literally a single backslash?
You should enclose your selection by using:
\%V pattern \%V
Like this:
:'<,'>s/\%V\\\\\%V/\/
For further information read:
:h \%V

Execute vim substitute command that includes '/' characters

I have a java file in which I wrote /t, instead of \t, the proper notation of the tab escape function. I want to use vim’s substitution feature (:s) but when I do something like this:
:%s//t/\t/g
I get the error message
E488: Trailing characters
How can I write the command such that I can execute the substitution? Thank you.
Use backslash to escape the characters. So:
:%s/\/t/\\t\g
You can also use a different delimiter like:
:%s#/t#\\t#g
Use colons instead of a slash to avoid confusions and errors if what you want to look-up/replace has slashes. This way VIM will be able to parse the sentence correctly. Then you need to scape the back-slash otherwise VIM will replace /t with tabulations
:%s:/t:\\t:g
Escape the forward slash in your search pattern:
:%s/\/t/\\t/g
or use another separator:
:%s;/t;\\t;g

vim: search and replace for "&"

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 "\".
Try not escaping everything - :.,+30s/&/&&/g seems to work for me.
You don't need to escape the ampersand in the first part of a regular expression. It only has special meaning in the second (replace) part.
You are looking for patterns that say "\&" and replace them with patterns that say "\&\&".
The simple way to replace is just:
:.,+30s/&/&&/g
It can also be useful remark that if you need to change the & position for example: _& to &_ the & has to be escaped only in the replace statements of the substitute expression:
:.,+30s/ &/\& /g
^

vim string substitution

I want to substitute all cygdrive/e with cygdrive/d using vim. But I can't get the matching pattern correctly. Here is my command:
s/cygdrive\/e/cygdrive\/d/g
it doesn't work. Can anybody show me what is wrong?
Thanks,
vim allows you to specify the delimiter.. (First character after s is the delimiter)
s/cygdrive\/e/cygdrive\/d/g
using line range argument .. and # as delimiter
ESC:
:1,$ s#/cygdrive/e#/cygdrive/d#g
Your search pattern and replacement string look fine.
Make sure you are in ex mode when you try it.
So press ESC, then : and then
%s/cygdrive\/e/cygdrive\/d/g
But if you want all he replacements in just the current line you can do:
s/cygdrive\/e/cygdrive\/d/g
You have to escape the special character
like this
s/cygdrive\/e/cygdrive\/d/g

Resources