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
Related
We have many \foo{something}. As the string inside the curly braces contains odd number of character, we want to add a space after the string. The result will be \foo{something }. We only want odd number of characters only. For example, in \foo{string} the string inside the curly braces has even number of character, then we do not add a space.
What I have tried:
a dot means a character, two dots means two characters
asterisk means repeat 0 or more times
Then, [..]*. means 1, 3, 5 ... characters.
Then, \foo{[..]*.} will be my matching sequence.
Then, :%s/\foo{[..]*.}/\foo{[..]*. }/g will add the space we want.
But we failed.
This cmd should do:
:%s/\\foo{\zs[^}]\+\ze}/\=substitute(submatch(0), '$', len(submatch(0))%2?' ':'','g')/
:%s/\\\w\+{\([^}]\{2}\)*[^}]\zs\ze}/ /g
Explanation:
Find pairs of characters (\([^}]\{2}\)*) followed by another character ([^}]) in between a macro \\\w\+{...}. Then do a substitution adding an additional space.
Glory of details:
\\\w\+{...} find macros of the pattern \foo{...}
Look for character that does not match an ending curly brace, [^}]
Look for pairs of non-}'s, \([^}]\{2}\)*
Find an odd length string by finding pairs and then finding one more, \([^}]\{2}\)*[^}]
Use \zs and \ze to set where the start and end of the match
Use a space in the replacement portion of the substitution to add additional space and thereby making the string even in length
Fore more help see:
:h :s
:h :range
:h /\[]
:h /\(
:h /\zs
:h /\w
:h /star
:h /\+
This should work
:%s/\v[(\{(\w{2})*) ]#<!}/ }/g
break down:
%s " run substitute on every line
\v " very magic mode
[(\{(\w{2})*) ] " matches every `{` followed by an equal number of word
characters and space, since you don't want to add a space again everytime
you run it.
#<!} " negative lookahead, matches all } not following the pattern before
Update
To solve the precise problem as you can read in the comments, the following command helps.
g/^\\foo/s/\v(\{([^}]{2})*)#<!}/ }/g
Changes:
g/^\\foo/... " only run it on lines starting with \foo
[^}] " it does now match all characters between { and } not only word
characters.
Pitfalls: won't work correct with multiple braces on the line ( \foo{"hello{}"} and \foo{string} {here it would also match} f.e. will fail). if one of these is a problem. just tell me
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.
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
^
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.
I have a file containing string like this one :
print $hash_xml->{'div'}{'div'}{'div'}[1]...
I want to replace {'div'}{'div'}{'div'}[1] by something else.
So I tried
%s/{'div'}{'div'}{'div'}[1]/by something else/gc
The strings were not found. I though I had to escape the {,},[ and ]
Still string not found.
So I tried to search a single { and it found them.
Then I tried to search {'div'}{'div'}{'div'} and it found it again.
Then {'div'}{'div'}{'div'}[1 was still found.
To find {'div'}{'div'}{'div'}[1]
I had to use %s/{'div'}{'div'}{'div'}[1\]
Why ?
vim 7.3 on Linux
The [] are used in regular expressions to wrap a range of acceptable characters.
When both are supplied unescaped, vim is treating the search string as a regex.
So when you leave it out, or escape the final character, vim cannot interpret a single bracket in a regex context, so does a literal search (basically the best it can do given the search string).
Personally, I would escape the opening and closing square brace to ensure that the meaning is clear.
That's because the [ and ] characters are used to build the search pattern.
See :h pattern and use the help file pattern.txt to try the following experiment:
Searching for the "[9-0]" pattern (without quotes) using /[0-9] will match every digit from 0 to 9 individually (see :h \[)
Now, if you try /\[0-9] or /[0-9\] you will match the whole pattern: a zero, an hyphen and a nine inside square brackets. That's because when you escape one of [ or ] the operator [*] ceases to exist.
Using your search pattern, /{'div'}{'div'}{'div'}[1\] and /{'div'}{'div'}{'div'}\[1] should match the same pattern which is the one you want, while /{'div'}{'div'}{'div'}[1] matches the string {'div'}{'div'}{'div'}1.
In order to avoid being caught by these special characters in regular expressions, you can try using the very magic flag.
E.g.:
:%s/\V{'div'}[1]/replacement/
Notice the \V flag at the beginning of the line.
Because the square brackets mean that vim thinks you're looking for any of the characters inside. This is known as a 'character class'. By escaping either of the square brackets it lets vim know that you're looking for the literal square string ending with '[1]'.
Ideally you should write your expression as:
%s/{'div'}{'div'}{'div'}\[1\]/replacement string/
to ensure that the meaning is completely clear.