VIM Fundamentals Advanced Motions: Horizontal & Vertical Movement
Transcript from the "Advanced Motions: Horizontal & Vertical Movement" Lesson
>> Let's go into some pretty advanced things. So this is where it gets kinda crazy. Okay, we're gonna go see really where the power of them actually is. So remember, just to recap, remember how I talked about I/A. I goes on this side, A goes on this side.
[00:00:19] I is the inside A is the outside they append, whatever the word is that A is used for It's fantastic. Remember, 5dd is equivalent of d4j, right. Because you are gonna delete the line plus few others, you're gonna delete your line plus 4 others or you're gonna delete your line and go forward down and delete it, right?
[00:00:41] So they're the same, you can kinda see how you're doing this. You can even do more kind of composability. You know how I talked about G, right? We keep talking about G, I can do dG, and I've just deleted from my cursor all the way down to the bottom right.
[00:00:57] That's pretty neat, so you can always mess with someone do dgdG and delete the whole file, right? It's all gone, but then I can just press U it's back, fantastic, right? All right, so let's go over the advanced motions. So this is really where VIM shines. Okay, we've been using all the slow commands effectively, the only one that's actually really fast is relative jumps.
[00:01:18] I highly recommend them but for this case, it's only one of the very fast motions. So let's just look at some advanced motion. So copy and paste this down and let's do this. Let's go over this. So, the first one, we're gonna start with 'c', so I haven't even talked about 'c' at all.
[00:01:36] 'c' is the equivalent to 'd', except for the only difference is, instead of deleting the line, you delete the line and go into insert mode. So it's kind of like this extra bonus, meaning that if I were right here, I could just go cc. And now not only did I just delete that line, but as you can tell right here, I'm in insert mode.
[00:02:01] So it actually saves you a single step of being able to jump right in. So that's pretty nice, right? One thing that's hard about 'c', is that 'c' is really hard to get into the habit of ,often I find myself d-ing when I should have been c-ing. It's just a kind of a natural thing because I learned about 'c' so much later after 'd' that I built up a lot of muscle memory.
[00:02:25] And so it took a long time for me to be able to get to that point of doing it. Now remember, 'c' works just the same way, so if I did 'c' for 'j' I deleted five lines and went into insert mode. So it's identical to 'd' except for the insert at the end.
[00:02:41] It will also respect indents, If you're in a file type that has inventing available. I don't know if it's going to respect much of the indenting here because we are doing. Cuz I'm in an MD file and these don't really have indent rules, right? And so there could be some difficulties here.
[00:02:59] All right, so let's first start here with the horizontal movements. So now we're gonna move into these. There's a whole bunch of other horizontal movements that are available. Let's kinda go over them one at a time. Now remember, you don't have to be proficient at these, you don't even have to remember all of them.
[00:03:13] What you need to do is, you need to know that there are other motions available, a lot of editing motions in need at least a way to be able to go find out what they are, or have some notion of what they are. And so this is kind of the purpose of this is that I'm showing you what's available, and you can decide on what's good for yourself.
[00:03:30] So I showed you earlier underscore which takes you to the beginning of the line. If you have content online, it will take you to the beginning of that content, right? It doesn't go all the way to the beginning, it just goes to the first non whitespace character, whereas if you press zero, you will actually go all the way to the beginning of the line.
[00:03:48] That's pretty cool right? Dollar sign will go to the end of the line. You can also use carat to go to the beginning of the line. There is a subtle difference with underscore versus carat but for the most part don't even worry about it. I just use underscore cuz honestly underscore is way easier to type.
[00:04:03] I'm kinda gonna be flying through these things because I want you just to know what's available. And if you can keep up please do try these things out because they actually are pretty cool. So capital D, I did show you this earlier, but it deletes everything past the cursor this direction, right?
[00:04:19] Like it just keeps on going. Yeah, yeah, it is this direction, okay. It deletes it, all the way this way, right? And so from your cursor to the end of the line, it is equivalent of doing d$. I just showed you what dollar does, right? So capital d, d$, effectively the same thing.
[00:04:36] You can do the same thing but with c, right? So if I do shift c, not only do I delete the contents, I go into insert mode at my characters position, I can start typing, this actually is pretty darn fantastic. Is really nice to have. S is just probably one of my favorite ones.
[00:04:52] Capital S will delete the whole line, respect indenting, and go into insert mode. So you can just simply delete that line you're on and go right into insert mode. It is very, very nice. Little s, I bet you can maybe guess what it does. Little s is just like x ,except for, It goes into inset mode.
[00:05:10] Little s will delete that single character, and then go into insert mode, yeah, pretty neat. I think it's pretty neat, it's also really fast way to swap two characters, you do x s, type in the character that you had before, escape, SHIFT+P, so it's the capital P. And there you go, blah, blah, blah, I know that's a lot of things.
[00:05:27] Just a quick xse Ctrl + c P, right? It's like that simple, right? I mean, it's pretty neat, right? I really like s, s was actually one of the more first characters that I discovered I just, I used it all the time. f was the one that made me love them.
[00:05:46] All right, so this is the where I went. Okay, then is faster is when I discovered f. So let's go over how it works. So, I wanna jump to that capital T in contents, the middle character, let's call say the middle, second one. So I can go f capital t, that's a capital c, because I'm good at typing f capital t.
[00:06:06] What it will do is it'll jump to the first occurrence of that capital letter. You can do f capital t again to the jump to the next one, or you can do semi colon, which will jump to the next one, you can use comma to jump backwards. You can use semi colon to jump forwards, which means you can find a letter and jump across it.
[00:06:25] It's actually pretty darn nice to be able to just go quickly through a horizontal row. 't' does the exact same thing, except for it's up to the character, not on the character, meaning if I go tT, I'll jump to the n. see I jumped almost there. It has the exact same motions with the semi colon in that so you can jump up to, but not past it.
[00:06:49] Pretty neat, I thought it's pretty neat at least up capital f and capital t. All right, you could probably guess how it would be capital f capital t, we'll just simply do f, but in reverse, right? I had to figure out which way I was gonna go, It goes this way, this time as opposed to this way.
[00:07:06] And these two are now backwards, right? So, semi colon goes backwards in the list, comma goes forwards because you use f to go forwards or f to go backwards. Exact Same thing with capital t, right? So shift t capital t will jump up to the capital t but one character ahead of it.
[00:07:27] Exact same things work right here, you got the idea? Awesome. We did talk about, I think that's, so that's like your primary horizontal movements. I personally use f and t a whole bunch. If I wanna delete the contents of an f statement, one way you can do is just dt close, right?
[00:07:45] So I jumped up until the closing parentheses and closed everything in between. Now, this may or may not be the best way to do it, but it is a way to do it. And it's pretty convenient, It actually is kind of nice to do. You can do that instead of strings, you can delete the content of strings.
[00:07:59] You can delete the content of stuff in between two characters. You can use them with c as well. So I can do c t, closing parenthese, which means I did a change. And now I'm in insert mode and I can start typing. All right, so you can see how c/d have this relation with each other.
[00:08:14] I also use it a lot when it comes to commas, right? So if I wanted to delete, say this middle one, I could do df space. There you go, I deleted up into and including that whitespace. Or I can do dt space, I can do dt, comma, delete up into the comma If it's like a larger word.
[00:08:34] say it could be, an anonymous function or something that has a bunch of symbols, that I couldn't just simply do a udwu on, right? So we're using true, it's pretty simple, but it can be more complex. All right, so I did show you relative jumps, right? So if I wanted to jump to that, let's learn about horizontal movement, I could go 10 up, right?
[00:08:53] And now we just did it, I just jumped there. I could do 9 down jump there. All right, so it's actually pretty easy, right? 9 up, 9 down, right. You can just jump around using relative jumps. And this is pretty nice way to move around but the thing that sucks is if it's something offscreen, how do you get to the thing offscreen?
[00:09:10] Well if you know the exact word you can simply just search for it. That's nice but you don't always know the exact words you're looking for, say like the shape of the coat. Hey, it's down here, I don't really remember what's down there. I just wanna be able to go down quickly, relative jumping stinks.
[00:09:26] And the second thing is I also found relative jumps to be one of the hardest things to adopt into my daily kind of code editing. And so I got so frustrated one time that I actually did something rather ridiculous. Hold on. What MRCS Is? Where are we, then till day?
[00:09:44] Okay, so I have this I'll show that at the end we'll get there but just not at this exact moment. Let's go back to the advanced emotions. All right, so now we're gonna talk about these ones. These ones actually altered your jumping Less. And so now we're gonna get into the vertical movements right?
[00:10:00] These are like the strong vertical movements. So they're opening and closing squirrely brace they also have opening closing parentheses but I just use squirrely brace sometimes, It effectively jumps by what is referred to as paragraph. Paragraph is any contiguous block of code, right? So I can sit there and just jump back and forth, up and down, up and down.
[00:10:19] Unfortunately, it does. If I look at my jumps, you'll see that it actually does fill up this whole space right here. You see all those blank lines, It's because I kept jumping to these blank lines back and forth, back and forth. And so it actually saves all those jumps when I use Ctrl 'o' It won't be as good.
[00:10:34] Obviously it's smart enough that if I do this, it doesn't save all those intermediate ones. But as I walked down Ctrl o will walk back up with the squirrely braces. They're pretty neat, right? And you can combine them so I could d curly brace, right I just deleted from one whitespace all the contiguous code to the next whitespace.
[00:10:55] Pretty cool, I like it I've used it, It's a fun time. Do you see any benefits to this? I would love it if people could say something, especially in chat or in the zoom call, like if there's something that you think is neat about that. What do you think are both the benefits or the negative amounts?
[00:11:10] If you have nothing to say, don't worry, I'll just say what I think are the benefits. The benefit is this, is that if it's short code that you're looking at, it's really easy to jump across, but the hard part is it totally depends on someone's coding style. Some people just put whitespace all over the place.
[00:11:27] Pretty much you're hamstrung at that point, right? You like you move so dang slow, some people will take or I can actually just go like this. I can just yank it right. I can go yank down and some people will put a huge amount right? Have code altogether.
[00:11:42] So if you jump, you'll just be like disorientingly up 100 lines and like what the heck did I just miss what was in between there? So it's not always the best way to do things. I'm kinda on the fence of how good it really is. And it also alters your jump list.
[00:11:58] So It can be good, can be bad, you can make it so it doesn't alter your jump list, but I found that it just makes some people's coats style stinky to navigate. Some people's coats style makes it really nice. So maybe I write code that's nice to use those to jump with.
[00:12:14] But maybe my coworker just writes big gigantic blocks of continuous code and I jumped from the top of the file to the bottom of the file. Super annoying.