Show Line Numbers in Vi or Vim
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.




October 02, 2008 at 6:32 pm, trilobites said:
wow, thanks. very helpful!
October 18, 2008 at 3:03 pm, thanks said:
thanks
November 09, 2008 at 2:17 pm, pandiano said:
MANY THX!!!!!!!!!!!!!
December 17, 2008 at 6:15 am, robertmarkbram said:
Helped me!
January 23, 2009 at 9:35 pm, tom said:
helped me too!
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.
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.
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
February 05, 2009 at 9:28 pm, Sandy said:
Thanks. It was helpful for me
February 10, 2009 at 4:29 am, Joe said:
Nice… had forgotten my vi
March 16, 2009 at 3:45 pm, Vasanth said:
thanks it helped!
March 20, 2009 at 2:15 pm, Dave said:
too easy..
May 13, 2009 at 3:59 pm, James said:
Good tip. I get horizontal lines underneath the numbers, is that supposed to happen?
June 16, 2009 at 5:37 pm, Anonymous said:
You can also add:
set number
~/.vimrc
August 20, 2009 at 12:01 pm, Dushi said:
that was so helpful. thanx…
September 08, 2009 at 5:29 pm, Tim Broder said:
sweet, thanks
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.
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!
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)
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.
December 10, 2009 at 8:23 am, Anonymous said:
first time – comments are important more then the post
December 24, 2009 at 1:54 pm, Anonymous said:
Thanks, it worked for me. (gvim version 7.2)
February 07, 2010 at 3:30 am, Anonymous said:
read the instruction for configuring .vimrc file
March 09, 2010 at 9:51 am, tojo said:
thanks krab
June 03, 2010 at 9:20 am, Opalinux said:
very useful thx ! ! !
June 24, 2010 at 6:37 am, Ramy Mousa said:
How to save a file with numbers ,Rathnasekaran.R
July 29, 2010 at 9:42 pm, Bytemangler said:
abreviations work, too:
:set nu
:set nonu
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!
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
September 12, 2010 at 1:52 am, A Obrzut said:
This did not work as root.
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.
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!
January 24, 2011 at 7:14 am, Dsa said:
Thanks
April 26, 2011 at 10:06 pm, pedro said:
do :help linenumbers
August 06, 2011 at 11:52 pm, Maikel Bald said:
Thanks!
August 27, 2011 at 3:34 am, linsu said:
Thank you
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
January 19, 2012 at 6:27 am, Maryam said:
Wonderful !!
April 16, 2012 at 12:55 pm, Edwin said:
Thanks… its useful to me…
May 04, 2012 at 10:32 am, L said:
Perfect, thanks!
First result on google when searching for ‘vim line number’, btw.
June 01, 2012 at 8:20 am, Baroda said:
Useful..
Thanks..
July 19, 2012 at 8:51 am, Zhang Chunyan said:
It is working. Thank you for your sharing!