Macros in Vim can be a great time saver, especially for complex and repetitive line-editing tasks. A macro is a way of recording a series of actions as a single command. I find this especially useful when I need to make some formatting changes to a set of lines. This is perhaps best illustrated by an example, but prior to that let us quickly look at the syntax for recording and using macros in Vim.

Recording a macro is split into three steps:

  1. q followed by a letter to start recording. The letter can be any valid register of your choice, e.g. m. If successful you will see recording in the bottom left of the screen.
  2. Carry out whichever actions you require. This isn’t constrained to just one line of actions, though often this is practical.
  3. Press q again to stop recording.

Using a macro is simply a case of typing @ followed by the register character chosen, m in the previous example.

A brief example

An example may make the benefits of macros clearer. Let’s say that we have the following data in a file and we want to transform this into a CSV file. This chunk of data is from a wireless sensor network log file that really did need converting to CSV.

StandDev (ToF): 2260ps, Mean (ToF): 64037ps, Errors: 0
StandDev (ToF): 2265ps, Mean (ToF): 66037ps, Errors: 0
StandDev (ToF): 2184ps, Mean (ToF): 82858ps, Errors: 1
StandDev (ToF): 3184ps, Mean (ToF): 82860ps, Errors: 1
StandDev (ToF): 1439ps, Mean (ToF): 55893ps, Errors: 0
StandDev (ToF): 2439ps, Mean (ToF): 75893ps, Errors: 3

We can change this to form part of a CSV file with a quick macro:

  • Move to the first line we wish to edit (remember, the macro will capture all commands, so if you execute a move command in your macro it will happen on each run).
  • Start recording, qm
  • Execute our edit steps. This can be done in many ways. The example command sequence below prepares a single line by: moving to the start of the line, systematically removing the words and units, adding a trailing comma.

      ^2df fpxxf 2df fpxxf df A,^[j
    • ^[ is the ESCAPE key
    • Note the move down to a new line at then end, j. We don’t have to use this approach, alternatives include starting our macro with a search, or using the normal command to run the macro on a selection of lines.
  • Stop recording, q

With our macro recorded we can now easily run this on the remaining lines, in our case five times, starting from the first unedited line using 5@m. As with many efficient uses of Vim, the power in macros really becomes apparent for cases where the task is very repetitive.

Macros are a good way of making you think about the commands you are using. This becomes more apparent when working on files that are varied. The example above was originally taken from a log file that had many other lines of different data, some already in the correct format. To get to a relevant line for the macro to operate on I included a search for ‘StandDev’ at the start of the recorded command sequence.

Remember, @ simply treats the proceeding register as a list of commands. This means a register used for a macro can be viewed and edited in the normal ways. This provides a convenient way to fix any mistakes made during the recording of a macro, by editing the register. The Macros page at wikia has a good explanation of this, notably including a section on saving macros — really useful if you know you will need your finely-crafted macro for future files.

Hopefully this post has shown the advantages macros offer in Vim, especially for those that do a lot of one-time text transformations.

Guest post by Martin Falkus (@mfalkus).