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

by on June 14, 2011 · 6 comments· last updated at August 6, 2012

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?

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

 
vi +commandHere fileName
vi +/searchTermHere fileName
vi +/LineNumberHere fileName
vim +/LineNumberHere fileName
 

To open file and go to line number 546, 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


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



You should follow me on twitter here or grab rss feed to keep track of new changes.

Featured Articles:

{ 6 comments… read them below or add one }

1 punktyras June 15, 2011 at 9:52 am

To open file and go to line number 431, enter:
$ vim +546 functions

431 -> 546

Reply

2 punktyras June 15, 2011 at 9:55 am

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

Reply

3 Doug Elliott July 13, 2011 at 2:17 pm

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

Reply

4 lxtips August 26, 2011 at 8:38 pm

thank-you @doug

Reply

5 Aleksander January 5, 2012 at 2:54 pm

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

Reply

6 Faraz February 8, 2012 at 10:05 am

Thanks Aleksander its very easy and helpfull

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <kbd> <blockquote> <pre> <a href="" title="">

Tagged as: , , , ,

Previous Faq:

Next Faq: