≡ Menu

How To Show Line Numbers In vi / vim Text Editor

How do I display vi / vim text editor line numbers while writing a shell script or c program? How can I turn on or off this feature on Linux, Apple OS X, or Unix-like operating systems?

Displaying line numbers under vi / vim can be very useful for debugging code errors and to improve overall readability of a program or scripts.

Vi / Vim show line number command

To display line numbers along the left side of a window, type any one of the following command while using text editor:
:set number
:set nu
Sample outputs:

Fig.01: vi/vim in action with line numbers

Fig.01: vi/vim in action with line numbers

To turn off line number again enter the same command:
:set nu!
If you need number every time you start vi/vim, append following line to your ~/.vimrc file:
vi ~/.vimrc
Append the following line:
set number
Save and close the file.

Tip: Open a file at particular location/line number

Jump to particular line number from a shell prompt, enter:
$ vi +linenumber file.c
$ vi +300 initlib.c

Jump to particular line that contains main() function from a shell prompt, enter:
$ vi +/searchTermHere file.c
$ vi +/main initlib.c
### note shell escape done with \ ###
$ vi +/addUser\( initlib.c
$ vi +/addUser\(arg1\) initlib.c

Share this tutorial on:
{ 42 comments… add one }
  • yoanderMay 12, 2008, 2:27 pm

    Inside vim editor in command mode you can go directly to specific line pressing line number (ex: 50) and shift g.

  • billyducMay 13, 2008, 10:15 am

    THat’s really kool

  • Planet LowyatMay 17, 2008, 4:20 pm

    Can VI display 3 desire lines such as line x(10), line y(25) and line z(50) only?

  • Dean NguyenJanuary 16, 2009, 4:16 pm


    The “:set number or :set nu” is working great in VI.

    Thank you!


  • neeraj sinhaJune 7, 2009, 9:41 am

    Thanks you so much!
    it works!

  • hilmyOctober 11, 2009, 1:44 am

    its working.


  • woofOctober 21, 2009, 9:44 pm

    Inside vim editor in command mode you can go directly to specific line pressing line number (ex: 50) and shift g.

    Or colon, line number, and Enter (e.g. :50 [enter]). It does the same thing, but is my personal preference over the 50G option.

  • PhilNovember 6, 2009, 8:30 pm

    Does not work in gvim with vim6.3 in centOS.

    • vlayneJanuary 12, 2013, 6:32 pm

      Works with vi & vim in Centos 6.3

      Distributor ID:CentOS
      Description:CentOS release 6.3 (Final)

  • TapasMay 8, 2010, 7:04 pm

    I use Debian you need to create .vimrc in your home directory for that.
    Also syntax on will help

  • myrabelle cabrianaMarch 8, 2011, 5:02 am

    thank uuuuuuuuuuuuu.hehe

  • VenuMarch 9, 2011, 6:54 am

    Previous post not properly added

    vi “file name” +
    vi + will open the file and cursor will be at specified line.

    If the format is
    vi “file name” :

    vi : – How can I get the same behavior.


  • rojahMay 24, 2011, 8:50 pm

    exactly what i was looking for. thank you.

  • sachinJune 1, 2011, 10:41 am


  • totoAugust 2, 2011, 6:33 am

    this is great tutorial. I can see line number in my vim editor. thanks

  • senthilAugust 18, 2011, 9:25 am

    its working .thx

  • RameshSeptember 13, 2011, 5:48 pm


  • SandipSeptember 15, 2011, 10:30 am

    You can also add:
    set number

  • totoMarch 20, 2012, 4:50 pm

    thank you for this info. My vim is look more beautiful :)

  • MalekMarch 21, 2012, 12:32 pm

    this is why I like vim

  • wMay 30, 2012, 8:50 am


  • Mike WJune 8, 2012, 7:36 pm

    This works in Poderosa too. Anyone know how to just turn on a display at the bottom of the screen that shows which line you’re on, as an alternative to having all the rows numbered? Poderosa does this on my Dreamhost account but not on another site I use, don’t know why.

  • MelanieJuly 16, 2012, 6:52 pm

    I have been to your site before and was back again searching for displaying the line numbers. Thank you for this quick bit of help.

  • ScarletJuly 18, 2012, 3:26 am

    it’s helpful for beginners,thx

  • shOctober 30, 2012, 7:25 am


  • Debabrata BanikNovember 14, 2012, 6:01 am

    Thanks, Its working.

  • rajagopalNovember 26, 2012, 1:13 pm

    How can i get current cursor number in vi editor???

    • vlayneJanuary 12, 2013, 6:35 pm

      launch vi –> vi ENTER
      type –> :set number ENTER
      Now as you press down or up, you will see the number value change on the bottom right.

    • jrrsApril 4, 2014, 10:58 pm

      :set ruler

  • newbeeFebruary 22, 2013, 4:10 am

    .vimrc not available at ~/
    now where should I add “set number”

  • sengsengFebruary 23, 2013, 9:35 pm

    @newbee: Are you using a Debian-based system? If so, .vimrc is not included in your home dir. You can create the file by doing this at your command prompt:

    $ vi ~/.vimrc

    You’ll then be inside the vi editor. Press “i” for insert mode and type “set number” into the first line of the editor.

    Once you are done, press ESC, then type ZZ and press Enter. You’ll automatically write the file and exit to your command prompt. You’ll now see .vimrc in your home dir.

    You should now be able to see line numbers when you vi a file.

    Last edited by Admin; 24th Feb 2013 at 9:59 AM. Reason: Correction.

  • soreanaApril 7, 2013, 2:45 pm

    You solve my problem in shortest waythat possible.
    thank you

  • preetMay 23, 2013, 9:06 am

    tnks really useful ……….

  • VanJune 9, 2013, 7:27 am

    Thanks for the tips, it works for me

  • markJune 11, 2013, 9:40 pm

    Every time I enter to my vim file I have to put “:syntax on” to show the colors. Is there I way I can set it up as default like “set numers”, (in fact I’m still trying to undertand how to make it with the numbers). Thanks.

    • nixCraftJune 12, 2013, 10:06 am

      Edit ~/.vimrc file and append the following:

      set number
       syntax on
       set ruler

      Save and close the file. It should now work.

      • markJune 13, 2013, 2:51 pm

        Thank you!! I done it! When I put ./vimrc it show a message of permision denied so I try with sudo, but doesn’t work either. So I createda new one: “vim ~/.vimrc” and I put the comands in the file!

        :set nu
        :syntax on.

        And it works!

  • RBJune 18, 2014, 9:12 am

    Beautiful. I was missing that (just started using Vim :3). The lines and columns are displayed at the bottom right but this is much nice and similar to all decent code editors.

  • Aldi FirmansyahJanuary 27, 2015, 7:34 am

    it’s working very nice. thanks

  • ashmantakFebruary 17, 2015, 5:57 am

    Thanks, it works.

  • ramyaMarch 27, 2015, 1:24 pm

    I want to indent and set numbers for each and every program by default. Is there anyway to do this

  • LionelSeptember 12, 2016, 7:18 pm

    You should take out the : in front of your command line it is a little confusing. Otherwise, excellent tutorial. God bless

Security: Are you a robot or human?

Leave a Comment

You can use these HTML tags and attributes: <strong> <em> <pre> <code> <a href="" title="">

   Tagged with: , , , , , , , , ,