Check out a free preview of the full VIM Fundamentals course

The "Macros" Lesson is part of the full, VIM Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

ThePrimeagen live codes how to use macros to record a set of keystrokes and replay them for quick code repetition and editing large chunks of similar code. How to jump to the beginning of the line is also covered in this segment.


Transcript from the "Macros" Lesson

>> So let's do macro. So there's a thing called macro pressure. It's a real thing. This is not a joke. Okay, this is a PSA, we're doing macro pressure. What that is when you use vim, you'll eventually get really good at using vim. But here's the problem, that the moment you start recording a macro, you will forget everything you know, and it'll feel like your fingers are filled with cement.

It's the craziest thing ever. It's called macro pressure. It's like stage fright, sort of. So, let's go over here and let's download this little example time, alright, so here we go. Also I do remember my first so I actually before we do that I do want to go over this I did put a little cue for me to remember this.

I remember seeing my first macro ever done, and it blew my mind. So let me show you it, I still remember to this day, so I want to just do test dot js, right? And what I'm gonna do is I'm gonna go like this, we're gonna start, we're gonna create an array.

Alright, let's just create an opening array. And I wanna go 1, 2, 3, 4, 5 I wanna just manually type out the letters. I know you can do the mappings and all that, but let's just pretend we couldn't do it for a moment, and we wanna go like this 1, 2, 3, 4, 5 all the way up to some number.

Like this would be very annoying if you had to manually type it all out, right? So we're gonna do a macro. So what we're gonna do, is I'm going to press Q. Now Q will put you into macro mode. Then I'm gonna type any letter I want, a, now notice what it says down there.

Macro pressure has now started, all of a sudden I feel like I'm gonna mess this up because now it's recording every one of my keystrokes. So what I'm gonna do is I'm gonna highlight this line with highlight line, I'm going to yank it. I'm going to paste it and I'm going to press Ctrl A, Ctrl A will increment your number if you're in T mux you have to press it twice if you have a T mux remap on a I'm in T mux I have a remap on Ctrl A so I press it twice.

But in regular vim Ctrl A will auto increment any number that is the first one that it sees in a line. It's a fantastic feature, I use it all the time. It's crazy. So now I'll have to do is press Q one more time. I'm done. Okay, we made it.

I pressed it, I did it the right way. So now if I press add sign, I press A, it yanked, it control A, awesome, I can do it again and again and again and again and again. I can do 100 at a and it will go and it will do it 100 times.

Hopefully there's a few smiles out there like hey, wait a second, that's kind of cool. Like I can record a set of keystrokes and replay them, that means I can get pretty generalized here. So that remember when someone earlier asked about, multiple cursors, this is kind of where I'm saying, hey, maybe multiple cursors isn't quite what you wanted.

We'll find out, because motions have a lot more power than simply editing text, they can do a lot more items. Alright, that was really cool. That was my first macro experience and when I saw that I don't know personally I'd just blew my mind I got so like giddy excited about it.

Alright, so I'm just gonna quit that file. And now, let's cut and paste this file, send it down. Let's go forward. Yeah, thank you. I said wow, too. I thought it was awesome also, just now that person said it too. All right, so let's curl this file down and let's do some macros.

This is gonna be tons of fun, all right. Also everyone, hopefully we'll get macro pressure, and everybody will screw it up. It'll be fantastic. All right, so what do we wanna do? Yes, also, I did write this code with a macro. I wrote all this code with the macro and just made it play itself.

So what do we wanna do? All right, what I wanna do is I want to take this code and I wanna convert it into a switch statement. Right, well. Unfortunately, there is just like a lot of things to do here, right? We could use a find and replace, right?

I could do a star and kind of figure that out, match everything say up to the first double quotes, group it, match it up to the second double quote. Match everything afterwards, replace it with a k statement, get that report in double quotes, put the colon on there, you got it right.

So we could do that with a case statement, but let's do it with the macro instead. Actually here I'll just show you really quickly how to deal with with finding replace. Sometimes it's just easier. So I'll just do a dot star match the whole line. I'm going to go all the way up to a quote.

Remember regex is a greedy or the opposite agree lazy, I'm not really sure what the right word is. They keep matching until they can't match any longer. So they're definitely not greedy. But they don't feel very lazy either. They just keep going. I'm not sure what the right term is.

I know there's a term and I forgotten it. Do that one I curvy that I always tell you about and there we go create a match group, then I'm gonna do another quote, awesome. And then I'm gonna just match the rest of the line, and then I'm gonna replace it with case that there we go, by doing this, boom, we got them all.

Awesome, there now alternative case statements when they were just if statements, but for the sake of this, let's not do that. Let's use macros. It's kind of like, let's see, no, what is it? This is kind of a trivial case, but it's just more of an exercise that we can all do together.

All right, so now we need to do this. What are we gonna do? What's the first letter you press, Q, we need to start the macro. We're going to start on the line we want to replace Q. Alright, what letter do we want to use? I just always use a replace my macro, there we go.

We're recording at a Here we go. All right, we're gonna do this. The pressures on, so I'm gonna teach you a very cool keystroke, which is how do we go to the beginning of the line? Well, it's actually pretty hard to go to the beginning of the line, because we can't just use W.

Notice that if we're on the else if there's an extra W, so we need something that just goes to the beginning. So I'm gonna do, underscore, right? Underscore will go to the first non whitespace character of the line. Awesome, now we're gonna do a couple other commands that we haven't learned yet.

It's just gonna be exciting, okay, you're gonna type the word T. Now T is a very special command, but we're gonna get there. So we're gonna do this, D for delete, T, and then, quotes. It's gonna delete up until the quotes. Yay, now we're gonna use F. F quotes, it's gonna to hop to the next one, we're gonna go over one, and we're gonna use D dollar, awesome.

So we've just done all of that. We have now erased everything around it. I'm gonna use SHIFT+A to go into insert mode at the end, put in the colon, and then I'm gonna to leave Insert mode then we use shift I to go to the first none white space character and type in case.

Man, we're so close. We haven't messed it up yet, I thought I was gonna mess it up, we haven't. This is fantastic. I'm gonna press Ctrl+C to leave insert mode and we're gonna go jj to j's down Now in the press Q, now we've stopped our macro. So if I've done this correct, it should go to the beginning of the line, delete up until the double quotes, jump to the next parenthese or the next quotes, then delete everything beyond that.

Do the colon insertion and then the case insertion and then go two down. So at a at a, I could do 10 at a. Yeah, look at that, Bingo. There we go. We've done it. We've just used a macro to replay and be able to do this. This is that I actually genuinely thought I was gonna screw that up in front of everyone because it's just so easy to screw that one up.

But we did it we were able to create a macro that can run on each line and go to the next result for us. Now, imagine that sense it's a macro that just does your actions. You could use find next, for where you need to jump to next.

Remember, it's just a macro is replaying your keystrokes, alright? Remember the register. Do you remember the register that we kept pulling up? For now, for fun, let's just type in colon register. In here do you see anything that's interesting? Hopefully, you're looking around and you're feeling a little astute.

You'll notice that in register A, what's in register A? Wait a second, I mean, this looks like exactly what I just typed, hmm. On if there's something we could do with that. We can

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now