Simple substitution - vim

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

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
/\/\*

Replace String in Bash with Specific Condition

If I have string:
path1=/path/me & you/file.json&path2=/path/you & me/file.txt
I expect the output to be like this:
path1=/path/me & you/file.json;path2=/path/you & me/file.txt
I need to replace & that it's front and back not contain space, I tried with sed but I keep got this
path1=/path/me ; you/file.json;path2=/path/you ; me/file.txt
You can use [^ ] to match a non-space character and make sure it's in a \(capture group\) so that you can reference it in the replacement string:
sed -e 's/\([^ ]\)&\([^ ]\)/\1;\2/'
This finds any three character sequence of non-space & non-space and replaces it just the two captured characters, effectively replacing any & without a space next to it.
This will affect foo&bar but not foo & bar or foo& bar or &foo
I'm guessing maybe you are looking for
sed 's/&\([a-z][a-z0-9]*=\)/;\1/g'
i.e. replace only semicolons which are immediately followed by a token and an equals sign. (You may have to adjust the token definition, depending on what your tokens can look like. For example, underscore is often supported, but rarely used. You mght want to support uppercase, too.)
If at all possible, fix the process which produces these values, as the format is inherently ambiguous.
sed -r 's/(\S)&(\S)/\1;\2/g'
Where -r enable regex and \S is all except spaces.

vim how to replace backslash and &

I am using vim and I want to replace
&\\ \hline to \\ \hline
Can somebody teach me how to do this?
%s/\&\\/\\/gc didn't work.
this is for latex table.
Inside the search pattern, you don't need to escape & (\& is a special atom for branches), but \ needs to be doubled. In the replacement part, a & would have to be escaped, just like the \ (yes, this is a bit complex, unfortunately). So, this would work:
:%s/&\\\\/\\\\/gc
If you want to assert the following \hline, too, it's simpler to end the matching with \ze (but still assert that the following part is there); this avoids having to duplicate the part that should be kept (or alternatively capture and re-insert it in the replacement):
:%s/&\ze\\\\ \\hline//gc

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 Search/replace: what do I need to escape?

I'm trying to search and replace $data['user'] for $data['sessionUser'].
However, no matter what search string I use, I always get a "pattern not found" as the result of it.
So, what would be the correct search string? Do I need to escape any of these characters?
:%s/$data['user']/$data['sessionUser']/g
:%s/\$data\[\'user\'\]/$data['sessionUser']/g
I did not test this, but I guess it should work.
Here's a list of all special search characters you need to escape in Vim: `^$.*[~)+/
There's nothing wrong with with the answers given, but you can do this:
:%s/$data\['\zsuser\ze']/sessionUser/g
\zs and \ze can be used to delimit the part of the match that is affected by the replacement.
You don't need to escape the $ since it's the at the start of the pattern and can't match an EOL here. And you don't need to escape the ] since it doesn't have a matching starting [. However there's certainly no harm in escaping these characters if you can't remember all the rules. See :help pattern.txt for the full details, but don't try to digest it all in one go!
If you want to get fancy, you can do:
:%s/$data\['\zsuser\ze']/session\u&/g
& refers to the entire matched text (delimited by \zs and \ze if present), so it becomes 'user' in this case. The \u when used in a replacement string makes the next character upper-case. I hope this helps.
Search and replace in vim is almost identical to sed, so use the same escapes as you would with that:
:%s/\$data\['user'\]/$data['session']/g
Note that you only really need to escape special characters in the search part (the part between the first set of //s). The only character you need to escape in the replace part is the escape character \ itself (which you're not using here).
The [ char has a meaning in regex. It stands for character ranges. The $ char has a meaning too. It stands for end-line anchor. So you have to escape a lot of things. I suggest you to try a little plugin like this or this one and use a visual search.

Resources