VI / VIM – Open File And Go To Specific Function or Line Number

ADVERTISEMENTS



Vim go to line number

How do I open a text file using VIM or VI and go to specific line? How do I open a file using vim and go to specific function() in my source code?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

The vi / vim text editor supports running any : command using the following syntax:

vi +commandHere fileName

OR

vi +LineNumber fileName

OR

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

Sample outputs:



ESC 42 Shift-g

Yoou can also use the ex command line to go to a line

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: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.

For example, if you wanted to go to line number 114, you need to press [Esc] and then enter:

:114

Press the ESC key. At the : prompt type the following command to run on line numbers:

set number

To turn off line numbering, type the following command at the : prompt again:

set nonumber

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

Sample outputs:



Demo: Vim text editor open a file at particular line number

Conclusion

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.