Thursday, October 14, 2010

vim: how to record and replay macros

To record a macro:
  1. Start recording: press ‘q’
  2. Choose a macro register: press ‘a’ to select ‘a’ as a location to save the macro to. You will see “recording” at the bottom left of the vim window.
  3. Perform editing actions: for example, suppose you want to delete any line containing the string, “Stage:” You can do this by pressing:
    Esc
    /Stage:
    dd
    

  4. Stop recording: press ‘q’
To replay a macro:
  1. Choose a macro register: In our case, we want the macro we just saved to register ‘a’.
  2. Repeat the saved macro: by pressing “@[register_name]” which in our case is:
    @a

  3. Multiple-repeat: press “[count]@[register_name]”, for example:
    8@a

No comments: