Lesson 26 of 26
VIm Macros
In this lesson
Roy explains that q and a letter start recording a macro into that register, q again stops it, and the @ sign with the letter plays it back, optionally with a count. To build a numbered list he records a macro into n that yanks and pastes a line and increments its number with Ctrl-A, looks at it with the reg command and runs it 10 times with 10@n. He then pastes the macro into the vimrc as a leader mapping, and notes that a count does not work with that mapping.
Transcript
[00:08] (external link, opens in new tab) Vim macros are one of those things that a lot of people hear about, but a lot of them don't really actually use, but they're actually pretty simple. The way Vim macros work is that you press the letter Q, and then you decide on a letter or a number that will be used as that as that macro location. Usually, it's a letter. And then you press and then you do something with your keyboard and it's being recorded. And then once you finish, you just basically just type Q again. And then the macro stops recording.
[00:38] (external link, opens in new tab) And from that moment on, whatever you just did is in that register. And you then you want to execute it, you press the add sign and then the letter you just recorded to and then the macro is executed. And of course, you can put a number before that. And then the macro gets executed multiple times. So for example, let's say you wanted you you don't always use them, but when when you do use them, they save a lot of time. Let's say you have a task of creating, I don't know, a thousand lines or editing a,000 lines with the same type of actions.
[01:11] (external link, opens in new tab) You can use macros for that. Here's a simple example that a lot of people use to display this power. If I wanted to create a list of 10 items, just the numbers, I can start by saying one, right? And then escape. But now I want to create a macro that copies the line and then increments the number using a
[01:42] (external link, opens in new tab) And and basically does that multiple times. So, let me hit Q and let's choose a the end character because I'm creating numbers and because I waited too long it the macro didn't start. So, let me start again and press QN. Now, you see at the bottom it says recording to the N macro
[02:13] (external link, opens in new tab) To the N register. So now I'm going to do Y to copy the row P. And now I'm in the second row. And I'll press Ctrl+ A to increment the number. And that's it. So I'm going to press Q again to stop recording. If I want to see this macro, I can type command reg to see my registers. And you'll see that in the double quotes n
[02:44] (external link, opens in new tab) I have this exact set of steps. So technically I could even paste it into my vimrc file and use that as a command later. So let me repeat that 10 times. I'm going to say 10 at n and this repeats. Let's do it a th00and or 500 just for the sake of argument. 507 at n and here we are. So macros are very powerful.
[03:14] (external link, opens in new tab) Let's see if we can add this to our vimrc and say write quit and e sorry vim home vrc. So, I'm going to go down to the bottom and I'm going to paste the contents of that macro. So, if you remember the the macro is in a register and I have a whole video about mapping and registers, but to access it, I'll say double quote n and then just paste.
[03:54] (external link, opens in new tab) And here I have it here. So, let me map this. I'm going to say N O remap. So in normal mode, no not recursive map, my leader key and I covered that in a separate video and N for number or maybe my leader key and then NN for for this macro and that will equal this set of actions.
[04:26] (external link, opens in new tab) And I can basically execute that multiple times. So let's see. I'm going to right quit open my index html and let me just delete from this from this line. Shift G, delete. And I'm going to write a new line here that says one.
[05:00] (external link, opens in new tab) And let me see if I can do a leader. In my case, leader is comma. So leader n and I have this. Let's try and do 10 leader andn. That didn't work because those numbers do not work with those different types of commands. We would have to do that separately. And we can do that as Vim functions, which I'm not covering yet in this course. But that's an example of how you can save or not lose. If you're doing really, really complicated actions, you want to save them as a one-off.