Vim Search and Replace to a single backslash - vim

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

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: How to search for a hard-coded string (not regex)?

In order to search for a string in Vim, I click "/" and then type the word that I have to search. Vim looks at this string as regular expression. I want to know how to search a string, as it it, and not treat it as a regex.
Search commands always search for patterns (also known as regular expressions). You can make patterns more or less magic but cannot turn metacharacters completely off. If you have a fixed string you have to escape the characters that vim understands as metacharacters.
With the very nomagic mode of Vim's regular expressions (:help /\V), only the backslash is a special character that needs escaping.
So, prepend \V to your literal search, and (either manually or via escape(pattern, '\')) duplicate any backslashes. The following turns a "regular" search in to a literal one; you could define a mapping for that:
:let #/ = '\V' . escape(#/, '\')

How to replace "%" inside vim? \% doesn't work

I have c format text like this:
"%d %d"
I wish to change it into text like "%l %l"
I tried in vim command mode like:
:%s:\%d:\%l:g
It doesn't work. Then I tried:
:%s:%%d:%%l:g
Doesn't work either. How to fix it?
You don't need to escape the % character. Either of these work:
:%s/%d/%l/g
:%s:%d:%l:g
You're not limited to just / or : as delineation characters though. From vim's help:
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. Example: >
:s+/+//+

How to replace '&=' with '=&' in 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.

Find and replace a string separated by delimiter

How can i find and replace a string like "aa:bb:cc:dd" with "ring12" when using vi in linux? For normal strings without delimiter, i know it as
%s/randomstring/ring12/g
But how to deal with string having ':' symbol? I know i should use backslash '\' but don't know the syntax.
actually it doesn't require a \ ... you can simply use something like this:
<ESC>:%s,aa:bb:cc:dd,ring12,g
Where <ESC> is the escape key to put vi in command mode.

Resources