How To Show Line Numbers In vi / vim Text Editor

by Vivek Gite on May 12, 2008 · 17 comments

Q. How do I display vi / vim text editor line numbers while writing a shell script or c program? How can I turn on or off this feature?

A. Displaying line numbers under vi / vim can be very useful for debugging code errors and to improve overall readability of a program.

vi / vim show line number command

To display line numbers along the left side of a window, type any one of the following:
:set number
or
:set nu


(Fig.01: Vi / Vim line numbers in action - click to enlarge image)
To turn off line number again enter the same command:
:set nu!
If you need number every time you start vi/vim, append following line to your ~/.vimrc file:
set number
Save and close the file.
Jump to particular line number from a shell prompt, enter:
$ vi +linenumber file.c
$ vi +300 initlib.c

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

{ 17 comments… read them below or add one }

1 yoander May 12, 2008

Inside vim editor in command mode you can go directly to specific line pressing line number (ex: 50) and shift g.

Reply

2 billyduc May 13, 2008

THat’s really kool

Reply

3 Planet Lowyat May 17, 2008

Can VI display 3 desire lines such as line x(10), line y(25) and line z(50) only?

Reply

4 Dean Nguyen January 16, 2009

Hi,

The “:set number or :set nu” is working great in VI.

Thank you!

Dean

Reply

5 neeraj sinha June 7, 2009

Thanks you so much!
it works!

Reply

6 hilmy October 11, 2009

its working.

Thanks!
hilmy

Reply

7 woof October 21, 2009

Inside vim editor in command mode you can go directly to specific line pressing line number (ex: 50) and shift g.

Or colon, line number, and Enter (e.g. :50 [enter]). It does the same thing, but is my personal preference over the 50G option.

Reply

8 Phil November 6, 2009

Does not work in gvim with vim6.3 in centOS.

Reply

9 Tapas May 8, 2010

I use Debian you need to create .vimrc in your home directory for that.
Also syntax on will help

Reply

10 myrabelle cabriana March 8, 2011

thank uuuuuuuuuuuuu.hehe

Reply

11 Venu March 9, 2011

Previous post not properly added

Hi,
vi “file name” +
vi + will open the file and cursor will be at specified line.

If the format is
vi “file name” :

vi : – How can I get the same behavior.

thnx
-Venu

Reply

12 rojah May 24, 2011

exactly what i was looking for. thank you.

Reply

13 sachin June 1, 2011

Thanks

Reply

14 toto August 2, 2011

this is great tutorial. I can see line number in my vim editor. thanks

Reply

15 senthil August 18, 2011

its working .thx

Reply

16 Ramesh September 13, 2011

thanks

Reply

17 Sandip September 15, 2011

You can also add:
set number
~/.vimrc

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 15 + 11 ?
Please leave these two fields as-is:
Solve the simple math so we know that you are a human and not a bot.




Previous post:

Next post: