Vim Movements Part II
If you haven't read Part I yet, you'll probably want to do that first.
Welcome back! Buckle your seat belts and hold on to your hats; we're about to blast through a lot of information.
Words! Words! Words!
Vim provides a total of eight different ways of navigating by word:
- w -- Moves the cursor to the beginning of the next word
- W -- Moves the cursor to the beginning of the next word (using spaces)
- e -- Moves the cursor to the end of the next word
- E -- Moves the cursor to the end of the next word (using spaces)
- b -- Moves the cursor to the beginning of the previous word
- B -- Moves the cursor to the beginning of the previous word (using spaces)
- ge -- Moves the cursor to the end of the previous word
- gE -- Moves the cursor to the end of the previous word (using spaces)
It might help to remember "w" for word, "e" for end and "b" for back. The major difference between the lower and uppercase word movements is how they deal with delimiters. Lowercase word movements stop at punctuation while capital word movements only stop at white space.
Let's take an example. If your cursor as at the beginning of this sentence:
shortcutfoo.com is a cool website
Then:
- The "w" would move the cursor to the first period (right after the "shortcutfoo")
- "W" would move the cursor the first white space (right after the ".com")
Seek and Ye Shall Find
Vim also provides quick ways to move the cursor based on a search character.
- f -- Moves the forward cursor to the first occurrence of
- t -- Moves the forward cursor to right before the first occurrence of
- F -- Moves the backward cursor to the first occurrence of
- T -- Moves the backward cursor to right before the first occurrence of
- ; -- Repeats a f/t/F/T command (see below)
- , -- Repeats a f/t/F/T command (see below)
The f/t/F/T commands cannot be used on their own but are paired with another character. For example, pressing "fM" moves the cursor forward to the fist occurrence of a capital "M". It is important to note that these commands only work on a single line; they won't wrap around the next line (but other commands do; more on that later).
When using a f/t/F/T command, the ";" repeats the command in the same direction while "," repeats the command in the reverse direction. For example, after a "f" command, ";" moves the cursor forward. However, after a "F" command, ";" moves the cursor backward.
Seek More and Ye Shall Find More
As previously mentioned, the f/t/F/T commands only work on a single line. However, other searching modes can operate on the entire document:
- / -- Moves the cursor forward to the first occurrence of
- ? -- Moves the cursor backward to the first occurrence of
- * -- Moves the cursor forward to the first occurrence that matches the word under the cursor
- # -- Moves the cursor backward to the first occurrence that matches the word under the cursor
- n -- Repeats one of the four previously mentioned commands (see below)
- N -- Repeats one of the four previously mentioned commands (see below)
Just like the "," and ";" commands, "n" moves the cursor in the same direction while "N" repeats the command in the reverse direction.
Confused Yet?
We covered a ton of commands, and there are many more we didn't have time to talk about. Vim can be overwhelming, but after while, it starts making more sense.
You might also find our Vim dojo helpful. It provides guided practice sessions to help you quickly master these tricky commands.
Until next time, keep Vim'ing!