How To Show Line Numbers In vi / vim Text Editor

by Vivek Gite · 8 comments

This entry is part 1 of 10 in the series VI / VIM Text Editor

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

Series NavigationTurn on or off color syntax highlighting in vi or vim»

Featured Articles:

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 8 comments… read them below or add one }

1 yoander 05.12.08 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.

2 billyduc 05.13.08 at 10:15 am

THat’s really kool

3 Planet Lowyat 05.17.08 at 4:20 pm

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

4 Dean Nguyen 01.16.09 at 4:16 pm

Hi,

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

Thank you!

Dean

5 neeraj sinha 06.07.09 at 9:41 am

Thanks you so much!
it works!

6 hilmy 10.11.09 at 1:44 am

its working.

Thanks!
hilmy

7 woof 10.21.09 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.

8 Phil 11.06.09 at 8:30 pm

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

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous FAQ:

Next FAQ: