You can go to a particular line or word in a file when using vi or vim text editor under Linux or Unix-like systems. There are several methods to go to line number in vim
Vim go to line number
The vi / vim text editor supports running any : command using the following syntax:
vi +commandHere fileName
vi +LineNumber fileName
vi +/searchTermHere fileName
vi +/LineNumberHere fileName
vim +/LineNumberHere fileName
Let us see some examples.
How to go to a particular line in a file
To open file and go to a line number 546 in a file named functions, enter:
$ vim +546 functions
Please note that if you are already in vim/vi, you can use the goto command. To do this, press [Esc], type the line number such as 42 and then press Shift-g:
ESC 42 Shift-g
When you press [Esc] key and then [Shift-g] without specifying a line number, vim will take you to the last line in the file.
Yoou can also use the ex command line to go to a line
For example, if you wanted to go to line number 114, you need to press [Esc] and then enter:
Press the ESC key. At the : prompt type the following command to run on line numbers:
To turn off line numbering, type the following command at the : prompt again:
The Vim goto line number command
One can use the G letter. For example, press [ESC] key and type 10G (Shift-g) goto line number 10.
- 10G : goto line number 10
- 100G : goto line number 100
- SHIFT-g : go to last line
How go to a particular word in a file
To open file and go to function called killproc(), enter:
$ vim +/killproc functions
Demo: Vim text editor open a file at particular line number
You learned how to go to a particular line number or word/function in a file when using a vi or vim text editor under Linux, macOS, *BSD and Unix-like systems.