vi editor: how to go to a line by line number

The vi or vim editor is the popular and feature rich editor for editing text files in Linux. You can go to a particular line in the editor in several ways depending on where you are accessing it from.

from the command line

If you are opening a file from the command line, and you want the cursor at a specific line then you can use a command line option to do so. You can add the +lineNum with the vi command, where you will replace the lineNum with the exact line number you want.

bash$ vi +343 file.txt 

from inside the editor

If you already have the file open in the editor, then you do have two options to do that. You can use the goto command, by following the steps below

  • press ESC to go the command mode
  • type the line number (eg. 343)
  • press Shift-g (or capital G)

If you do not type any line number, then Shift-G will take you to the last line in the file. Another way to do this is using the ex command line in the editor

  • press ESC to go the command mode
  • type : to go the command line
  • type the line number (eg. 343)
  • hit enter

If you like to see what line you are currently on then turn on the line numbering in the editor. You can do that with the ex command line as well. Press ESC key to go the command mode, and then type :set number and then enter to enable line numbering.