vimtricks.wiki Concise Vim tricks, one at a time.

How do I jump to a specific line number in Vim?

Answer

42G

Explanation

The 42G command jumps the cursor directly to line 42 in the current file. Replace 42 with any line number to jump to that line. This is one of the most essential navigation commands, especially when working with error messages or log output that reference specific line numbers.

How it works

  • G is the motion to go to a line — without a count, it goes to the last line of the file
  • Prefixing G with a number like 42G tells Vim to jump to that exact line number

Alternative

You can also use the Ex command form:

:42

This moves the cursor to line 42 as well. However, note that :42 does not add an entry to the jump list, while 42G does. Using 42G is preferred if you want to be able to jump back with <C-o>.

Example

You see a compiler error on line 87. Press 87G to jump directly to that line. After inspecting or fixing the issue, press <C-o> to jump back to where you were.

Tips

  • Use gg to go to the first line of the file (equivalent to 1G)
  • Use G without a count to go to the last line of the file
  • Use <C-g> or :set number to see the current line number
  • Use 50% to jump to the line at 50% of the file (halfway through)
  • The line number is shown in the bottom-right corner of the screen if ruler is enabled (:set ruler)

Next

How do I edit multiple lines at once using multiple cursors in Vim?