gk12345
gk12345

Reputation: 416

Vim search and replace to amend a string with common structure but varying words

I'm fairly new to Vim and I haven't been able to find on this site how to search and replace with a varying part of a string. I need to apply a global edit to all times "SetTag("...")" appears with ... being any word. My edit is to add one more word after the second quotation mark. example: SetTag("err" + __LINE__ with the bolded part being what I need to add. Can anyone let me know how this is possible with a vim search command? Thanks!

Upvotes: 0

Views: 108

Answers (1)

L. Scott Johnson
L. Scott Johnson

Reputation: 4382

nb: I assume "word" is any sequence of characters other than a doublequote character. Modify as needed.

:%s/SetTag("\([^"]*\)")/SetTag("\1" + __LINE__)/

the escaped parentheses grab the sub-match; the \1 in the replacement string is replaced by that sub-match.

Upvotes: 1

Related Questions