VIM Fundamentals VIM Fundamentals

Advanced Motions: Jump, Delete, & Select

Check out a free preview of the full VIM Fundamentals course:
The "Advanced Motions: Jump, Delete, & Select" Lesson is part of the full, VIM Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

ThePrimeagen demonstrates more advanced motions including jumping by half a page, jumping to the next opening or closing curly brace, and deleting the content inside of curly brace pairs.

Get Unlimited Access Now

Transcript from the "Advanced Motions: Jump, Delete, & Select" Lesson

[00:00:00]
>> So anyways, here's my favorite one, which for a long time, I hate it, I actually thought this was by far the worst form of navigation. I'm on YouTube multiple times saying how disorienting it is, but I got used to it, I just finally went, that's it I'm committed, I'm committing here.

[00:00:15] We're gonna do this and so it's Ctrl d and Ctrl u Ctrl D will jump down by a half page, Ctrl U will jump up by half page. If you didn't notice when I close this reopen it, when I jump back down here I just did Ctrl DD.

[00:00:27] A couple times landed right where we needed to be and kind of picked up from there, so I use this because it is a very predictable jump. Once you can quick getting disoriented by that quick code jump, and you kind of start to feel it. And it becomes more normal, then it's super predictable, right exactly what's going to happen.

[00:00:46] The thing that's below your cursor will now be above your cursor, right? And so it's okay, I see how this thing works, right okay, that's really easy actually It's not that bad. I'd say that in this one for me has what I refer to as appear benefit, other than if you're too zoomed in.

[00:01:03] Now for everybody here, I zoom in pretty heavily, which means I'm literally jumping nine lines of code at a time, right? If you're super zoomed out and you'd like to read like super small code, when I'm jumping right now by 40 lines of code. So it can become very large jumping at that point, so it is unfortunately tied to your viewpoint.

[00:01:21] There is also like I believe it's b and e yeah, b and e i think jumped half pages, quarter pages I can't remember, I don't ever use them. Whenever they're another form of jumping, I've never really gotten it meant to them. Here's another one that's pretty neat is bracket m and bracket capital M.

[00:01:40] Effectively if I do closing bracket M, it'll jump to the next opening squirrely brace, this is really nice for C style languages, right? I can just jump, did it all the way to the closing and then I can go the back way I can just jump back up.

[00:01:54] I have yet to really get used to using this, I don't really just because I find Ctrl + D to almost always fit my use case, right? Because I don't really read code and have to hop down block by block. But it is something that's there and I know a lot of people actually find this to be one of the preferred ways to navigate.

[00:02:10] Not for me, but you can try it out and you can see it like it also, the key combo of brackets can be really uncomfortable on some people's keyboards. But there is a plugin by a person named t Pope, the father of Vim plugins. And he has a bunch of these that will actually map to other things within Vim.

[00:02:27] That makes it so that this becomes a more consistent way you navigate in vim so, it's not the worst, I believe it. He also has a for a quick fix list so, you can do like bracket q so it feels the same way, right? If you start to feel, like Vim operates the same all the time for me, just not for me, I make a quick fix list to J and K.

[00:02:46] I just do things a little bit differently, but you get the general idea all right, one more this, I love this one. This one became one of my favorite ones of all time because it's just this thing that's so magical. It's also probably a little weird for a lot of people to see someone so excited about an editor but think about it.

[00:03:06] This same excitement that's in me could be a new as well, right you don't even know how exciting it is once you can use it. When you're on a pair, meaning notice that right away when I'm on this squirrely brace. Look all the way at the bottom right there, it put a little underline right it's able to pair up brackets really quickly.

[00:03:23] And when I press percent sign, it will jump between brack l jump between pairs. It does not work with text, but it works with brackets, squirrely braces, and parentheses and so it's very nice. So if you wanted to jump between something, you can just do it and one of my favorite motions of all time.

[00:03:42] Even though it's completely and absolutely not the fastest way to do it, It's shift v, f score Li percent sign D, right? I can just delete that block by doing that by doing a shift v jumping to the end, jump to the matching, delete those lines, It just feels super cool to do, right?

[00:03:59] It's not the most efficient, but it just feels Incredible, I don't know why it is I just get so excited about these things, and you can do it right in here, right? So I could do the exact same thing right here, I could just jump between them and just delete this right?

[00:04:12] We shift v, there we go, I was able just to delete out that array really quickly, which makes it really nice. I could have done a looking right here, that's a four down there I could have done d4j right? That would have worked, just not as convenient I don't think I think that those kind of things just feel so cool to do, just so neat.

