vim: search and replace for "&" - 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 "\".

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
^

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 replace ":" in a pattern without removing all ":"?

I've got this python-like text:
if a==0:
print ok
[1:]xy
I wish to delete all "[1:]" content. So I tried this:
%s/[1:]//g
Unfortunately, the first line is changed to be
if a==0
The ":" was eliminated, not as my expectation. So how should I do?
Vim search uses regular expressions to match, [ and ] are special characters in regex to match sets of characters.
[1:] means 1 or :. You need to escape the brackets like: %s/\[1:\]//g
Escape square brackets, like this:
%s/\[1:\]//g

vim - how to find lines with specified amount of specified characters

I have 100k lines file and some lines has unescaped apostrophe, like:
""Luis" number 4"
I want:
"\"Luis\" number 4"
so how can I find all lines having more than 2 " character. is it possible to do it easly in vim ? otherwise it is for simple script task.
If you just want to find such lines you can search as follows:
/.*".*".*".*
where .* will match zero or more characters and " is the literal quote you're looking for.
The first and last .* patterns aren't strictly necessary, but they make sure the whole line is highlighted when a search is found.
You can try
:%s/""\([^"]*\)"\(.*\)"/"\\"\1\\"\2"/
%s/\v"(.*)"(.*)"(.*)"/"\1\\"\2\\"\3"/
Breaking this down:
%s: Substitute on every line
\v: Very magic (don't have to escape all those parens in regex)
"(.*)"(.*)"(.*)": Anything in quotes anywhere inside anything in quotes
And replace with a quote, everything up to the opening inner quote (\1), an escaped quote, everything inside the inner quotes (\2), another escaped quote, then the rest (\3) and a close quote.

Simple substitution

How do I substitute &&&&&&&&& with &? I have tried :%s/&&&&&&&&&/&/gbut i only get more &.
Because & is special in the replacement part (it means "the whole matched string"), you have to escape it:
:%s/&&&&&&&&&/\&/g
(Note the backslash before & in the replacement part.)
You must escape & in the replacement section. Unescaped, & refers to the whole match.
:%s/&&&&&&&&&/\&/g
You need to escape & with \ in replace string
:%s/&&&&&&&&&/\&/g
In replace string, & has a special meaning and contains matching string.
Therefore in you case, you are replacing nothing but match itself hence no change.
If your intension is to replace multiple & with a single one, then try following
:%s/&\+/\&/g
You need a quantifier in the expression:
:%s/&\+/&/g
not sure if it works like that in vim, it is default regex, the plus sign tells it should capture any combination of one or multiple '&' signs, using &{9} should find exactly nine

VIM search for characters starting with ampersand

I have to search for something like &variable (and replace with &variable.test). I could just do \&variable, but vim also shows me all characters starting with &variable (&variableXXX for instance), which I do not want. I thought this - /\<\&variable\> would work, please let me know where am I going wrong.
The first problem is that \& is not a literal & its a special regex match that matches branches in vim. To match a literal & just use &.
The second problem is that \< matches the beginning of a keyword by looking to see if the next character is a keyword. However & is not normally a keyword so it always fails.
To get around this you can make & a keyword by doing
set iskeyword+=&
Then you can use the regex \<&variable\> to match the things you want.
Relevant help pages :h iskeyword, :h /\< and :h \&
Short answer: use /&variable\>.
You don't want the \ in front of the &. See help \&.
Your \< is in the wrong place -- it belongs after the &, not before, since & is not a keyword character (:help keyword).
In fact, you don't need the \< at all. You could search for &variable\> and you would find &variable but not &variableXXX. You don't have to use \< just because you're using \>.

Resources