Go to a Specific Line in Vim

Vim is a powerful editor that, at worst, will only feel fast. When you blaze through your work in Vim efficiently, you will feel higher than anyone.

But for that to happen, you need to master the navigation in Vim. Let me share a few tricks with you.

Starting Vim at a particular line

Whenever you open a file in Vim, the cursor will be on line 1.

You can make Vim open a file with your cursor at a particular line. Simply type 'vim', with a line number specified after '+' sign and followed by the filename.

vim +5 foo.rs

As you can see in the video, the 'main.rs' file opened with the cursor at line 30 instead of line 1.

If you know the total number of lines in a particular file, you can specify a number that is greater than the number of lines in a file while opening the file in Vim and the cursor will be at the bottom-most line.

If you already have a file opened for editing and you have enabled line numbers in Vim, you can try one of the following ways of moving the cursor up or down.

Move the cursor relatively to the line you are on

Assume that you are on line 54 and you want to move the cursor up 10 lines; here is how you can achieve that.

Make sure that you are in Normal mode; you can do that by pressing the Escape (Esc) Key.

Now, type any number; In our case, we want to move our cursor up by 10 lines, so type '10' and then press the 'k' key. This will move the cursor 10 lines up.

If you press the 'j' key after typing a number 'x' (in normal mode), the cursor will move 'x' lines down.

What are Vim Modes? How to Change Them?
Vim has three different modes for handling user input and processing them.

Jump to a line number in Vim

In an instance where you have a sure/faint idea of what is on line 'x', there are 2 ways you can move your cursor to line 'x'.

In this method, the line that your cursor is on does not matter. What matters is the line number of the line that you want to move your cursor to.

Please make sure you are either in Visual or Normal mode.

Let's say that I want to move my cursor to line 30. So I will type the line number that I want to go to, '30', and follow it with a single uppercase 'G'. Press the 'Enter' key and voila, you are on the 30th line of that file.

The same can be done by using the 'gg' instead of 'G'. It is only a matter of preference.

Jump to the first or last line

Several keyboard shortcuts are used for navigation. Two of which are 'G' and 'gg'.

If you want to move to the last line of a text file in Vim, the 'G' key will move the cursor to the last line in the file.

By default, when you open a file in Vim, the cursor will be on the first line. Then you make some edits and want to move to the first line; how do you do that? To do that, press the 'gg' combination. It is case-sensitive, so make sure they are in lowercase.

This article covers how you can move your cursor to a specific line in the Vim editor.

If you are interested in learning more than just the Vim Basics, I highly recommend using this program by Jovica Ilic.

Mastering Vim Quickly - Jovica Ilic
Exiting Mastering Vim Quickly From WTF to OMG in no time