Vi/Vim is a command-line text editor which is managed via keyboard shortcuts and commands. In order to move the cursor into different or specified lines, you should use the keyboard shortcuts or commands. In this tutorial, we will examine how to move or jump into a specified line in vi/vim.
Start Vi/Vim By Jumping Specified Line
Vi/Vim is a very initiative command line text editor where you can specify the line number you want to go as a parameter to the vim command. We will just put a plus sign before the line number and provide the file name we want to open and jump inside. In the following example, we will go to line number 40 for the file named list.txt.
$ vim +40 list.txt
Go To The Specified Line With Keyboard
Vi/Vim provides the G command in order to navigate or jump to the specified line number. First press the ESC key in order to change command mode and then type the line number you want to jump to. The last step is using typing G or gg. In the following example, we will jump to line number 40.
or alternatively, gg can be used like below.
Go To The Specified Line From Command Line
Vi/Vim provides a command line in order to use and configure the Vi/Vim editor. This command line can be used simply to go to the specified line number just providing the line number after the
: like below. But in order to be sure for the command line first press the ESC key and then type the following line to just line number 40.
Show Line Numbers
In order to use line numbers for navigation and go to the specified line numbers, the lines numbers should be enabled for the Vi/Vim. By default, the line numbers are not enabled for the Vi/Vim but they can be enabled for the current file with the following commands. Before executing the following command press the ESC key.
Go To The First Line Of The File
Vi/Vim provides a shortcut in order to jump to the first line of the file. Just use the following keys to jump-start the line of the file. But press ESC before these keys.
Go To The Last Line Of The File
Vi/Vim provides a shortcut in order to jump to the last line of the file. Even you do not know the last line number of the file you can use the following key shortcut to navigate the last line of the file.
Alternatively, we can navigate and jump to the end of the file by specifying a very high line which is more than the line count of the file. In the following example, we use
+999999 to navigate to the end of the file.
$ vim +99999 CMakeLists.txt