VIM Fundamentals

Basic Navigations



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


Transcript from the "Basic Navigations" Lesson

>> 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?

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.

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.

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.

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 markdown, and then we're gonna execute vim, exercise md.

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.

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.

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?

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.

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?

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.

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?

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.

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.

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.

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.

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.

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.

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.

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.

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.

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?

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?

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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?

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?

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.

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.

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