How To Show Line Numbers In vi / vim Text Editor

by on May 12, 2008 · 32 comments· last updated at May 12, 2008

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



You should follow me on twitter here or grab rss feed to keep track of new changes.

Featured Articles:

{ 32 comments… read them below or add one }

1 yoander May 12, 2008 at 2:27 pm

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 at 10:15 am

THat’s really kool

Reply

3 Planet Lowyat May 17, 2008 at 4:20 pm

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 at 4:16 pm

Hi,

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

Thank you!

Dean

Reply

5 neeraj sinha June 7, 2009 at 9:41 am

Thanks you so much!
it works!

Reply

6 hilmy October 11, 2009 at 1:44 am

its working.

Thanks!
hilmy

Reply

7 woof October 21, 2009 at 9:44 pm

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 at 8:30 pm

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

Reply

9 vlayne January 12, 2013 at 6:32 pm

Works with vi & vim in Centos 6.3

noarch:printing-4.0-amd64:printing-4.0-noarch
Distributor ID: CentOS
Description: CentOS release 6.3 (Final)
Release: 6.3
Codename: Final

Reply

10 Tapas May 8, 2010 at 7:04 pm

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

Reply

11 myrabelle cabriana March 8, 2011 at 5:02 am

thank uuuuuuuuuuuuu.hehe

Reply

12 Venu March 9, 2011 at 6:54 am

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

13 rojah May 24, 2011 at 8:50 pm

exactly what i was looking for. thank you.

Reply

14 sachin June 1, 2011 at 10:41 am

Thanks

Reply

15 toto August 2, 2011 at 6:33 am

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

Reply

16 senthil August 18, 2011 at 9:25 am

its working .thx

Reply

17 Ramesh September 13, 2011 at 5:48 pm

thanks

Reply

18 Sandip September 15, 2011 at 10:30 am

You can also add:
set number
~/.vimrc

Reply

19 toto March 20, 2012 at 4:50 pm

thank you for this info. My vim is look more beautiful :)

Reply

20 Malek March 21, 2012 at 12:32 pm

this is why I like vim

Reply

21 w May 30, 2012 at 8:50 am

Thanks!

Reply

22 Mike W June 8, 2012 at 7:36 pm

This works in Poderosa too. Anyone know how to just turn on a display at the bottom of the screen that shows which line you’re on, as an alternative to having all the rows numbered? Poderosa does this on my Dreamhost account but not on another site I use, don’t know why.

Reply

23 Melanie July 16, 2012 at 6:52 pm

I have been to your site before and was back again searching for displaying the line numbers. Thank you for this quick bit of help.

Reply

24 Scarlet July 18, 2012 at 3:26 am

it’s helpful for beginners,thx

Reply

25 sh October 30, 2012 at 7:25 am

Thanks

Reply

26 Debabrata Banik November 14, 2012 at 6:01 am

Thanks, Its working.

Reply

27 rajagopal November 26, 2012 at 1:13 pm

How can i get current cursor number in vi editor???

Reply

28 vlayne January 12, 2013 at 6:35 pm

launch vi –> vi ENTER
type –> :set number ENTER
Now as you press down or up, you will see the number value change on the bottom right.

Reply

29 newbee February 22, 2013 at 4:10 am

.vimrc not available at ~/
now where should I add “set number”

Reply

30 sengseng February 23, 2013 at 9:35 pm

@newbee: Are you using a Debian-based system? If so, .vimrc is not included in your home dir. You can create the file by doing this at your command prompt:

$ vi ~/.vimrc

You’ll then be inside the vi editor. Press “i” for insert mode and type “set number” into the first line of the editor.

Once you are done, press ESC, then type ZZ and press Enter. You’ll automatically write the file and exit to your command prompt. You’ll now see .vimrc in your home dir.

You should now be able to see line numbers when you vi a file.

Last edited by Admin; 24th Feb 2013 at 9:59 AM. Reason: Correction.

Reply

31 soreana April 7, 2013 at 2:45 pm

You solve my problem in shortest waythat possible.
thank you

Reply

32 preet May 23, 2013 at 9:06 am

tnks really useful ……….

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <kbd> <blockquote> <pre> <a href="" title="">

Tagged as: , , , , , , , ,

Previous Faq:

Next Faq: