How to Record Vim Macros in Code Files to Automate Processes
Learn how to create macros in Vim to automate processes and help save you time as a developer on repetitive tasks. For the demo I walk through how to efficiently style CSS and HTML code.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

As we continue our journey on learning VIM, I want to now introduce you to the concept of how to build your own macros. Now if you've never heard of a macro they were originally popularized in some old software such as Excel and some different business applications like that.

And essentially what a macro allows you to do is to record behavior and then replay it as many times as you need in order to duplicate that same kind of behavior. So I think the best way of examining what macros are. Is actually looking at some examples.

So here we have a HTML file, and this is one spot where I will use macros quite a bit. And it is to duplicate behavior with certain things like tags or styles or anything like that. And I'm gonna start off with a CSS file, so right up here you can see that we have a CSS file that has all of the styles all scrunched together.

large

So imagine that you got this off of a website, copied it in and it works but you want to fix the structure on it. So the way that you can do this is to first, go and put the color on a new line here. And the next thing that I want to do is to actually start the record process.

So the way that recording works in VIM, is you're gonna start with the letter Q. And make sure that you are exactly in the spot that you want all of this to happen, because VIM is very good at following orders, it's not good at kind of picturing in its mind what you wanted. So you have to give it exactly the very most explicit orders possible, cause all its gonna do is copy the commands that you ran.

So here, I start with my cursor on "color", and I'm going to type in the command "Q", and you can see on the bottom right hand side there where is says "Q", and that is the reserved key word in keybinding for recording macros.

large

And then we have to give the macro a name so that we can call it later. So in this case I'm gonna do "Q" and "R" and from here you can see on the bottom left hand side where it says it's recording at "R".

large

And all I want to do here is I want to skip ahead to each semicolon and then hit a return. So the way I can do that is I'm gonna switch into insert mode and I am going to, or I'm sorry, before I switch into insert mode, I'm gonna go into the spot where I want. So I'm gonna type "F" followed by the semicolon, and then I'm gonna use "L" one spot to the right. Switch into insert mode and then move it onto the next line. So if I hit escape, the way I can end my recording is by typing in "Q" once more.

So now we have a recording that is complete. So start off with your cursor in that same spot, and then you can repeat this as many times as you want. You can see that we want to do this once, twice, three times, and I could even do a fourth one there, but lets see what three does for us. So the way that you run a macro is you don't start with a command, so don't do something like colon, or anything like that.

You want to actually just start typing the number of times you want to do this. So three, followed by the "@" symbol, the "@" symbol is how you call a macro, and then whatever you used when you set the macro. So we used "R" and as you can see, that did it perfectly.

large

So right there, we were able to go and run it. It ran through three times and it went and styled it exactly the way we wanted. Now we can also do this just one time. So if I want to do "@" followed by "R". You can see that it went and it did the very final one, and it put our curly brace at the very end. So that's a pretty cool thing, cause as you can see this would have taken quite a bit more time, and this is kind of a contrived example, you know you have one of these. It may not be worth it to create a macro specifically just for this.

However, imagine that you grabbed over a huge CSS file and you want to perform the same type of behavior over and over and over again, recording macros is a good way of doing that. Now let me show you one other way, this is one that I use a lot, which is to add bullet points.

So imagine that you have a set of a unordered list, just like we have right here.

large

And we need to be able to have all of these items populated with li tags in front of them and behind them. So the way that you can do this is first start from wherever you are going to start from. And then type in "Q" followed by whatever you want to bind this to, so I'm gonna say "Q" "L", you can call yours whatever you want and this is gonna start a recording of at "L".

Now switch into insert mode and just start typing in exactly what I want. So I want to add a li tag before and then I'm going to switch and add this at the end. And now I'm going to hit escape, navigate down exactly to where I want to be. If I don't do this it'll just keep on adding it on the same line, which is not what I want.

So now I can just type "Q" again, and we have five more that we want to do this to. So I'm gonna type in "5" "@" and then I bound this to "L" so I'm gonna say "5" "@" "L". And you can see it goes, and it populates each one of those.

large

So that is a few different examples of how you can implement macros inside of VIM.