Go To Line In Vim
Preston Tunnell Wilson
Senior Software Engineer, InStride Health
Published: 1/31/2024
The Short Answer
To go to a specific line number, enter the line number in Command-line Mode (:<line number>) or the line number and G (<line number>G) in Normal Mode.
For more information on how to switch between the different Vim modes, check out our brief introduction to the different Vim modes.
Opening a File in Vim at a Specific Line Number
If you know ahead of time which line you want to go to, for example when you need to edit a failing test, you can include the line number when you start Vim with vim +<line number> <filename>:
Note that this syntax works for any Vim command in general: if you want to execute a command upon Vim startup, you can include it after +.
Navigating with Relative Lines
We've covered going to a specific line by using the line number directly in Command-line mode and Normal mode, but we can also navigate to a specific line relative to our current line (our article on moving up and down in Vim covers this briefly). For smaller vertical jumps, you can probably judge the amount to jump by eye, and you will get better and better at estimating larger and larger jumps the more you use Vim. However, Vim itself can show you how many lines to jump up or down with relative line numbers. Turn them on with set relativenumber in Command-line mode, then use the number in the gutter of the file to jump to a line relative to your current placement.
Showing line numbers is very helpful when editing in general. For more information on showing line numbers, check out our article on showing and hiding line numbers in Vim.
Going to a Line that Contains Searched-For Text
To navigate to a line that contains a specific word or text, you can use the search pattern syntax /<pattern>, which can also be used as a Vim startup command. See our article on finding and replacing text in Vim for more detail.
Navigating Using the Mouse
Finally, you can also enable the mouse in Vim and go to a specific line (and column) by clicking on it. You can enable the mouse for certain Vim modes or in all modes (which is what I did below with set mouse=a):
Written by
Preston Tunnell Wilson
Senior Software Engineer, InStride Health
Filed Under
Related Articles
Vim Find And Replace
You can use either the substitute command or the slash and dot method to search for patterns and replace with different values.
Vim Modes
Learn about seven of Vim’s modes
Go To Top of File in Vim
Learn how to jump the top of the file in Vim, then navigate, search, highlight, and delete.
Vim / Vi Page Up and Down Controls
Quick reference for Vim's page up and down controls
Vim Go To End/Start of Line
Go to the end or beginning of a line in Vim
Copy & Paste in Vim / Vi
Copy (Yank), Paste (Put) and Cut (Delete) in Vim
Undo & Redo in Vim / Vi
Keyboard shortcuts and summary of how Vim tracks changes
Show & Hide Line Numbers in Vim / Vi
Toggle absolute and relative line numbers
Select all in Vim / Vi
Select all and copy, paste, or delete the contents
Searching in Vim
Search forward, backward, case insensitive, and more
Go To End of File in Vim / Vi
Select and delete to the end of a file in Vim
How to delete lines in Vim / Vi
The best answer depends on which mode you use