15

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

5 Answers 5

9
:%s/\$data\[\'user\'\]/$data['sessionUser']/g

I did not test this, but I guess it should work.

1
  • 2
    You don't need the escapes in the replace part. Dec 16, 2011 at 18:26
8

Here's a list of all special search characters you need to escape in Vim: `^$.*[~)+/\

6
  • Thanks for this concise piece of knowledge!
    – Math1000
    Jan 31, 2020 at 9:22
  • You need to escape ) and + too
    – Rol
    Feb 13, 2021 at 14:42
  • 2
    as well as /.
    – Christian
    Mar 5, 2021 at 9:57
  • What you might miss, at least I ran into this: < and > like in xml tags do not need to be escaped. Jan 16 at 22:36
  • Why do I need to escape + character when using a regex in vim? I supposed I need to escape only when looking for +
    – Asmoox
    Apr 3 at 10:13
4

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.

3

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).

1
  • Interesting information about the second //. Thanks for it.
    – Falassion
    Dec 16, 2011 at 18:31
1

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.

1
  • I was really looking for an easier option for searching. I'll get to know more about the built in search and replace, but it's probable that I'll try one of those you showed me. Thanks.
    – Falassion
    Dec 16, 2011 at 18:32

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.