[00:04:33] I don't know why it's just so awesome to justice all right, so we're going to get a little bit zany now all right. Let's think about the ways we can delete this, now one thing we could do is what I showed you the shift. I've kind of jumped ahead a little bit because I'm just kind of zoom in a little bit faster.

[00:04:51] This is more of the educational one I thought we were gonna all walk through this and do this but now that I know that we're five hours into this. Everyone's probably not necessarily following along as deeply, so I'll just show you it. So we could do the highlighting, the deleting, like I showed you we could have done the d6j.

[00:05:05] We could have done that, we could have done a shift v 6j down, then just simply hit d, right? There's a lot of options to kind of go through all here, we could use c instead of that, but we can spice it up a little bit more and let's do that.

[00:05:20] Let's go down a little bit just a little bit I want to delete all of this well, I could use all those offsets and all that or I could do this D. Obviously, that's not D that's i d i squirrely brace,now by doing that, I've deleted the insides of the matching squirrely brace come on.

[00:05:45] Tell me that's not awesome, that is awesome right, and it works with all sorts of pairs It is fantastic idea I get in there, It also works with ci, look at that. I just deleted everything, I'm in insert mode and I am properly indented, ready to fly, that is just fantastic.

[00:06:00] I can yank the inside of it right now I'm pasting right, I'm able to paste right in there. I can also do things like this I can vi the insides right there I already have it, this is fantastic, right? But what's the problem well, I'm not getting on the outsides, what letter do you think I have to press to be able to delete also the containing, pairs right?

[00:06:27] So if I did DI brace, deletes the insides, what deletes the outsides, you can just say it and chat If you're here. If you're feeling fast, if they're telling me the truth, you're only five seconds away, come on. Tell me what would, if I's the insides, what is the outside or web or zoom, you can just say the letter A, yes, it is totally A.

[00:06:46] So if you go da squirrely, you'll delete the score release as well, which means that if you did va squirrely, you now have everything highlighted. Then I can do a shift v to convert my visual mode into visual line mode and I have the whole block highlighted. One thing that's really awesome is that sometimes you're not at the start of a function.

[00:07:08] So that's super cool V, do that doing that It doesn't really work because I am not at the start of the function. So instead I can do va squirrly, shift v go into that and there, bow down delete it, It's fantastic, right? And we'll show you one more thing, but you can't tell anybody because this is too cool okay, so let's do this.

[00:07:26] We're now here we now have highlighted both the squirrely braces and the contents on the inside. If you press O, you can jump between your visual selection, how cool is that so I can go into line mode kit. I can go up a little bit hit go down a little bit perfect, we have now deleted it, just get the whole thing It's just too dang awesome, right?

[00:07:54] Here's that part I just I don't know, I just think that's super exciting to me that's just the most exciting thing ever to be able to do this. No, I'm getting funny looks from people but this is just the best way for me to do it. So I want to take what's inside this squirrely bait or this if statement and put it into the other if statement.

[00:08:12] So I can why I squirrely just did that I can jump down by 10 going here I can vi squirrely brace highlight everything in there and paste it right in. So I just yanked from one contiguous section relative jumped into another one, paste it right in. So as you can see, the more familiar you become with them, the more amazing these motions get.

[00:08:36] And I'm not even sure if I put this last one, I didn't even put this last one in, but I do and I've been using it a lot, all right. So sometimes, you'll see that I want to highlight some part of the line but not the whole thing.

[00:08:48] Especially if the if anyone out there write C++, every C++ function looks class. And then a whole bunch of stuff and then everything is just everything just there's colons everywhere, right? And you want to be able to say highlight something, the whole thing and just replace it, so there's capital W, so exciting here we go.

[00:09:12] So there's remember the VI how we can highlight or YI or di we delete the insides of If I'm in the middle of a word, I can go di. I just see, let's see di, little w, I just deleted from the inside of the word, that's pretty awesome.

[00:09:31] Which means you can also if you're in the middle, you can vi W, you can highlight from within the word, It also works with ci W, awesome. We're ready to go right It works with everything that about but sometimes you want to not only do your word. But really you want to do everything that doesn't have whitespace, right?.

[00:09:52] You want to do the whole word not just simply whatever is the tokens that separate words. So you can do v i capital W, no, actually highlight, everything that is non whitespace non contiguous whitespace, right? And so that makes it so that if there's a word you have and you need to just move it down into another one.

[00:10:10] I can highlight it do that and then say I move it somewhere else and vi word paste right, I can just replace that word right there. It's really fantastic I can why I capital W juicy yank that whole thing and now I just have it, I'm ready to yank it.

[00:10:24] I use this actually more and more it's surprising how often I use yank by capital W. Or visually select by capital W to replace it with something that I have inside my registers.