Show line numbers in vi or vim
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










trilobites said on October 2, 2008
wow, thanks. very helpful!
thanks said on October 18, 2008
thanks
pandiano said on November 9, 2008
MANY THX!!!!!!!!!!!!!
robertmarkbram said on December 17, 2008
Helped me!
tom said on January 23, 2009
helped me too!
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.
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.
Sandy said on February 5, 2009
Thanks. It was helpful for me
Joe said on February 10, 2009
Nice… had forgotten my vi
Vasanth said on March 16, 2009
thanks it helped!
Dave said on March 20, 2009
too easy.. :-)
James said on May 13, 2009
Good tip. I get horizontal lines underneath the numbers, is that supposed to happen?
Anonymous said on June 16, 2009
You can also add:
set number
~/.vimrc
Dushi said on August 20, 2009
that was so helpful. thanx…
Tim Broder said on September 8, 2009
sweet, thanks
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.
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!
Anonymous said on December 10, 2009
first time – comments are important more then the post
Anonymous said on December 24, 2009
Thanks, it worked for me. (gvim version 7.2)
Anonymous said on February 7, 2010
read the instruction for configuring .vimrc file
tojo said on March 9, 2010
thanks krab
Opalinux said on June 3, 2010
very useful thx ! ! !
Ramy Mousa said on June 24, 2010
How to save a file with numbers ,Rathnasekaran.R
Bytemangler said on July 29, 2010
abreviations work, too:
:set nu
:set nonu
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!
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
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
A Obrzut said on September 12, 2010
This did not work as root.
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!
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.
Dsa said on January 24, 2011
Thanks
pedro said on April 26, 2011
do :help linenumbers
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)
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.
Maikel Bald said on August 6, 2011
Thanks!
linsu said on August 27, 2011
Thank you
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
Maryam said on January 19, 2012
Wonderful !!