Moving around – talk to your editor
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 the h, j, k, and l keys. 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 that might be a little easier on the eyes:
Figure 1.31 – Visual representation of the h, j, k, and l directional keys
Vi (Vim’s predecessor) was created on an old ADM-3A terminal, which didn’t have arrow keys. The h, j, k, and l keys were used as arrows.
Figure 1.32 – The ADM-3A terminal (image by Chris Jacobs, Wikipedia (CC BY-SA 3.0))
Try it! There’s a lot of value to getting used to the h, j, k, and l keys for movement; for example, 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 h, j, k, and l 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 with a number, which would repeat the command that number of times. For example, hitting 5 + j will move the cursor five lines down, while hitting 1 + 4 + l will move the cursor 14 characters to the right. 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 backward 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.
Vim has two kinds of word objects: referred to as lowercase “word” and uppercase “WORD”. In the Vim world, word is a sequence of letters, digits, and underscores. WORD is a sequence of any non-blank characters separated by white space (it’s technically more complicated than that; see :help iskeyword
if you’re curious).
Let’s take the following line of code from our example:
Note
Notice the cursor position – it’s hovering over the first character of ingredient
.
Hitting w will move the cursor to the next comma while hitting W will take you to the beginning of with_spam
. 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 backward to the beginning of the word |
B |
Move backward 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:
Figure 1.33 – You can see three paragraphs here
The INGREDIENTS
constant, and the prepare_menu_item
and main
functions are three separate paragraphs. Use a closing curly brace, }, to move forward, and an opening curly brace, {, to move backward, 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.
Figure 1.34 – A visual representation of basic movement keys in Vim
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.