tags:

views:

79

answers:

1

I've got a string:

doCall(valA, val.valB);

Using a regex in GVIM I would like to change this to:

valA = doCall(valA, val.valB);

How would I go about doing this? I use %s for basic regex search and replace in GVIM, but this a bit different from my normal usages.

Thanks

+1  A: 

You can use this:

%s/\vdoCall\(<(\w*)>,/\1 = doCall(\1,/

\v enables “more magic” in regular expressions – not strictly necessary here but I usually use it to make the expressions simpler. <…> matches word boundaries and the in-between part matches the first parameter and puts it in the first capture group. The replacement uses \1 to access that capture group and insert into the right two places.

Konrad Rudolph
My actual use case was subtly different doCall(valA, val.valB);- your answer above is working great for my initial question, but not with the extra . it's not working.
André Vermeulen
@André: It definitely should work, unless the dot appears in the *first* argument. In that case you need to adapt `\w` accordingly, e.g. write `\w|\.` to also allow a dot, **and** remove the less-than and greater-than signs.
Konrad Rudolph
Oh yes it's working fine - thanks! Will need to study this piece of magic now...
André Vermeulen