Suppose I want to create a macro that swaps two function parameters.

For example, I search for swap:

swap(first, second);

And with the cursor pointing at the 's' in swap, I can search forward for the paren, move one to the right, delete everything up to the comma, move back to the (, use '%' to go to the ending ')', add another comma, paste the first parameter, then go back to the swap, and delete the comma that used to separate the first and second parameter.

h%i, <escape>

(And I have made a quick-and-dirty macro for this.)

This works fine as long as the first parameter doesn't have parentheses in it. For example, this:

swap(foo, func(2, 3));


swap( func(2, 3), foo);

However this:

swap(func(2, 3), foo);


swap( 3, func(2), foo);

Because the parens in the first argument confuse the macro; it doesn't realize it should ignore the first comma because it's inside parens.

Anyway, my question is, is there a way to search for the next "," that isn't inside of parens? For example, one way would be to search for a regex [,(] that matches either a comma or opening paren; then if it found a paren, use % to skip forward to the matching closing paren, and then search forward again. Of course, that would require being able to conditionally loop, which is not really available within a macro (right?)

Or possibly, is there a way to search for a "," after a sequence containing equal numbers of right and left parens?

I was just hoping that something like this exists, given that % exists.

1 Answer 1


You'll need plugin assistance for this -- because correctly extracting parameter boundaries is not trivial and cannot be done (perfectly) only with regex.

So far I'm aware of two plugins. There is mine (shipped with lh-cpp), and I've no doubt link(s) to the other one(s) will be given to you as well.


Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.