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
Gis the motion to go to a line — without a count, it goes to the last line of the file- Prefixing
Gwith a number like42Gtells 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
ggto go to the first line of the file (equivalent to1G) - Use
Gwithout a count to go to the last line of the file - Use
<C-g>or:set numberto 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
ruleris enabled (:set ruler)