← Impulso Blog

Be productive with Vim – Part 2: Navigation

Learn how to move easily through a text file using the navigation commands and command-line mode in Vim, the powerful command-line editor.

Be productive with Vim

Editorial translation by Impulso.

Hi, everyone. This is the second article in the Vim series. In the first one, we gave a general introduction, talked about the main advantages of using the editor, and covered the most important and commonly used commands.
You can find it by clicking here

In this article, we’ll talk about navigation commands. And without further ado, let’s get to it!

Arrow keys

By now, you’ve probably tried moving around the file with the arrow keys. They work, but they aren’t Vim’s “official” navigation keys. Vim defines its own navigation keys, and the ones that correspond to the arrows are H (left), J (down), K (up), and L (right).

But... Xavier, if these keys do the same thing as the arrow keys, why would I use letters that give me no clue what they do and aren’t even laid out in a way that makes sense? They’re hard to remember, too. Well, yes. I agree. Still, Vim’s official commands do have some advantages. For example, while the J key moves down one line, the Shift + J command brings the next line up to the end of the current one. Also, with your hands in their usual typing position (old-school typists know this well), these keys are within reach without having to move your hands. What’s more, Vim is designed to let you edit even on the smallest, simplest keyboards.

As with the arrow keys, although PgUp and PgDn work in Vim, the “official” page navigation commands are Ctrl + F or Command + F on a Mac to move forward one page, and Ctrl + B or Command + B to move back one page.

Vim gives you a few extra ways to get around beyond what’s available on the keyboard. One of them is word navigation (Word).

Press W to move through your text word by word. This motion treats punctuation as a word of its own and stops when it reaches it. But as mentioned earlier, there’s also the command with Shift. Shift + W ignores punctuation (or treats it as part of the word), so it moves through the text without stopping at things like commas.

The commands B and Shift + B work the same way, but move backward. They do exactly the reverse of W and Shift + W.

To go to the last line of your file, press Shift + G. To go to a specific line, type the line number followed by G + G.

You’ll notice that there are several ways to get the same results in Vim. That’ll become clearer as we go. For now, let’s say you want to go to line 1. From what we’ve learned so far, we know the command is 1 + G + G. But pressing just G + G, without typing the number 1, also takes us to the beginning of the file. And of course, we can also use the number followed by the “shifted” version of the command. For example, 3 + Shift + G is another way to get to line 3.

In the first article, we also saw how to enter command-line mode. It turns out this is the most powerful command mode in terms of the number of options and features it offers. So this is a good time to mention that you can use it to move between lines in a file, too.

You’ve probably noticed that in command-line mode, you have to press Enter to run a command. From here on, I’ll leave that key out of the command descriptions.

Back to navigation,:44 will also take you to line 44 of the file.

Besides the exclamation mark!, there are other similarities to regular expressions in Vim. For example,:$ will take you to the last line of your file.

This isn’t directly related to navigation, but I thought it made sense to mention another command that uses a key we’ve already covered, the G key. The Ctrl + Shift + G command shows some basic information about the file we’re editing, like how far through the file we are as a percentage.

Wrapping up

Navigation alone gave us plenty to talk about in Vim, don’t you think?

In the next chapter, we’ll cover some of Vim’s deletion commands. I’m looking forward to it.

See you then, bro/sis!!

Related articles