Moving Around
Vim expects you to keep your hands on the home row, so the arrow keys are a crutch you’re about to drop. Everything in this chapter happens in Normal mode.
The basic four
h j k l move left, down, up, and right. That’s the whole set. Yes,
j looks like it should mean “jump” and it doesn’t; the mnemonic that
sticks for most people is that j has a little hook at the bottom,
pointing down.
It feels slower than arrow keys for the first day. By the third day, not having to move your hand off the home row starts to feel faster, because it is.
Moving by word
w jumps to the start of the next word. b jumps back to the start of
the previous word. e jumps to the end of the current or next word.
Capitalized versions (W, B, E) do the same thing but treat
punctuation as part of the word, useful when you’re moving through code
with lots of symbols.
Moving by line
0 jumps to the very start of the line. ^ jumps to the first
non-blank character on the line (useful when a line starts with
indentation). $ jumps to the end of the line.
Moving by bigger chunks
gg jumps to the top of the file. G jumps to the bottom. Typing a
number before G, like 12G, jumps to line 12. { and } jump up and
down by paragraph (a paragraph, to Vim, is a block of text separated by
blank lines).
Counts: the multiplier trick
Almost any motion can be prefixed with a number to repeat it. 3j moves
down three lines. 5w moves forward five words. This is one of the
things that makes Vim fast once it’s automatic: you look at where you
want to go, count roughly how far it is, and jump there in one move
instead of tapping a key repeatedly.
Try it
Open a file with several paragraphs of text (any markdown file works, including this one, opened in a second terminal) and practice:
- Use
hjklexclusively for two minutes. Resist the urge to reach for arrow keys, even when it feels slower. - Jump to the last line with
G, then back to the first withgg. - Place your cursor in the middle of a word and practice
w,b, andeuntil you can predict where each one lands before you press it. - Use
0and$to jump to the start and end of a line with mixed content. - Try
4jto jump down four lines in one move, then6wto jump forward six words.