Vim go to line number
The vi / vim text editor supports running any : command using the following syntax:
vi +commandHere fileNameOR
vi +LineNumber fileNameOR
vi +/searchTermHere fileNamevi +/LineNumberHere fileNamevim +/LineNumberHere fileNameLet 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 functionsSample outputs:Fig.01: Vi / Vim open a file at a specific line numberPlease 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-gWhen 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:
How to display line number in vim
Press the ESC key. At the : prompt type the following command to run on line numbers:
set numberTo 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 functionsSample outputs:Fig.02: Vi / vim open a file at a specific function / subroutine / procedure / method in source code
Demo: Vim text editor open a file at particular line number
Animated gif 01: VIM in action
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.
Posted by: Vivek Gite
The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via
RSS/XML feed or
weekly email newsletter.