This Vim tip is for something that most programmers need to do on a routine basis, moving between specific line numbers. Maybe you have an error message and need to go check out a specific line of code. Maybe you want to go to the last line and continue where you left off.
Opening the file
If you do not already have the file open, you can quickly open it to a specific line number using the following syntax.
$ vi +<num> <file>
$ vi + <file>
Replace <num> with the line number. If you do not specify a line number, it will go to the last line.
Another way of doing that is to use the -c option, which allows you to execute ex commands. The ex commands are discussed below, but basically they are the commands that start with a colon.
$ vi -c $ <file>
$ vi -c 12 <file>
File is open
Now that you have the file open, you can still easily and quickly navigate between line numbers. Below is a list of commands that can be used from within the command mode.
+---------+----------------------------------------------------------+
| <num>G | Go to the specific line number. |
| | Line numbers start at 1, if you use 0G it will actually |
| | go to the end of the file. |
+---------+----------------------------------------------------------+
| G | Go to the last line in the file. |
+---------+----------------------------------------------------------+
| ^G | Displays the current status of where you are. |
| | Note: ^ means CTRL key |
+---------+----------------------------------------------------------+
| :<num> | Go to specific line number. |
| | Unlike the G command, both :0 and :1 will go to the |
| | first line. |
+---------+----------------------------------------------------------+
| :$ | Go to the last line in the file. |
+---------+----------------------------------------------------------+
| :# | Display the current line number. |
+---------+----------------------------------------------------------+
If you read through that list closely you will have noticed there is more than one way to do it. Use whatever is more comfortable for you to type, or easiest to remember.
Common Tasks
Now that you know the commands, here are some quick scenarios of when and how to use them.
Where am I?
When you’re editing the file you might want to know where you are. By default vi does not display this information, but it is a quick keystroke to find out. By pressing ^G, the current line number, how many lines in the file, and what percentage that is will be displayed. You could also use the ex command # to find out what line you are on, though that it not quite as useful.
^G
:#
I prefer to know where I am at all times so I enable an option to display that information in the status line. You can enable this by typing :set ruler or abbreviated as :set ru. I put it in my ~/.vimrc so that it is always enabled.
Another way to know where you are is to enable the option which precedes each line with the number. To turn this on type :set number or :set nu.
Remember to disable options you precede the option name with no, ie. :set no[option].
Get to line 100
There’s an error on line 100 and you had better fix it fast!
99j (assuming you are on line 1)
:100
100G
$ vi + 100 <FILE>
$ vi -c 100 <FILE>
Back to the beginning
You want to go to the first line in the file.
:1
1G
$ vi + 1 <FILE>
$ vi -c 1 <FILE>
To the end
Now to get to the last line in the file.
:$
G
$ vi + <FILE>
$ vi -c $ <FILE>
And with that, I am going to wrap this up. You can see there are a number of ways to quickly get to a specific position with just a couple of keystrokes.
Recent Comments