≡ Menu

VI / VIM: Open File And Go To Specific Function or 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?

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

Tutorial details
DifficultyEasy (rss)
Root privilegesNo
Requirementsvim
Estimated completion timeLess than a minute

vi +commandHere fileName

OR

vi +LineNumber fileName

OR

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

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

Animated gif 01: VIM in action

Animated gif 01: VIM in action

Tweet itFacebook itGoogle+ itPDF itFound an error/typo on this page?

{ 9 comments… add one }

  • punktyras June 15, 2011, 9:52 am

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

    431 -> 546

  • punktyras June 15, 2011, 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?

  • Doug Elliott July 13, 2011, 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

  • Jimmy A. Gunawan July 14, 2011, 12:57 am

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

  • lxtips August 26, 2011, 8:38 pm

    thank-you @doug

  • Aleksander January 5, 2012, 2:54 pm

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

    • Faraz February 8, 2012, 10:05 am

      Thanks Aleksander its very easy and helpfull

  • Yordan Georgiev September 26, 2013, 8:15 am

    # 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”‘

  • Mohammed AbdelRahman January 27, 2015, 6:58 pm

    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

Leave a Comment