Go To Line In Vim

Last Updated on 2023-03-15

The Short Answer

To go to a specific line number, enter the line number in Command-line Mode ([.inline-code]:<line number>[.inline-code]) or the line number and [.inline-code]G[.inline-code] ([.inline-code]<line number>G[.inline-code]) 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-file-specific-line]Opening a File in Vim at a Specific Line Number[#opening-file-specific-line]

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 [.inline-code]vim +<line number> <filename>[.inline-code]:

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 [.inline-code]+[.inline-code].

[#navigating-relative-lines]Navigating with Relative Lines[#navigating-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 [.inline-code]set relativenumber[.inline-code] 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-line-specific-text]Going to a Line that Contains Searched-For Text[#going-to-line-specific-text]

To navigate to a line that contains a specific word or text, you can use the search pattern syntax [.inline-code]/<pattern>[.inline-code], which can also be used as a Vim startup command. See our article on finding and replacing text in Vim for more detail.

[#navigate-with-mouse]Navigating Using the Mouse[#navigate-with-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 [.inline-code]set mouse=a[.inline-code]):