How do I record a macro to swap two function arguments?
Answer
qa f,ldt,F(p q
Explanation
This macro swaps two comma-separated arguments inside parentheses by cutting the second argument and placing it before the first.
How it works
qa— start recordingf,— find the commal— move past the commadt,ordt)— delete to the next delimiterF(— find backward to the opening parenp— paste after the parenq— stop recording
Example
func(second, first)
Becoming:
func(first, second)
Tips
- This works for simple two-argument functions
- For more complex cases, use visual selection
- Text objects like
i(help with nested parentheses - The vim-exchange plugin provides more robust swapping