Editing ViM Macros

March 17, 2007 at 03:16 PM | categories: tips, vim | View Comments

At the BYU UUG meeting this week, Peter mentioned a great ViM trick that I've never seen before. Here's my feeble attempt to document it.

Because ViM macros are stored in registers they can be edited. This means that if you create a long macro and then realize that you forgot to send the cursor to the beginning of the line before finishing the recording, you don't have to create the entire macro again, you can just add the motion command. You use it like this:

  1. Start recording your macro by typing q and then the single character ([0-9a-zA-Z"] are allowed). The single character is the register your macro will be stored in.
  2. Enter the commands you want included in the macro.
  3. Type q to finish recording the macro.
  4. At this point you could run the macro by using @ and the register name.
  5. To modify the macro, move to a blank line and type "Rp replacing R with the register name of your macro. This will paste all the commands in your macro to the current line.
  6. After making the changes you need, save the macro by typing 0"Ry$ replacing R with the register name you want to use for the macro. The register name does not have to be the same as the original.