Vi Tip of the Week: Go to a specific line

This is part of a series of tips on using Vi.  To see the complete series, click here.

Sometimes when I open up a file in vi, I want to go to a specific line.  Perhaps I had a compile error on a certain line, and I want to take a look at that line.  It’s nice and easy to do: when in command mode, just type the line number, followed by an uppercase G.  So, for example, if I type:

35G

I’ll be taken to line 35.

Actually, in the situation I mentioned, there’s an even better way to do it — when I open the file, I can specify what line to go to with the +linenumber option on the command line.  For example, to open a file called dummy.txt and go directly to line 35, I can type:

vi +35 dummy.txt

and the cursor will be placed on line 35.

There are a couple variants that I use frequently: if I want to go to the first line, I enter:

1G

and if I want to go to the very last line of the file, I don’t put a number first, just the uppercase G

G

takes me to the last line.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.