Tutorial for Vim: Recording Macros


Tutorial for Vim: Recording macros

I put together a video tutorial describing how to perform a simple code cleanup task using a Vim macro (this also works for vi macros):

The basics boil down to:

  1. The q command is the record macro command. Press it then press another key to choose a vim register to record into.
  2. Think about a repeatable sequence of actions that would accomplish your task. Generally this needs to be length-independent (i.e. use commands that will work no matter what the line length is), so commands like f, %, and the like are your friends.
  3. Use the @ command to play back your macro. It expects the name of the register you chose in step 1.
  4. Prefix the @ command with a number to repeat the chosen task that number of times.

The video covers an extremely simple use case for vim macros and goes into a bit more detail than this post. It’s a very powerful mechanism that can be extended far beyond what I’m illustrating here. If you have a better way to accomplish the code cleanup task I demonstrate in the video, please contribute!

Thanks for reading.