Take a moment to think about your favorite text editor. How do move the cursor around? You can probably use your arrow keys, page up/down and home/end keys. If you want to get really crazy, there’s a chance that your editor supports control modifiers such as Control+Home or Control+End. However, in a standard editor, there aren’t many options. If you’re like most people, you probably think the same bland movements apply to Vim. Au contraire, my friend; Vim is no ordinary editor!
Before we dive into the extensive list of Vim movement commands, let’s wet our feet with a couple of basics.
There are two ways to move the cursor left/down/up/right. You can either use the arrow keys (like you would in any other editor), or you can use:
- h – Moves the cursor left
- j – Moves the cursor down
- k – Moves the cursor up
- l – Moves the cursor right
Given the two options, which one should you use? It depends. Generally, you’ll want to use h/j/k/l as it is more efficient (being in the home row). In addition, as we’ll see later, there are many combinations that rely on the h/j/k/l movement that might not work with the arrow keys. However, when you are in edit mode, the arrow keys are generally the only way to move the cursor without first exiting edit mode. The only exception to this is that “Control+O” (that’s “oh”, not “zero”) allows you to temporary pause edit mode to enter a single movement command.
Before we move on, here are a couple of patterns you’ll want to keep in mind.
Firstly, you can prefix almost any movement command with a number. For example, as you now know, “h” moves the cursor to the left. “4h” moves the cursor 4 positions to the left and is equivalent to pressing “hhhh”. Secondly, upper and lower case commands that share the same letter generally related (for example, “w” and “W”). Sometimes they will do the exact opposite, other times they will do the same thing but in different ways. However, they are almost always linked in some way.
The First and the Last
The home/end keys work for most versions, but as you can already guess, Vim can do much more!
Moving to the Beginning of a Line
- 0 – Moves the cursor to the line’s first column
- ^ – Moves the cursor to the line’s first non-whitespace character
- I – Like ^, but also enters into edit mode (mnemonic: “Insert at the beginning of a line”)
- S – Like “I”, but it erases the entire line (mnemonic: “Substitute a line”)
- o – Like “I”, but moves the cursor the beginning of the line below, inserting a new line
- O – Like “I”, but moves the cursor the beginning of the line above, inserting a new line
Moving to the End of a Line
- $ – Moves the cursor to the end of a line
- A – Like “$”, but also enters into edit mode (mnemonic: “Append to a line”)
Moving to the Beginning and End of a Document
- gg – Moves the cursor to beginning of the document
- G – Moves the cursor to the start of the document’s last line. You must use “G$” to get to the true end of the document
Moving Relative to the Screen
- H – Moves the cursor to the start of the screen’s first line (mnemonic: “High”)
- M – Moves the cursor to the start of the screen’s middle line (mnemonic: “Middle”)
- L – Moves the cursor to the start of the screen’s last line (mnemonic: “Low”)
We’ve only begun to scratch the surface, and there’s much more to come! Head on over to Part II to continue your journey.