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.

  • trilobites

    wow, thanks. very helpful!

  • thanks

    thanks

  • pandiano

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

  • robertmarkbram

    Helped me!

  • http://www.thdyoung.com/scrapbook/ tom

    helped me too!

  • Rob

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

    • http://www.tech-recipes.com Quinn McHenry

      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.

      • Mark Szymanski

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

  • Sandy

    Thanks. It was helpful for me

  • Joe

    Nice… had forgotten my vi

  • Vasanth

    thanks it helped!

  • Dave

    too easy.. :-)

  • James

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

  • Anonymous

    You can also add:
    set number
    ~/.vimrc

  • http://dushan888.wordpress.com/ Dushi

    that was so helpful. thanx…

  • http://timbroder.com Tim Broder

    sweet, thanks

  • Rick Graham

    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.

    • Cédric Bosdonnat

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

      • Ville Aakko

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

        • Ville Aakko

           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.

  • Anonymous

    first time – comments are important more then the post

  • Anonymous

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

  • Anonymous

    read the instruction for configuring .vimrc file

  • http://twitter.com/totsapium tojo

    thanks krab

  • Opalinux

    very useful thx ! ! !

  • Ramy Mousa

    How to save a file with numbers ,Rathnasekaran.R

  • Bytemangler

    abreviations work, too:

    :set nu
    :set nonu

  • SagarRmali

    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!

  • lancesimms

    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

    • A Obrzut

      This did not work as root.

      • Anon

        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.

  • cs

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

  • Dsa

    Thanks

  • pedro

    do :help linenumbers

  • http://www.facebook.com/people/Maikel-Bald/1674261727 Maikel Bald

    Thanks!

  • linsu

    Thank you

  • Sathis

    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

  • Maryam

    Wonderful !!

  • Edwin

    Thanks… its useful to me…

  • L

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

  • Baroda

    Useful..
    Thanks..

  • Zhang Chunyan

    It is working. Thank you for your sharing!

  • visitor

    Thanks!