2. Moving Around in Vim: Baby Steps
Now that you’re in a file, it’s time to move around a little bit. If you’ve ever played with vim before, you’ve probably seen something that looks like this:
1 k
2 h l
3 j
This is a super-unintuitive way of telling you which keys will move your cursor which directions. In English it goes like this:
-
hmoves your cursor one space left -
lmoves your cursor one space right -
kmoves your cursor one line up -
jmoves your cursor one line down
So play with those four keys for a while. The layout is weird at first, I’ll grant you, but it serves the purpose of keeping your hand firmly on home row, and after a while you’ll start to just believe that h j k l should always move your cursor around. This is great when you’re using a program that understands vim keys. It also means that you’ll end up in an instant message conversation with someone, and you’ll inadvertently send them a message that says “jjjkl” as you try to figure out why you can’t move your cursor. It’s a curse.
Okay, moving on. Once you’ve gotten comfortable moving your cursor around, it’s time to edit something. Use the movement keys to get your cursor where you want it and press i.
At the bottom of the screen you should see a message appear:
1 --INSERT--
This is vim’s way of telling you that pressing keys now will make things change in your text. In vim parlance, you have just entered insert mode.
Type something witty in the document. I would put “So long, and thanks for all the fish” because it’s better than “hello world”. But I know it doesn’t matter what I tell you to enter; you’ll put whatever you want. That’s just how you are. You’re independent and I respect that.
Once your witticism is entered it’s time to save it up. Hit <esc> and the --INSERT-- message at the bottom of the screen should disappear. This means that the keyboard is back to issuing commands to vim instead of changing the text on the screen. Vim calls this normal mode, but don’t worry too much about that yet. Type
1 :w
And your changes are saved, friend.10
Now let’s do something a little more daring and, dare I say it, useful. Moving around with your fingers on home row is nice and all, but it’s not worth all this hassle and bother. Let’s look at some slightly larger jumps in the text.
Position your cursor somewhere on a line of text–anywhere is fine– and make sure the -- INSERT -- thingy is gone by pressing <esc> a bunch of times until vim starts beeping and/or flashing at you. 11 Now press A. Note that was an uppercase A, not a lowercase a. This handy little shortcut moves you to the end of the line and puts you back into insert mode, all at once. Think of it as “append” or “add”. Nifty, huh? Okay, press <esc> again and let’s do another one. Press o. (Again, note the case.) Vim adds an empty line, right under the line you were on, and once again you’re in Insert mode, ready to enter new text into the file. Write something if you want, and then press <esc> again to get out of Insert mode.
We’re going to talk more about moving the cursor around later, but for now look how awesome you already are! Since you don’t need to worry about changes this time, you can just type
1 :q!<enter>
To exit vim.
Zoom!
Let’s go back into vim and play with three simple (but useful) commands that I call the zoom commands. To enter vim this time type
1 $ vim sample1.html
So that we’ve got a document to play with. Press j repeatedly to move your cursor down a few lines, it doesn’t matter where it ends up. What we’re going to do is tell vim to scroll the window so that the line the cursor is on is positioned where we want it.
The first “zoom” command is zt, which (I say) stands for “zoom top.” When you press zt in command mode it will move your current line to (you guessed it) the top of the screen. So, if your cursor is on line 15 and is one third of the way down the screen it will now be at the top of the screen.
Next up is zb, or “zoom bottom.” As you can guess, it moves your current line to the bottom of the screen, letting you see everything that came before.
The third and arguably most useful zoom command is zz, which positions the line your cursor is on in the middle of the screen, so that you can see that line in context. I use all three commands fairly frequently, but I find this is the one I like the best. It’s my secret favorite child.
All right, we’ve done some good work here. You’ve started vim and gotten around a bit, and more importantly, you made your code all pretty. In the next chapter we’ll get down to the single most important thing you ever have to do with an editor: choose your color scheme.