- Read Tutorial
- Watch Guide Video
To start off this section on how to master Vim, we are going to start off with some examples on walking through how we can navigate through a file using the Vim interface.
You wanna start off in the terminal, and if you don't have the terminal open, and you're on a Mac, then you can hit Command + Space, and just start typing in terminal, and hit Return, and then this is what you'll see. Your colors might be different, you might have a different set of items right up here, but this is just your basic terminal.
So I'm going from here to create a file, and the way I can do that is by typing in vim, and then the name of the file. I'm just gonna call mine example.txt
, and this is going to start up a Vim session here.
Now, it would be kinda difficult to learn how to use Vim and learn how to navigate without any content. So I'm going to switch into Chrome, and pull in a few paragraphs of lorem ipsum text here. So I'm gonna grab everything in this third paragraph, switch back.
Now, if I just typed in Command + V, like you could on Sublime, or Microsoft Word, or anything like that, then it's not going to work, because we are not in the correct mode. And in one of the next guides, I'm gonna go into all of the modes that you can use in Vim, but for right now, let's just talk about how we can switch into a mode where we can insert text.
To do that, you type in the letter I on the keyboard, and if you look on the bottom left-hand side, then it says insert.
Now, if I want to get out of insert mode and back into what's called the command mode or the normal mode, I can hit the Escape key, and it takes me back. But I actually do wanna paste some text in, so I'm gonna hit I, and then I'm just going to paste that text in, and you can see it's right here.
Now, if I hit Return, I can go down, and let's do this a few more times, so we can have five paragraphs to work with. Now that I have all of this, I can hit Escape, and I'm back in normal mode. So like I said, in one of the next guides, we'll go into the difference in all the modes, but to have a very easy example, if I were to try to type characters in right now, you can see that nothing is actually working.
If I typed in a different character, then it's going to let me navigate, and that is the key thing to know with this normal mode, is normal lets you navigate and lets you perform all kinds of various tasks in Vim. This is not the mode where you would type anything in. If I hit I again, right here, I can start typing in, and it will let me type anything I want, and it gets added. If I hit Escape, I'm back in normal mode, and that's all I have to do.
Now, for navigation in Vim, you have all kinds of different options, and you can even customize it if you'd like to, but the basic ones are the ones we're gonna talk about right here, and you can use your arrows, however, that's considered kind of a bad practice. So right here, I'm using the arrows to try to go left and to go right, and I can use arrows to go down and also up.
However, Vim is all about efficiency as a developer, so there is a special position, and the position is called your home position, with your hands. And if you have ever taken just a basic typing class, you know that home position is where you have your fingers on your left hand on A-S-D-F, and then your fingers on your right hand in J-K-L-semicolon, and then your thumbs on the Space bar, that's just kind of considered your home position.
Well, Vim is so efficient that instead of you having to move your right hand every single time that you wanna move left or right, it actually has all your navigation keys right here on the keyboard. So, in other words, if I wanna go left, you can see where the cursor is right there. If I wanna go left, then I can just type in the letter H, and it lets me go left. If I wanna go right, then I can type in the letter L, and it lets me go right.
Now, if I wanna go down, I type in the letter J, and then if I wanna go up, I type in the letter K. So just in review, H is to the left, J is down, K is up, and L is to the right. Now, if you're using Vim, these are going to be keys that get ingrained in your brain so much that you will be able to use them without even thinking twice about it. So that is how you can navigate from left to right, and up to down in Vim.
Now, if that was all you could do, this would be a absolutely horrible text editor, so we need to talk about some other ways that we can navigate. So let's say that I wanna go to a particular line. Say I wanna go to the second line in the system, I can type in, while in normal mode, Colon + and that's gonna take me to the second line in the program.
Later on, we're gonna talk about how we can do things like add line numbers and customize our system. For right now, just know that's where line two is. If you're wondering why line two isn't some place like right here, it's because of the text I pasted in. This is all considered just one single line, because it's a paragraph.
If I wanted to do something like this, where I created, you got a line one, then line two, line four, and so on and so forth, then if I typed in Colon + 2 it would take me right where you'd expect there on line two.
So navigating down with the letter J, I can go up and down, but let's say that I have a huge file. So let's say that I have a file that's a CSS file, and I have 5,000 lines in the file. You wouldn't wanna have to go all the way down manually using J, you also wouldn't wanna go and have to know or kinda guess how many lines there are in order to do something like we're doing here. So if I wanted to go to line five, I could do Colon + 5 on the bottom, and it would take me to line five.
However, let's say that I have no idea how many there are. If I wanna get to the very bottom of the file, I can just hit Shift + G, and that takes me to whatever the very last line is.
So now I'm going to switch into insert mode, but I'm gonna type in the letter O, and I'm going to give you so many commands that your head's probably gonna be spinning if you've never used Vim before, but don't worry. We are throughout this entire section, we're gonna be repeating things quite a bit, so don't let this scare you off.
But if I wanna switch into insert mode, and I want to go onto the next line, then I can type in the letter O, just like I'm doing right here, and I can say final line, jumping up to the top. Now if I want to navigate all the way down to the final line, hit Shift + G, and I am right there, and everything is working properly.
So those are some basic ways of being able to navigate down and being able to go from the top to go to any line and go from right to left, up to down. This is simply the very most core part of using Vim, this is not anywhere near, and doesn't touch all the different efficiencies that we're gonna learn throughout this section, so definitely stay tuned on that.
Now, one final thing, and this is an interesting thing, if you've never used Vim before, it may not seem like a big deal, but knowing how to actually quit the program is a surprisingly challenging thing. Usually, you'd have to Google it or something like that, but that's what we're gonna do, I'm not going to let you finish off this first guide without having an idea on how to close the program.
Technically, yes, you could just come up here, click, but then you wouldn't have any of your items saved. Vim has all of your commands that you're gonna need right here in the bottom left-hand corner of the screen. So if I want to save this file, then I can type in Colon + W, and it will save the file. Now, if I want to quit the file, then I can do Colon + Q, and this will quit the file.
Now if I want to come back, and let's say that I wanna be more efficient, so I come down to line four, and I say, this is the fourth line, hit escape. Now, if I wanna save and quit in one stroke, I can do Colon + WQ, so that's write and quit, and that will quit. Now, let's say that I open this up, and I put in some gibberish for some reason or another, and I don't wanna save it, I simply want to quit. The way you can do that is make sure you're in command mode, hit Colon + Q with an exclamation mark, and what this will do is it will let you quit the program without saving it. So if I do that, open it up again, you can see that all my gibberish has closed off.
So that is the initial start to how to navigate inside of Vim, how to save files, how to quit out of files, and some of the very few basic shortcuts. In the next guide, we're gonna start going through some of the more advanced things that help us be more efficient when navigating through a code file.