VIM Fundamentals

Basic Navigations

ThePrimeagen

ThePrimeagen

terminal
VIM Fundamentals

Check out a free preview of the full VIM Fundamentals course

The "Basic Navigations" 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 basic navigation including moving the cursor up, down, left, and right, recentering the screen, hopping forward by word, and hopping backwards. How to delete a single character is also covered in this segment

Preview
Close

Transcript from the "Basic Navigations" Lesson

[00:00:00]
>> And so let's quit VIM for one quick second. Now, I assume everyone remembers how to quit VIM colon q. You have to be a normal mode. You will now quit VIM. You press enter and my goodness. What just happened? Look what it says. It says no, right?

[00:00:14]
Since last change, it's trying to save you right now. It's trying to say, Hey. You just wrote stuff for a file, but you didn't save it. So you have two choices. We can either save the file or get rid of it. I'm gonna opt in to getting rid of it.

[00:00:28]
But you can either do right quit to write it and then quit. Or you can do quit bang, and this will simply force quit and ignore anything that hasn't been written yet alright? So it's like your way of kinda getting in and out of it. And so now let's start doing some basic navigations together.

[00:00:47]
Okay, so this is where it's gonna get pretty exciting, hopefully. So I'm gonna actually just copy this. I'm gonna go over here. I'll paste this in the chat. You can just copy and paste it. Yes, please curl down something you don't know what it is and let it be put onto your system.

[00:01:00]
And let's edit it. It's just the internet. It's safe, trust me. Alright, so I'm gonna do the same thing. I'm gonna curl this down. What it will do is it will curl down a basic lesson. And then it's gonna save it to a file called exercise.md markdown, and then we're gonna execute vim, exercise md.

[00:01:17]
This should just work, trademark, that phrase should just work. Here we go. So I'm gonna press enter, and this is what it approximately should look like for you. I'm using Neo vim, so the colors may be a little bit different. But overall, this is what it's going to look like for you.

[00:01:32]
So this is actually just a file from this presentations repo so you can go and look at it if you want to. So let's start moving together alright? So I'll have the keys being displayed down here so you'll be able to see me move around. But the first exercise is to learn about j.

[00:01:48]
We're in normal mode, right? Yes, it doesn't say anything else down below. You can press j to walk. So let's press j and go all the way down to j. Once you've done that you've completed your first set of motions. You are now moving, man doesn't that just feel so slow?

[00:02:04]
Like, why would you ever wanna edit text just pressing j? [LAUGH] I agree with you. Don't worry, it's gonna get better. Hey, at least you can move downwards now. Okay, we're going someplace and if you remember Bill Joy's keyboard if you're paying attention, you could probably guess the rest of the letters.

[00:02:20]
Alright, so let's go down a little bit further, all the way down to this line right here, which is now gonna tell you to press l to follow the line to the o. And then we'll press x together. So I'm gonna go over and I'm gonna press l don't hold it don't cheat now, the reason why you don't want to cheat is you want this to set into your memory, right?

[00:02:39]
You want to press it for now until you can really feel it. A little bit of kinda like some hidden vim knowledge holding j holding k holding l holding h are anti patterns. Don't do that you should never be in a situation in which you're holding something because that is incorrect.

[00:02:58]
But for now we're just gonna be pressing it, it's gonna be the worst possible way of navigating because you need to just get it in here. Alright, so let's press x. x will delete a single character that is underneath your cursor. Strange, right? Why would you ever do that?

[00:03:12]
It can't happen that often. Surprisingly yes, x will have it a lot in your lifetime. It is mind numbing how often x happens. Anyways so let's keep going down. You can press j to go down. Now notice something. My cursor is at the bottom of the screen I don't know what's coming up next.

[00:03:30]
This feels really uncomfortable. Now this is not the way you should do it, but for now, this is what we'll do. Press zz. Lowercase zz. Uppercase zz does quit, so don't get your cases mixed up. So lowercase zz will recenter your screen. And let's go down, down, down.

[00:03:49]
What just happened there? Why is my cursor so far forward? Notice that that my cursor jumped all the way over. Why is that? Well, that's because the line that we left had our cursor way up here. So then we'll maintain your cursor position. And so if you go on two lines, your cursor could bounce around depending on its last furthest known location.

[00:04:09]
So maybe a little bit disorienting, but it happens. So let's keep doing this. Now notice that the instructions say hey, use j and l to walk. But look at me. I'm just like walking. No, I'm at the bottom. Something like zz again. I'm just like walking downwards and it's just all walking to o for me, because we are already so far out.

[00:04:27]
So now that I've reached o, I'm gonna press x deleted again. For those that are going along with me, I am trying to leave enough time for you to be able to do this and walk along with me. So if I'm telling a story or saying something that seems a little off topic, just buying some time, okay, I'm just here for you.

[00:04:44]
That's, what I'm trying to do here okay? So let's keep going down. But this time we're gonna do something a little bit different. When we get to here you can recenter again if you'd like to good habit that you know you're just learning some keystrokes you're learning how to kinda move stuff, press h to go over once, and then press l to come back.

