Daniel Kaplan
Daniel Kaplan

Reputation: 67514

Why isn't vim replacing my text the way I expect?

I have this string in my document:

subject.reset();

I am running this regex:

:%s/reset\(\)/doStuff\(\)/g

And the result looks like this:

subject.doStuff()();

Where did that extra parenthesis pair come from? I wanted it to look like this:

subject.doStuff();

How do I do that search and replace in vim?

Upvotes: 1

Views: 75

Answers (1)

rock321987
rock321987

Reputation: 11042

You don't need to escape () inside vim. Try

:%s/reset()/doStuff()/g

or

:%s/reset/doStuff/g

\( or \) denotes capturing group inside vim. See here about capturing group.

In your example

reset\(\) actually means that you are replacing reset and a capturing group(which is empty and it does not really contain the parenthesis you intended). So, basically you are replacing only reset with doStuff()..

subject.doStuff()();
         ^^      ^^
         ||    (was already here after reset and not replaced)
     reset is replaced with do Stuff()

The parenthesis after reset is still there

Upvotes: 3

Related Questions