Vim allows you to navigate content a lot more efficiently than most conventional editors. Let's start with the basics.
You can move your cursor around character by character by using arrow keys or letters h, j, k, and l. This is the least efficient and the most precise way to move:
Key | Alternative key | Action |
h | Left arrow | Move cursor left |
j | Down arrow | Move cursor down |
k | Up arrow | Move cursor up |
l | Right arrow | Move cursor right |
The following diagram is a visual representation which might be a little easier on the eyes:
Vi (Vim's predecessor) was created on an old ADM-3A terminal, which didn't have arrow keys. Keys h, j, k, and l were used as arrows.
Image by Chris Jacobs, Wikipedia (CC BY-SA 3.0)
Try it! There's a lot of value to getting used to hjkl for movement: your hands stay on the home row of your keyboard. This way you don't have to move your hands and it helps you stay in the flow. Furthermore, many applications treat hjkl as arrow keys—you'd be surprised how many tools respond to these.
Now you might be inclined to hit directional keys multiple times to get to a desired position, but there's a better way! You can prefix every command by a number, which would repeat the command that number of times. For example, hitting 5j will move the cursor five lines down, while hitting 14l will move the cursor 14 characters to the left. This works with most commands you encounter in this book.
Calculating the exact number of characters you would like to move is pretty hard (and nobody wants to do it), so there's a way to move by words. Use w to move to the beginning of the next word, and use e to get to the end of the closest word. To move backwards to the beginning of the word, hit b.
You can also capitalize these letters to treat everything but a white space as a word! This allows you to differentiate between the kind of things you'd like to traverse.
Let's take the following line of code from our example:
Hitting w will move the cursor to beginning of the word add_animal, while hitting W will take you to the beginning of animal. Capitalized W, E, and B will treat any characters bundled together and separated by a space as their own words. This can be seen in the following table:
Key | Action |
w | Move forward by word |
e | Move forward until the end of the word |
W | Move forward by WORD |
E | Move forward until the end of the WORD |
b | Move backwards to the beginning of the word |
B | Move backwards to the beginning of the WORD |
The following screenshot shows more examples of how each command behaves:
Key | Initial cursor position | Resulting cursor position |
w | ||
e | ||
b | ||
W | ||
E | ||
B |
Combine the movements shown with the directional movements you learned earlier to move in fewer keystrokes!
It's also really useful to move in paragraphs. Everything separated by at least two new lines is considered a paragraph, which also means each code block is a paragraph, as can be seen in the following example:
The functions add_animal and main are two separate paragraphs. Use a closing curly brace } to move forward, and an opening curly brace { to move backwards as detailed in the following table:
Command | Action |
{ | Move back by one paragraph |
} | Move forward by one paragraph |
Don't forget to combine these two with numbers if you need to move by more than one paragraph.
There are more ways to move around, but these are the most important basics. We'll be covering more complex ways to navigate in chapter 2, Advanced Editing and Navigation.