Vim Search/replace: what do I need to escape?

:%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.

Tags:

Vim

Replace