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

last updated in Categories , ,

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

Vim go to line number

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:

Vi / Vim open a file at a specific line number
Fig.01: Vi / Vim open a file at a specific line number

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

How to display line number in vim

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.

  1. 10G : goto line number 10
  2. 100G : goto line number 100
  3. 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:

Vi / vim open a file at a specific function / subroutine / procedure / method in source code
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
Animated gif 01: VIM in action

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.

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.

12 comment

  1. And maybye there is the way to go to speciffied occurence of search term? Say, to the last? Or to 25th occurence?

  2. If you want to pre-position at the x’th occurrence of “string”, you can use the command
    vi +x/string file

  3. Use cheatsheet code for standard functions help in vi/vim
    This will help for jump finding end line, first line, end of file, etc.

  4. Also note that you can use this functionality from within vim as well, e.g.:
    :e +50 path_to_file

  5. # search for a regex string from a dir recursively and print the ready vim cmds
    find $dir -name ‘*.pl’ -exec grep -nHP ‘perl_regex_to_search_with’ {} ; | perl -ne ‘m/^(.*):(d{1,10})(.*)/g;print “vim “. “+$2 ” . “$1 n”‘

  6. Unfortunately, when opening multiple files from command line, I cannot pass a command to each one.
    You can open multiple files with VIM in one line:

    $vim file1 file2 file3

    But the following will not work:
    $vim +43 file1 +31 file2 +76 file3

    This should open file1 with line pointer at line#43 and file2 at line 31 and file3 at line 76
    But what happens is only the last command is used, so file3 opens at line 76 and the other two opens at line 0

    Still, have a question? Get help on our forum!