Show line numbers in vi or vim

Contributor Icon Contributed by Rex Date Icon February 16, 2004  
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 vi and vim.


While editing a document (and not inserting text), type the following to turn on line numbers:

:set number

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

:set nonumber

Previous recipe | Next recipe |
 
  • penno
    Does anyone know how to make line numbers the default?? :?
  • soorena776
    read the instruction for configuring .vimrc file
  • qmchenry
    If you are in a UNIX operating system, you can create a file in your home directory called .exrc that vi will read when it starts. Just put set number on its own line as well as any other set. To see what other options are available and their current values, use :set all from within vi.
  • penno
    That works wonderfully! Thank you very much!! :D
  • Anonymous
    Alternativley you can see the current cursor postiton with :set ruler
  • trilobites
    wow, thanks. very helpful!
  • thanks
    thanks
  • pandiano
    MANY THX!!!!!!!!!!!!!
  • robertmarkbram
    Helped me!
  • tom
    helped me too!
  • Rob
    Is it possible to get vim to show line numbers by default? I need them more often than not.
  • 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.
  • 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?
  • ginetta
    You can also add:
    set number
    ~/.vimrc
  • that was so helpful. thanx...
  • sweet, thanks
  • Rick Graham
    This is what I was looking for.

    Here's what I did:

    Edit ~/.vimrc

    add these lines:

    nnoremap <silent> <F6> :set number<CR>
    nnoremap <silent> :set nonumber<CR>

    Now, F6 turns line numbers on, Shift F6 turns them off.
  • @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 <silent> <F6> :set number!<CR>
  • sud
    first time - comments are important more then the post
  • mostafamahdieh
    Thanks, it worked for me. (gvim version 7.2)
blog comments powered by Disqus