The Power of Recursive Macros in Vim

~ 2 minute

If for some crazy reason you’re not already a user of Vim, shutdown your computer and go think about your life choices.

Joking aside, Vim is really a great editor. And if you didn’t know – Vim supports macros.

Basics of macros in Vim

Macros represent a simple concept which can be described as “record the sequence of my actions, save them, and anytime I need them again, execute them.”

This is probably the most underused Vim feature which can improve your productivity dramatically. You can do all sorts of amazing stuff with your code using macros.

Incredibly powerful. Here’s a simple example:

What’s happening here:

  • qa – start recording macro in register a
  • i<Tab>' – enter Insert mode, insert Tab and 
  • Esc – get back to Normal mode (so I can run next command)
  • AAppend command, which places cursor at the end of the current line, in Insert mode
  • ', – Insert ',
  • Esc – get back to Normal mode again
  • j – Go down one line
  • ^ – Go to the start of the current line
  • q – Stop recording the macro

Then, using command @a I run the macro on the current line. After that, I can just hit @@ to run the macro I previously run.

Recursive macros

Using macros can be even more effective with recursion. Recursive macros are especially useful when you need to act on many lines in a file.

In order to record a recursive macro, you need to start with an empty register. You can make the register a empty by hitting qaq.

Now, let’s see recursive macro in action, with a slightly different example:

 

Let’s see what’s going on here:

  • qa followed by Tab and ' followed by Esc – same as in first example
  • f: – find the first occurrence of : and place cursor on it
  • C – command to delete everything from cursor to the end of the line, and enter Insert mode
  • ', followed by Escj and ^ – same as in the first example: insert ', get to Normal mode, move one line below and jump to beginning
  • @a – this is the key step: while recording a macro in register a, we call it inside itself!
  • q – stop recording the macro
  • @a – now we run the recursive macro – and there you go – magic! :)

 

There’s so much more you can do with macros in Vim!

My colleagues sometimes stare at my screen and wonder wtf is going on, when my hands are even not on the keyboard – and my code is being edited by a macro :D

You can master this magic too!

Get Macros chapter (plus three other!) from my book Mastering Vim Quickly for free.

Get 4 chapters for Free!


If you liked the post, we should get connected - follow me on Twitter

14 Comments The Power of Recursive Macros in Vim

  1. Lucio

    Thanks! I use macros a lot and never knew you could do that. But how does that macro know when to stop, in your example?

    Reply
    1. jole

      There’s no another `:` to jump to on the next line (after the last one edited by macro). That’s exactly why I used f: in this example.

      Reply
  2. Manuel

    Cool! What I didn’t understand is how vim knows when to stop running that macro. I mean, in the example there is more lines below the list of IPs but they keep untouched.

    Reply
  3. Judd Maltin

    Nice trick.

    Why not use @a to run the macro exactly the number of times your need it without leaving stray characters when done?

    Or start the macro with a more precise find?

    Reply
    1. jole

      Those are the valid available options. Sometimes, recursive macro is the best way to go, and many people don’t know about it. So that’s why this post ;)

      Reply
  4. Sammy

    Nice!

    Another neat trick is to use your first macro, then visually select the rows that you would like to change (e.g. Vap or similar) then type :normal @a to run the macro on all lines in your selection.

    Reply
  5. Gustavo Passos

    I’ve been learning Vim for less than a month, and I am trying to gather all the useful information I can get, and this was great!
    I’m planning to make it my main text editor and It really seems like anything can be done with it.
    Thanks!

    Reply
  6. Lance Alligood

    Nice tutorial. I find the magic of recursive macros to be super powerful–especially when you have a very large (but not necessarily known) number of changes that need to be made.

    Based on your example of framing the IP address in single quotes, you may want to check out Tim Pope’s vim-surround plugin, which simplifies the process of framing a block of text with single/double quotes, parens, brackets, curly braces, & even HTML/XML tags. It’s one of those plugins (although I’m generally not a fan of vim plugins) that’s so useful & intuitive that I can’t believe that it hasn’t been rolled into the Vim codebase itself.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *