0

I have the following lines in my text file:

.abc(),
.def(),
.ghi(),
.jkl(),

and I'd like to transform them into this:

.abc(abc),
.def(def),
.ghi(ghi),
.jkl(jkl),

I've tried :%s/\.\(\w\+\)(\),/.\1(\1),/g but it won't take.

1
  • 1
    Put more effort in your questions. Your poor formatting lead the first responder on a false trail. Commented Mar 22, 2023 at 6:10

1 Answer 1

2

The problem is this backslash:

:%s/\.\(\w\+\)(\),/.\1(\1),/g
               ^

which is ambiguous as the capture group has already been closed. Because of that, your patten doesn't match and your substitution does nothing.

Removing it makes your substitution work:

:%s/\.\(\w\+\)(),/.\1(\1),/g

Now, the complexity of that substitution may be required by the actual content of the actual file but it doesn't fit your sample.

  • The parts that don't change before and after the parts that change can be removed:

    :%s/\(\w\+\)(/\1(\1,/g
    
  • Since there is only one match on each line, the /g is useless:

    :%s/\(\w\+\)(/\1(\1
    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.