This post is about how to increment and paste a counter while executing an emacs macro. For some reason, this is not easy to find on the Internet. So I'll write it down on my blog since I keep forgetting.


  1. Set the start point of the counter
    M-: (setq i 1)
  2. Start recording macro with
    C-x (
  3. Insert index as needed with
    C-u M-: i
  4. Increment index when finished with macro
    M-: (setq i (+ i 1))
  5. Complete macro with
    C-x )
  6. Execute Macro with
    C-x e
To execute the macro multiple times, use ALT-(# of times) C-x e.