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

How do I apply different macros to alternate lines?

Answer

qa ... j@bj q

Explanation

You can create macros that call other macros, applying different operations to alternate lines or creating complex editing patterns.

How it works

  • Record macro b for one type of line
  • Record macro a that processes the current line, moves down, calls @b, moves down
  • This alternates between two different operations

Example

Alternating header and detail formatting:

qb I  <Esc>q          " macro b: indent
qa I## <Esc>j@bjq      " macro a: add header, move down, indent next, move down

Tips

  • Macros can call any other macro
  • This is useful for processing structured data with alternating formats
  • Be careful of infinite recursion when macros call each other
  • Test each component macro independently first

Next

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