sponsor Vim development Vim logo Vim Book Ad

basic Tip #19: line numbers...

 tip karma   Rating 509/210, Viewed by 23096 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   February 25, 2001 22:17      complexity:   basic
author:   [email protected]      as of Vim:   5.7

I have started doing all my code reviews on a laptop because of the number command.

:set number will put line numbers along the left side of a window

:help number

 rate this tip  Life Changing Helpful Unfulfilling 

<< Cleanup your HTML | Are *.swp and *~ files littering your working directory? >>

Additional Notes

[email protected], August 19, 2001 17:00
A faster way to do this is
:set nu
bindu@wavell, November 11, 2001 13:48
An even faster way to do this :) is

:se nu
[email protected], May 20, 2004 5:39
An even better way, if you like maps: Put this in your .vimrc file:  
:nmap <C-N><C-N> :set invnumber <CR>
By pressing Ctrl-N twice  (or some other shortcut you fancy) in normal mode, you can toggle between showing and hiding line numbers.
lativyn {at} dcs.gla.ac.uk, November 22, 2004 14:53
and to get the line numbers on hardcopies too use:

    :set printoptions=number:y
Anonymous, April 22, 2005 11:40
Even better--
:se nu!

That /toggles/ line numbers, so when you're ready to lose them, the toggle is in your history.
spliffy, July 19, 2005 17:33
Is there a way to adjust the width of the gutter used by the line numbers? Right now it leaves enough room for several thousand lines, and rarely are my files more than a few hundred. It would be nice if it would only take up as much room as necessary.
[email protected], August 9, 2005 6:50
spliffy asked about the gutter size. Try setting the printoption left:0in. Originally I used left:0pc and it did nothing. This will move everything, including header to the left.
[email protected], October 16, 2005 5:22
So, is it possible to set the gutter to a resonable size? In my files I'd hardly use 3-digit line numbers.
anders ttaa panikk ttoodd org, October 19, 2005 17:02
Yes, I hardly use read code with more than 9999 lines as well... some solution would be nice.
[email protected], November 10, 2005 11:41
I also would like to know about changing the line numbers so that it doesn't take so much space. That would be awesome.
[email protected], December 12, 2005 1:34
according to :he 'nu', look at :he 'nuw'

'numberwidth' 'nuw' number (Vim default: 4  Vi default: 8)
Minimal number of columns to use for the line number.  Only relevant when the 'number' option is set.
Since one space is always between the number and the text, there is one less character for the number itself.
The value is the minimum width.  A bigger width is used when needed to fit the highest line number in the buffer.  Thus with the Vim default of 4 there is room for a line number up to 999.  When the buffer has 1000 lines five columns will be used.
The minimum value is 1, the maximum value is 10.

HTH. -s
[email protected], January 1, 2006 1:41
Please note that "set numberwidth" only available in VIM 7.
Standard VIM 6.x version doesn't support this.
[email protected], April 24, 2006 12:39
Is it possible to set line number 1 from a certain line, say when I 'm in the middle of the file and want to count how many lines the following paragraph hold, so if I set current line as line 1, I'll know intuitively the number of lines.
anonymous, July 11, 2006 12:41
"set numberwidth" (available in version 7) sounds like exactly what I want, but does anyone have a suggestion making the "set number" gutter less wide on 6.x?  Thanks!
[email protected], July 28, 2006 19:32
> Is it possible to set line number 1 from a certain line, [...]
> want to count how many lines the following paragraph hold [...]

I think it is not possible using "set number", but you can "set sc" (set showcmd). When enabled, the number of characters or lines selected in visual mode will be shown at bottom of the window.
okocha, August 30, 2006 7:21
Is it possible to choose the color for line numbers?
[email protected], September 4, 2006 3:25
highlight LineNr      term=bold cterm=NONE ctermfg=DarkGrey ctermbg=NONE gui=NONE guifg=DarkGrey guibg=NONE
that shud do.!
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
Sponsored by Web Concept Group Inc. SourceForge.net Logo