You can move around in Vim with just a few keys, and once you get the hang of it, you’ll wonder how you ever lived without it.
Let’s see it in action. Imagine this file content:
This is a sample file for demonstrating Vim navigation.
We want to move the cursor efficiently to different positions.
The goal is to avoid using the arrow keys entirely.
Start with your cursor on the T of This.
Press l (lowercase L). The cursor moves one character to the right, landing on h.
Press h. The cursor moves one character to the left, back to T.
This is the most basic movement: h for left, l for right.
Now, let’s try words.
Position your cursor on the T of This.
Press w (lowercase W). The cursor jumps to the i of is.
Press w again. It lands on the s of sample.
Press w again. It lands on the f of file.
w moves forward to the start of the next word.
What if you want to go back?
Position your cursor on the f of file.
Press b (lowercase B). The cursor jumps to the s of sample.
Press b again. It lands on the i of is.
Press b again. It lands on the T of This.
b moves backward to the start of the previous word.
This is powerful, but what if you need to jump within a word?
Position your cursor on the T of This.
Press w three times. You are now on the f of file.
Press f (lowercase F) followed by l. The cursor jumps to the l of file.
f followed by a character will move the cursor forward to the next occurrence of that character on the current line.
Let’s try going backward within a word.
Position your cursor on the l of file.
Press F (uppercase F) followed by s. The cursor jumps to the s of sample.
F followed by a character will move the cursor backward to the previous occurrence of that character on the current line.
The problem this solves is the inefficiency of using arrow keys or the mouse to navigate text. Traditional text editors often require you to lift your hands from the home row to move the cursor, breaking your typing flow. Vim’s modal nature and its powerful, mnemonic keystrokes allow you to keep your hands on the home row, making editing significantly faster.
Internally, Vim treats the file as a grid of characters. h, j, k, l are fundamental movements that correspond to left, down, up, and right, respectively. w and b are word-based movements; Vim has a definition of what constitutes a "word" (usually a sequence of alphanumeric characters and underscores, separated by whitespace or punctuation). f and F are character-wise searches on the current line.
The real magic happens when you combine these with counts. If you’re on the T of This and want to move to the f of file, you can press 3w. The 3 is a count, telling w to repeat three times. Similarly, 3b would move you back three words. For character searches, 3fl would find the third l on the line.
Most people don’t realize that w and b have variations. W (uppercase W) and B (uppercase B) are "WORD" movements. A "WORD" is typically defined as a sequence of non-whitespace characters, separated by whitespace. So, if you have word1,word2, w will move to w of word1, then w of word2. W will move to w of word1, then to , of word2. This distinction is crucial for navigating code or text with heavy punctuation.
The next concept you’ll want to master is vertical movement within the file using j and k.