Show line numbers in vi or vim

Contributor Icon Contributed by Rex  
Tag Icon Tagged: UNIX  

Displaying line numbers while editing a file can be handy. 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

 

38 Comments -


  1. trilobites said on October 2, 2008

    wow, thanks. very helpful!

  2. thanks said on October 18, 2008

    thanks

  3. pandiano said on November 9, 2008

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

  4. robertmarkbram said on December 17, 2008

    Helped me!

  5. tom said on January 23, 2009

    helped me too!

  6. Rob said on February 5, 2009

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

  7. Quinn McHenry said on February 5, 2009

    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.

  8. Sandy said on February 5, 2009

    Thanks. It was helpful for me

  9. Joe said on February 10, 2009

    Nice… had forgotten my vi

  10. Vasanth said on March 16, 2009

    thanks it helped!

  11. Dave said on March 20, 2009

    too easy.. :-)

  12. James said on May 13, 2009

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

  13. Anonymous said on June 16, 2009

    You can also add:
    set number
    ~/.vimrc

  14. Dushi said on August 20, 2009

    that was so helpful. thanx…

  15. Tim Broder said on September 8, 2009

    sweet, thanks

  16. Rick Graham said on September 16, 2009

    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.

  17. Cédric Bosdonnat said on September 18, 2009

    @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!

  18. Anonymous said on December 10, 2009

    first time – comments are important more then the post

  19. Anonymous said on December 24, 2009

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

  20. Anonymous said on February 7, 2010

    read the instruction for configuring .vimrc file

  21. tojo said on March 9, 2010

    thanks krab

  22. Opalinux said on June 3, 2010

    very useful thx ! ! !

  23. Ramy Mousa said on June 24, 2010

    How to save a file with numbers ,Rathnasekaran.R

  24. Bytemangler said on July 29, 2010

    abreviations work, too:

    :set nu
    :set nonu

  25. SagarRmali said on July 30, 2010

    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!

  26. Mark Szymanski said on August 31, 2010

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

  27. lancesimms said on September 1, 2010

    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

  28. A Obrzut said on September 12, 2010

    This did not work as root.

  29. cs said on October 4, 2010

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

  30. Anon said on November 4, 2010

    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.

  31. Dsa said on January 24, 2011

    Thanks

  32. pedro said on April 26, 2011

    do :help linenumbers

  33. Ville Aakko said on May 16, 2011

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

  34. Ville Aakko said on May 16, 2011

     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.

  35. Maikel Bald said on August 6, 2011

    Thanks!

  36. linsu said on August 27, 2011

    Thank you

  37. Sathis said on December 21, 2011

    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

  38. Maryam said on January 19, 2012

    Wonderful !!

 

RSS feed for comments on this post. TrackBack URL

Leave a comment -