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.

Get Unlimited Access Now

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.

[00:00:20] 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.

[00:00:39] 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.

[00:00:52] 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.

[00:01:11] 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.

[00:01:28] 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.

[00:01:52] 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.

[00:02:09] 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.

[00:02:24] 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.

[00:02:44] 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.

[00:03:01] 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.

[00:03:15] 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.

[00:03:29] 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?

[00:03:48] 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.

[00:04:07] 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.

[00:04:20] 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.

[00:04:32] 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.

[00:04:50] 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.

[00:05:10] 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.

[00:05:27] 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.

[00:05:43] 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.

[00:06:00] 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.

[00:06:26] 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.

[00:06:44] 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.

[00:07:12] 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.

[00:07:35] 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.

[00:07:49] 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.

[00:08:11] 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