Skip to main content

Vim Basics

Moving Around in Vim

Learn to navigate through the text, lines and file in Vim.

Forget that you have a mouse when you use Vim. It's useless. The keyboard is everything here. Vim is based on Vi editor and the Vi editor existed before the mouse become an essential part of the computer.

Basically, when you are in Vim, you cannot use your mouse to click at in point in the screen like normal text or code editors. This is why you need to know the keyboard shortcuts for moving around in the Vim editor.

Traditionally, the h, j, k and l keys are used for moving up, left, right and down respectively when you are in the command mode. While this is applicable to both Vi and Vim editors, I don’t prefer using these weird key-combinations.

I use arrow keys for moving around in Vim. It works well even when you are in insert mode in Vim. That’s the standard way of moving around that most of the newer generation are accustomed to.

If you are new to Linux or to Vim, you may opt for the arrow keys. No harm done.

However, if you want to be a bit more proficient with Vim, you may want to memorize a few Vim shortcuts for easily moving around the screen. Some of my favorite Vim movement commands are:

  • H – Move to the top of the screen. Note that it doesn’t always mean moving to the first line in the file. It’s just for moving to the top of the visible screen.
  • L – Move to the bottom of the screen. Note that it doesn’t always mean moving to the last line in the file. It’s just for moving to the bottom of the visible screen.
  • M – Move to the middle of the screen.
  • [[ – Move to the first line of the file. You can also ue gg here.
  • ]] – Move to the last line of the file. You can also use G here.
  • nG – Move to line number n. Note that you won’t see anything on the screen while typing the line numbers.
Make sure that you are in command mode before using keys for moving around. Switch to command mode by pressing the Esc key.

As you can see, with these additional movement commands, it will be easier for you to move around in a big text file. You can learn more movement commands like { and } for moving back and forth paragraph, w for moving word by word etc.

You can also use ‘repeater modifiers’ like nG you just saw with most commands in Vim. For example, if you use 5w, it will move 5 words. If you use 6H, it will move to the 6th line from the top of the screen.

You'll see it in a bit more detail in the next chapters.