[00:05:01]
Now at this point, your cursor will not automatically bounce over because the furthest known location is the second column n right? So now let's go down j j j over over over over over over j j j, then the other direction h h h h j j l l l l l j z z, I'm gonna recenter things.

[00:05:21]
j j over over over and over. And this is gonna take a little bit of time you'll probably find that this is actually a little bit difficult. You'll have to like think. Okay, I need to go left it's left l over h and you'll probably try them both and you'll get kinda confused.

[00:05:35]
When you get to the end hit that x feels great, right? We did it, we got it. Things are getting better. You have so many keys now that you know. So we're gonna go back we're gonna go further down. We're gonna do it again but this time we're gonna add in the last one which is gonna be k.

[00:05:50]
k goes up. So now we're gonna have a little bit of a foot race okay? I'm gonna see if I can go all the way down to the o, delete it and go all the way back. The first person to finish it in the Zoom call, please say hey, I just did it, alright?

[00:06:04]
Let's see if I can be twice as fast I went off the rails. Is that a disqualification? No, I actually pressed a button that I'm not supposed to press or not. We'll talk about that later. My goodness. This guy is so fast. I think you've been h j k and l before, haven't you?

[00:06:25]
Coming in here with your big scales. Alright that's good though. That is awesome. I'm happy to see that you're able to move and to do these things. This is really, really good. Now, this is absurd. Okay, I want to let you know, this is not how text editing looks in them.

[00:06:40]
But this is very good to have these fundamental movements down because often, you may be off by a single row or two rows. You're off by a character or two characters and it's just better solved with little quick movements and you don't want to have your brain go.

[00:06:55]
How do you go down? Is that j or k? Let's try k dang. That's up. Let's press j right? Like you don't wanna have to go through these thoughts. And the only way to get there is by practice and using it. And so when I first started to kinda answer that original question of hey, this all feels super overwhelming.

[00:07:13]
I don't know what to do. How do you learn all this stuff? The way that I did that was I started with just h, j, k and l and the next couple commands and that is it or the next couple motions technically, and I forced myself to get good at those.

[00:07:28]
And then I moved on. Today, we will be kinda going over a lot. So, you know, what's available. Cuz I didn't even know what was available. As I got better, I was like, well, how do I go faster? What do I do? And people would tell me a little secrets and I feel I just have all this secret knowledge of him.

[00:07:44]
But it's just out there. So I'm gonna help everyone get to the right spot. So let's go down, down, down. I'm gonna use z z again to get there. Now we're gonna be way over so everyone just hold h, I know I told you not to hold, but for the sake of today, hold h to right here.

[00:07:59]
And now we're gonna go to w. Now w probably does what you think it might do which is to hop by word, w w w w right? Not too hard let me just get rid of this. There we go. So there you go. I've made it to o.

[00:08:18]
Now notice something kind of unique about w. Here I'm gonna go. I'm gonna go all the way back when I press w it hopped to the beginning of every non-white space character up until the l. The reason why is it hops by words and there's certain tokens that are considered a word in a word delimiter.

[00:08:36]
This happens to be one of those breaking points. So just jumped up to the o. Now that we're at the l, we can press x. Fantastic. And now let's go down and we're gonna do this again except for we're gonna include b. So follow with me. So press b to go all the way back.

[00:08:50]
Yeah, b's pretty great. If you overshoot, press w to go forward until you get to the beginning. So we're at the beginning, we'll press w, w, w, w, look what happened there, because it didn't detect the next word until the next line we landed here. So now let's press j to go down.

[00:09:09]
I know, if you were to press w would have went straight to o, little secret shortcut. Don't do the supermario shortcut okay? We don't wanna do that here. So press j to go down. And then once you're here, press b to walk backwards right? One at a time, and notice where it stopped.

[00:09:24]
Once again, o and dash are two different delimiting items. And so now you have to press b once more, and you'll go to the o. Let's hit x, we just did it, we dang done it fantastic. Hopefully, you're feeling super confident at this point. You've got your h, j, k's and w's and b's all in your head and l is gonna make it there at some point, but you're feeling really good.

[00:09:44]
So last little thing I have right here is just a little kinda fun figure eight that we can go on. So everyone just try to go through a twice, just pressing the keys, right? Go all the way with l, go down, go back up and then go back with h.

[00:09:57]
And then once you go all the way back to the top, try going the other direction just so it kind of hopefully fits a little bit better in your head cuz the reason why I'm making such a big deal out of this is we're primarily gonna use j and k a ton today alright?

[00:10:12]
It's just gonna happen a bunch. And so I just really wanna make sure you're at least familiar with j and k h and l. I don't really use them that often, but they do happen. So awesome we've done it. Quickly chat. How do we quit VIM or meet?

[00:10:27]
I don't know how fast chat is. How to quit vim question mark. Can they actually respond to me in time? I don't even know. Let's find out. But what is it? What do you press? Anybody who's been paying attention? Say it. There you go, colon q fantastic. So colon q.

[00:10:44]
Bam. No happened again. What do you do? Either colon q bang or w q to write it alright? There we go. We've done it. So now we're actually gonna there we go. Yeah, look at all those golf games. Look what's happening here. This is chat, this is fantastic.

[00:11:01]
Good job chat.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now