vimtricks.wiki Concise Vim tricks, one at a time.

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

  1. qa — start recording
  2. f, — find the comma
  3. l — move past the comma
  4. dt, or dt) — delete to the next delimiter
  5. F( — find backward to the opening paren
  6. p — paste after the paren
  7. q — 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

Next

How do you yank a single word into a named register?