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?

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
vi +LineNumber fileName
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:

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:

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:

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.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 14 comments so far... add one

CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
14 comments… add one
  • punktyras Jun 15, 2011 @ 9:52

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

    431 -> 546

  • punktyras Jun 15, 2011 @ 9:55

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

  • Doug Elliott Jul 13, 2011 @ 14:17

    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 Jul 14, 2011 @ 0:57

    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 Aug 26, 2011 @ 20:38

    thank-you @doug

  • Aleksander Jan 5, 2012 @ 14:54

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

    • Faraz Feb 8, 2012 @ 10:05

      Thanks Aleksander its very easy and helpfull

  • Yordan Georgiev Sep 26, 2013 @ 8:15

    # 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 Jan 27, 2015 @ 18:58

    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

    • dragon788 Aug 24, 2016 @ 15:02

      You can open multiple files in this way, but you need to quote everything after the first file according to so your example would be:

      $vim +43 file1 +”31 file2 +76 file3″

  • dragon788 Oct 27, 2015 @ 19:33

    I use this vim plugin to do the same thing. The best part is I can copy from my grep output or a test/debug failure and jump immediately to the spot where it failed.

  • Fernando Hamasaki de Amorim a.k.a. Prodis Nov 15, 2015 @ 15:01

    Thank you for information.

  • nobody Nov 4, 2020 @ 9:30

    Awesome. Learned something new today.

  • Jalal Nov 5, 2020 @ 5:19

    Very Nice Article
    Thanks a lot

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Problem posting comment? Email me @