Check out a free preview of the full Developer Productivity, v2 course
The "VIM Motions & Operators" Lesson is part of the full, Developer Productivity, v2 course featured in this preview video. Here's what you'd learn in this lesson:
ThePrimeagen demonstrates VIM features such as motions (e.g., moving up, down, left, right), operators (e.g., delete, copy), combining motions with operators, using quick fix lists, and the ability to customize and extend VIM using Lua scripting.
Transcript from the "VIM Motions & Operators" Lesson
[00:00:00]
>> ThePrimeagen: Okay, so let's go. I'm just gonna show you a couple cool things about Vim, things that are a little bit different that maybe you just don't know about. First off, motions are pretty darm cool. It just kind of give you quick just breakdown of all the fun stuff.
[00:00:13]
So I'm just gonna put in a bunch of text around here, right? There we go, fantastic and do some of that, and then I'm gonna go, lspStop, because you probably don't want to see all that. Okay, so we have a bunch of we have a bunch of text right here.
[00:00:26]
So obviously, j, if you haven't seen h and l go back forth. l goes one direction, h goes the other one j, goes down, k goes up. You've seen those, w jumps by word, right? So it's pretty easy. So if you had a bunch of spaces in here, w would jump by word, b goes backwards.
[00:00:45]
But those are all basic, no movements of them. F will jump to the first occurrence of that character. So f opens parenthesis will jump to it. Then I can do, there's no more of those. Let's go F open bracket. Then I can do something semicolon, and we'll jump to the next occurrence common will jump back.
[00:01:02]
So you can kind of jump through the list on the horizontal side, F or T will jump backwards. T jumps up to, but not including. So this is some cool things, but that's not what makes vim pretty cool, right? That's some basic motions, right? What makes it really good is that.
[00:01:20]
You combine motions with operators. So d is an operator d means I'm gonna delete, d down in the very bottom right here means I'm in operator pending mode. It's now operator delete now I can do something. So I can go f, opening, what's it called, opening parentheses, and it's gonna delete from my key up to and including the opening parentheses.
[00:01:43]
So you can start seeing, hopefully, that this has a lot of power to it. You can start really moving around. You can combine them. So I can go d, j, so I deleted my current line plus one below. I can go 3j and jump three down. That's why, if you look over here, my numbering is so weird.
[00:01:59]
I have the number of the line that I'm on. And plus relative line numbers either way. So if I wanna go down to this one right way down here, I'd go 8j and jump right to it. If I wanna delete from here all the way up to here, I'd go d11k delete 11 lines up, right?
[00:02:13]
You start getting kind of these ideas of combining things. c is delete except for it goes into insert mode. So ct opening parenthesis. I'm now deleted all the way up to it, but I'm in insert mode, so now I can just type. So okay, that's where those are some fun things but then there's even better operators.
[00:02:30]
Which makes it more fun is that you got AP and IP. So if I go v for visual mode, which highlights, and then I go around paragraph, it will highlight the section around the paragraph. That's pretty nice. So that means I can also dap. You can dap on the haters.
[00:02:46]
I can delete around the paragraph really easily. So a paragraph's any, no white space continuous code. So dap will delete that. dip will delete it, but not the new lines below it. And so there's a lot of fun things you can do with that. You can also to do all the other things.
[00:03:01]
I can yip, I can yap it just a standard Twitter user, I can just yap it and that will put it onto my paste buffer. And so now I can paste it all I want. The things about Vim that also make it unique, as long as you separate out your system clipboard plus your pasting, I can have something that is on my paste buffer, so I can just press p to paste it.
[00:03:20]
And that's pretty nice, right? But I can also have whatever's on my clipboard. So if I go Ctrl+C or Ctrl+V, that's still from those lines I had earlier. So I have the things I can copy on the outside of my editor, say, web addresses, whatever, and then the things I'm copying inside my editor.
[00:03:34]
They're very different kind of distinct things, so it's a little different experience. And so there's the basic crash course Course, on motions, just so everybody knows them all. There's, lots and lots and lots of motions. You can do a lot of really weird things gUIW, all right? g, there you go.
[00:03:50]
IW is really fun. So that's inside word viw will highlight the words you're on if there's periods and other stuff. So say you're in a function, or you're in some sort of C++ or you use a lot of colons. If I go viw, it only highlights up to one of those special characters, but viW, highlights up to and beyond only to the white spaces.
[00:04:11]
So that means I can yiW and yank that whole thing. Now things get even better is that you can remap all of your stuff too. So if I go leader, which for me is space and then I go, yiW. That just did a special operation. That put it onto my clipboard, or should have put it onto my clipboard.
[00:04:30]
There we go, so my clipboard now, I just didn't do it in time. It just put it a space bar but there you go. So now you can even do things that, which I think is super cool, is the ability to remap Operations to actually be different. So if I go this, if I just go capital, let's see what's it called?
[00:04:48]
Go space y you'll notice that it remapped it to double quotes plus y, which is a way that you go to the system clipboard. So it's called the unnamed plus register or whatever it's called. So, it's I've remapped space y into a special area. And now I can just do a motion.
[00:05:03]
fh and that just copied into my clipboard up to the h and including the h. So you can get really kind of adventurous with all of this, but it gets even better. I can delete inside, see that string? Hello from prime. I can go see ci"". I've just jumped to the upcoming string deleted all the contents out of it, and now I'm in an insert mode and I can type, because I used c.
[00:05:27]
You can do the same thing, di() will delete everything within the parentheses. If you had two sets of parentheses, you could technically go d2i(), d2i that. It'll delete both sets of parentheses, which is kind of, now you're getting really fancy footwork there. I never do that kind of stuff but you can get pretty fancy with all that.
[00:05:48]
And so those are all the compound fund operators. There you go. I just want to give you a quick little understanding that once you get really good at manipulating text, you can do quite a bit. And it gets pretty dang fun. One thing that I just think it's fun, which maybe that's kind of silly of me to think programming can be fun.
[00:06:06]
But something that I do all the time, that I just really doing, is if I need to copy this function, Shift+V for highlight line, f4 to the squarely brace percent sign to go down, y to yank it, right? And so you just do that, and it just feels fun to do, right?
[00:06:20]
I can just yeah, and it just feels good to go through these motions, and then all sudden you start doing them. They're like combos, plane some sort of street Fighter game, Mortal Kombat, and you're just into it. It's just a lot of fun. I don't know, it just gets me all addicted and happy about it.
[00:06:35]
And so there you go. So I wanted to show you that. So there you go. There's some motions, there's some other quick things or cool things that I think are just really good. Let's go into a bigger project. Let's go into a project called caleb, I'm making the game that uses Vim motions as a platformer.
[00:06:50]
I know sounds really crazy, right? And so it's still a platformer, except for use of emotions, you get the idea. So if I jump into here, and this is not this is the other one dev and there we go. Okay, we're gonna open up the proper version right here.
[00:07:04]
There you go there's my proper one if I say I search for I don't know state ca S state. It'll show me a whole bunch of occurrences of them. This is just your standard fuzzy find you have this in VS code and all this one thing that I think is super cool about them and I didn't know about this for years is something called a quick fix list.
[00:07:21]
A quick fix list is all of those locations, and you can bunch of different ways you can create a quick fix list, but from search results, I can put all of those locations into this list. And so now I have this list. It doesn't have to be there.
[00:07:35]
It can go that, but I can navigate that list, and they'll show me every single occurrence of that, and I can walk through each one of them. So you can see I'm on 14 out of 390 and so I can just walk through all of them and see them.
[00:07:47]
You can do operations over the top of a quick fix list. Maybe I wanna go quick fix list? Do I want to replace everything that says state with StateFoo? And now it's gonna go through and do all of them. If I wanted to, I press q to stop it, but you could do that.
[00:08:01]
You could also do g, and there you go and then fantastic just highlight does everything awesome. And now we have StateFoo through everywhere. That'd be really bad. So if I were to at this point hit Save, that would probably not be a very good experience, because I just changed everything to StateFoo throughout my entire project that had the word S, state.
[00:08:19]
But quick fix lists are Pretty cool because if you are looking for a pattern, something you need to jump on, that's not a symbol, right? This is not for your lsp, this is for symbols that you just can't quite put on. You can do this and then hop through them as actual points.
[00:08:33]
If you had test breaking and you wanna take your stack trace, you could theoretically take a stack trace, pump it into a quick fix list and walk those points as well. You can do a lot of these kind of fun and clever operations really quickly with it. I'm gonna quit this really quickly quit all bang there we go.
[00:08:50]
All right, so they're very good. They also work really good when it comes to your, what's it called? Your lsp so you can take every lsp reference. And put it onto a quick fix list and then walk them, which is really nice to have, because sometimes you don't want to go back to that.
[00:09:06]
You just wanna be doing something in this file. Then you can go to the next one, then you can go to the next one, and it shows you every single version of where this thing exists throughout your program. So it's really good. Quick fix lists are just, absolutely incredible, because you can just do so much with them.
[00:09:20]
You can get harpoon also for VS code, I think harpoon is great. If you don't use harpoon, do you use harpoon?
>> Speaker 2: No.
>> ThePrimeagen: You hurt my feelings. So harpoon, effectively, all it is sticky files. You choose the file you want, and then that will become a sticky files.
[00:09:31]
I have three sticky files right now in this project, those three so I can just swap between them with one press thing, because I always have, I often find myself editing one file. And then I'll edit one other file, and I just swap back before those things all the time.
[00:09:44]
And I really hate this thing where you have to go input, okay, edit this one, okay? And okay, that one, okay, I need to go back. And then you keep on doing this thing where you're constantly fuzzy fighting between two or three files over and over again. So I just got tired and just wrote my own plugin just to do it for me.
[00:09:58]
So I just never have to think about that again. VS code has a similar thing, just ways to improve your life is a really good move. Or if you find yourself just doing the same thing over and over again, just don't live your life that. Don't, you don't want that.
[00:10:14]
Trust me, you don't want that. There's one little fun thing that I just ran into just recently that I thought was super cool. So we're gonna do this really quickly, which is gonna be aoc-elixer. And this is probably a weird one. I think some people might know where this is going.
[00:10:31]
But for advent of code this year, if you don't know what advent of code is, effectively think Leak code Christmas, right? That's all it is. It's an advent calendar with leak code style problems, but they tend to be a lot more fun that you have to solve on a daily basis, okay?
[00:10:48]
And so this year's day three had this problem. Effectively, what it was is that you had to decipher this input and perform all the multiplication so anything that did, mul. Opening parenthesis, no spaces, up to three digits, comma, three digits, closing parenthesis, that's it. You have to take that and multiply that together.
[00:11:10]
And then, this one would be dropped because it has brackets. This one would be good because it doesn't. This one would be dropped because it's closing brackets. This one would be executed, and this one would be executed. And the fun part about it is that you can actually solve all of this using Vim as the means in which you can solve it as opposed to actually writing a program.
[00:11:30]
You don't even have to write a program to solve this because you can actually solve this also with sed, which is kind of fun. So I can do something this where I can take every single line and I could look for say everything that says the word mul, right?
[00:11:43]
Okay, we can find this, right? We see that, we're seeing that. Then, I need to find a digit, right, but it could be up to three digits. So I don't really know how many, so I have to go 1 to 3, right? There we go. Then I can do a comma and I can do it again.
[00:11:58]
Somewhere between 1 to 3 of those digits. One to three of those digits and then a closing currency. There you go. I found one of these groups, right? So, then what I could do is I could do remember that old fighting one-eyed Kirby. Well, it doesn't have to be a one-eyed Kirby in those capture groups.
[00:12:12]
So, I'm just gonna capture that as a group do a do a replace it and then do a new line, and then do that for every one of them. So now I've partially solved this problem by doing a fun find and replace within my editor. I'm not even programming.
[00:12:27]
I'm just doing this by the way, this has actually happened in real life where I had to execute it was 200 files needed an upgrade. But the upgrade was programmatical, and so I recorded the macro in which I played across all the files. Files, it had to change a function and move if statements around and all that.
[00:12:44]
And so I didn't have to manually do it 200 and some times or try to get a Node program correct to do it. Instead I could play a macro, see the outcome, play the next macro, next one, next one. It just made it really fun. And I used a quick fix list to do that, so I can find the offender, play it once, and then navigate to the next one.
[00:13:01]
Play it,, so it's, yes, okay, I messed that one up. What did I mess that one up for? It's pretty fun. So okay, look at this, so we've got that. We've just done that fun little thing right there, which made me move all that. Then I could do it again.
[00:13:15]
I could do something even more fun, right? I could go, hey, match everything up to the mul, right, up to mul, and then I could do that exact same thing, but this time let's capture these decimals, right? So let's capture any digit that's 1 to 3 long, right?
[00:13:34]
And then let's capture, did I get this one correct? Did I not get this one correct? Hold on we're not correcting. There it is all right, let's just get this thing correct really quickly. Hold on, 1 to 3. There we go and then I want to do digit, 1 to 3.
[00:13:57]
There we go. And then we're gonna close it off. So there we go. We found all those ones. We've matched everything right here. So what I'm gonna do is I'm gonna do a capture group on these digits, capture group on these digits. It's kinda just fun stuff do that, and I'm gonna replace it with 1 times 2, so I can just do a multiplication right here.
[00:14:15]
Okay, that's kind of interesting, right? I can delete that line for now. I can take all these ones. I can replace the new line character with a +. Take that and pipe it out to bc, the calculator, basic calculator. There you go, that's the answer. It's 161, right?
[00:14:31]
It's kind of a fun little thing and so then I recorded that as a macro. And I could replay all those keystrokes in that order and then solve the entire puzzle. And there you go solved aoc without actually writing a program, just manipulating text, which is actually kind of a fun thought exercise that you can go through, which just shows that knowing your tools.
[00:14:49]
And what you can do with them actually allows a whole different, unique possibility for solving it's not that you can really use this regularly. But there's so many little scooby doo scrambles you find yourself in and being able to play around and solve stuff that.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops