Show Line Numbers in Vi or Vim

Posted by Rex in UNIX

Displaying line numbers while editing a file can be useful. This recipe describes how to turn on and off the display of line numbers in the vi and vim editors.


Obviously, displaying line numbers is essential when debugging code and scripts. While editing a document (and not inserting text), type the following to turn on line numbers:

:set number

If you are in insert or append mode in vi / vim editor, this will not work correctly. Just strike the Esc key to exit this mode and try again.

If you tire of the line numbers, enter the following command to turn them off:

:set nonumber

The Conversation

Follow the reactions below and share your own thoughts.

43 Responses to “Show Line Numbers in Vi or Vim”

  1. October 02, 2008 at 6:32 pm, trilobites said:

    wow, thanks. very helpful!

    Reply

  2. October 18, 2008 at 3:03 pm, thanks said:

    thanks

    Reply

  3. November 09, 2008 at 2:17 pm, pandiano said:

    MANY THX!!!!!!!!!!!!!

    Reply

  4. December 17, 2008 at 6:15 am, robertmarkbram said:

    Helped me!

    Reply

  5. January 23, 2009 at 9:35 pm, tom said:

    helped me too!

    Reply

  6. February 05, 2009 at 11:22 am, Rob said:

    Is it possible to get vim to show line numbers by default? I need them more often than not.

    Reply

    • February 05, 2009 at 3:14 pm, Quinn McHenry said:

      Sure thing — just add

      :set number

      to the file .vimrc in your home directory.

      Sometime when you get bored, type :set all while in vim.. it’ll show you all the available options. Chances are if there’s some behavior you don’t like in vim, you can tweak it.

      Reply

      • August 31, 2010 at 11:38 pm, Mark Szymanski said:

        You shouldn’t need the colon (:) if it is in your ~/.vimrc. Just add set number to the ~/.vimrc

        Reply

  7. February 05, 2009 at 9:28 pm, Sandy said:

    Thanks. It was helpful for me

    Reply

  8. February 10, 2009 at 4:29 am, Joe said:

    Nice… had forgotten my vi

    Reply

  9. March 16, 2009 at 3:45 pm, Vasanth said:

    thanks it helped!

    Reply

  10. March 20, 2009 at 2:15 pm, Dave said:

    too easy.. :-)

    Reply

  11. May 13, 2009 at 3:59 pm, James said:

    Good tip. I get horizontal lines underneath the numbers, is that supposed to happen?

    Reply

  12. June 16, 2009 at 5:37 pm, Anonymous said:

    You can also add:
    set number
    ~/.vimrc

    Reply

  13. August 20, 2009 at 12:01 pm, Dushi said:

    that was so helpful. thanx…

    Reply

  14. September 08, 2009 at 5:29 pm, Tim Broder said:

    sweet, thanks

    Reply

  15. September 16, 2009 at 11:43 am, Rick Graham said:

    This is what I was looking for.

    Here’s what I did:

    Edit ~/.vimrc

    add these lines:

    nnoremap :set number
    nnoremap :set nonumber

    Now, F6 turns line numbers on, Shift F6 turns them off.

    Reply

    • September 18, 2009 at 8:10 am, Cédric Bosdonnat said:

      @Rick: you can use only one shortcut to toggle the line numbers also with the following line (note the ! after the number which mean “toggle” for vi):

      nnoremap :set number!

      Reply

      • May 16, 2011 at 10:23 am, Ville Aakko said:

        For me, I needed to remove  (otherwise they vere pasted into vim) (otherwise they vere pasted into vim)

        Reply

        • May 16, 2011 at 10:25 am, Ville Aakko said:

           What… hmm., no preview and this notices the tags and does something gunky… anyways, I ment to remove (sorry for the whitespace), i.e. only nnoremap :set number! is sufficient.nnoremap :set number! is sufficient.

          Reply

  16. December 10, 2009 at 8:23 am, Anonymous said:

    first time – comments are important more then the post

    Reply

  17. December 24, 2009 at 1:54 pm, Anonymous said:

    Thanks, it worked for me. (gvim version 7.2)

    Reply

  18. February 07, 2010 at 3:30 am, Anonymous said:

    read the instruction for configuring .vimrc file

    Reply

  19. March 09, 2010 at 9:51 am, tojo said:

    thanks krab

    Reply

  20. June 03, 2010 at 9:20 am, Opalinux said:

    very useful thx ! ! !

    Reply

  21. June 24, 2010 at 6:37 am, Ramy Mousa said:

    How to save a file with numbers ,Rathnasekaran.R

    Reply

  22. July 29, 2010 at 9:42 pm, Bytemangler said:

    abreviations work, too:

    :set nu
    :set nonu

    Reply

  23. July 30, 2010 at 6:31 am, SagarRmali said:

    my terminal doesn’t show color word. for example when we write int then it turns green, but mine shows black. this happens for all commands .
    help me!

    Reply

  24. September 01, 2010 at 8:17 pm, lancesimms said:

    I tried so many of these things to get the line number to always appear at the lower right hand corner and none of them worked. It turns out that all you need to do is add

    set ruler

    to your .vimrc file

    Reply

    • September 12, 2010 at 1:52 am, A Obrzut said:

      This did not work as root.

      Reply

      • November 04, 2010 at 8:00 am, Anon said:

        The .vimrc file needs to be in your home directory. Since the root user’s home directory is in a different location than the standard user, it would make sense to put it in that location.

        Though if using sudo to act as root, it may or may not use the user’s .vimrc file – simple enough to test.

        Reply

  25. October 04, 2010 at 9:11 pm, cs said:

    This toggles line numbres:
    :set nu!
    Put this in .vimrc to make F6 toggle line numbers:
    nnoremap :set nu!

    Reply

  26. January 24, 2011 at 7:14 am, Dsa said:

    Thanks

    Reply

  27. April 26, 2011 at 10:06 pm, pedro said:

    do :help linenumbers

    Reply

  28. August 06, 2011 at 11:52 pm, Maikel Bald said:

    Thanks!

    Reply

  29. August 27, 2011 at 3:34 am, linsu said:

    Thank you

    Reply

  30. December 21, 2011 at 7:55 am, Sathis said:

    I want a VI command by which i can remove all my vi settings. ie, I assume there must be something which can be entered in single line mode (Like we do :set ic, :syn off) by which vi will behave with default or no settings. I wasn’t able to get something by googling

    Reply

  31. January 19, 2012 at 6:27 am, Maryam said:

    Wonderful !!

    Reply

  32. April 16, 2012 at 12:55 pm, Edwin said:

    Thanks… its useful to me…

    Reply

  33. May 04, 2012 at 10:32 am, L said:

    Perfect, thanks!
    First result on google when searching for ‘vim line number’, btw.

    Reply

  34. June 01, 2012 at 8:20 am, Baroda said:

    Useful..
    Thanks..

    Reply

  35. July 19, 2012 at 8:51 am, Zhang Chunyan said:

    It is working. Thank you for your sharing!

    Reply

  36. November 22, 2013 at 2:41 pm, visitor said:

    Thanks!

    Reply

Leave a Reply