1

I want to search and replace a string in vim with preserving the variable part of string. for example:

StringA("variable1")
StringA("variable2")
StringA("variable3")
StringA("variable4")

replace this with:

StringA("variable1", new_constant_string)
StringA("variable2", new_constant_string)
StringA("variable3", new_constant_string)
StringA("variable4", new_constant_string)

What i want to do is search for

s/StringA(*)

and replace it with

        s/StringA(*)/StringA(*, new_constant_string)

where (*= variable1,variable2,variable3,variable4 and is preserved)

1
  • Do you want to preserve the double quotes or not? Commented May 17, 2017 at 2:07

2 Answers 2

3

You can also filter the lines using g and then apply s

:g/StringA(/s/)$/, new_constant_string)/
  • :g/StringA(/ all lines containing StringA(
  • s/)$/, new_constant_string)/ substitute ) at end of line with , new_constant_string)
    • remove $ is there can be characters after ) in the line
Sign up to request clarification or add additional context in comments.

Comments

2

Another method:

:%s/StringA(".\{-}"/&, new_constant_string

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.