Vim Tips Wiki
No edit summary
 
No edit summary
Tags: rollback sourceedit
(28 intermediate revisions by 17 users not shown)
Line 1: Line 1:
  +
{{TipImported
{{review}}
 
{{Tip
 
 
|id=19
 
|id=19
  +
|previous=18
|title=line numbers...
 
  +
|next=20
|created=February 25, 2001 22:17
+
|created=February 25, 2001
 
|complexity=basic
 
|complexity=basic
|author=scrott--AT--users.sourceforge.net
+
|author=scrott
|version=5.7
+
|version=7.3
 
|rating=446/180
 
|rating=446/180
  +
|category1=Options
|text=
 
  +
|category2=
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
 
 
}}
 
}}
  +
It is sometimes useful to display line numbers so the number of each line can easily be seen.
   
== Comments ==
+
===Enabling===
 
To display line numbers:
A faster way to do this is
 
  +
<pre>
 
:set number
  +
</pre>
  +
or:
  +
<pre>
 
:set nu
 
:set nu
  +
</pre>
   
  +
===Disabling===
the_voice666--AT--hotmail.com
 
  +
This will turn off the line number display:
, August 19, 2001 17:00
 
  +
<pre>
----
 
  +
:set nonumber
An even faster way to do this :) is
 
  +
</pre>
 
  +
or:
:se nu
 
  +
<pre>
 
  +
:set nonu
bindu--AT--wavell
 
  +
</pre>
, November 11, 2001 13:48
 
----
 
An even better way, if you like maps: Put this in your .vimrc file:
 
:nmap &lt;C-N&gt;&lt;C-N&gt; :set invnumber &lt;CR&gt;
 
By pressing Ctrl-N twice (or some other shortcut you fancy) in normal mode, you can toggle between showing and hiding line numbers.
 
 
jalmeida--AT--math.ist.utl.pt
 
, May 20, 2004 5:39
 
----
 
and to get the line numbers on hardcopies too use:
 
 
:set printoptions=number:y
 
 
lativyn {at} dcs.gla.ac.uk
 
, November 22, 2004 14:53
 
----
 
Even better--
 
:se nu!
 
 
That /toggles/ line numbers, so when you're ready to lose them, the toggle is in your history.
 
 
'''Anonymous'''
 
, April 22, 2005 11:40
 
----
 
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.
 
 
spliffy
 
, July 19, 2005 17:33
 
----
 
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.
 
 
sfarliegh--AT--earthlink.net
 
, August 9, 2005 6:50
 
----
 
So, is it possible to set the gutter to a resonable size? In my files I'd hardly use 3-digit line numbers.
 
   
  +
The following command is useful because it toggles the display of line numbers. Assuming no numbers are currently displayed, this command will display them. Entering the command again will hide them.
gurdiga--AT--gmail.com
 
  +
<pre>
, October 16, 2005 5:22
 
 
:set nu!
----
 
  +
</pre>
Yes, I hardly use read code with more than 9999 lines as well... some solution would be nice.
 
   
  +
===Mapping to toggle line numbers===
anders ttaa panikk ttoodd org
 
  +
You can also define a mapping to toggle the option, for example:
, October 19, 2005 17:02
 
  +
<pre>
----
 
 
:nmap <C-N><C-N> :set invnumber<CR>
I also would like to know about changing the line numbers so that it doesn't take so much space. That would be awesome.
 
  +
</pre>
   
 
By pressing Ctrl-N twice in normal mode, Vim toggles between showing and hiding line numbers.
floresg2--AT--gmail.com
 
, November 10, 2005 11:41
 
----
 
according to :he 'nu', look at :he 'nuw'
 
   
  +
==Enabling line numbers on startup==
'numberwidth' 'nuw' number (Vim default: 4 Vi default: 8)
 
  +
To enable line numbers on startup, simply add the following to your [[vimrc]].
Minimal number of columns to use for the line number. Only relevant when the 'number' option is set.
 
  +
<pre>
Since one space is always between the number and the text, there is one less character for the number itself.
 
  +
set number
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.
 
  +
</pre>
The minimum value is 1, the maximum value is 10.
 
   
  +
==Adding line numbers only to certain files==
HTH. -s
 
  +
Create a filetype plugin for each filetype where you'd like to have numbering enabled (see {{help|ftplugin-overrule}}) and add the following line:
  +
<pre>
  +
setl number
  +
</pre>
   
  +
==Changing gutter column width==
projects--AT--caelyx.net
 
  +
If you have Vim version 7 or greater, you can change the width of the "gutter" column used for numbering:
, December 12, 2005 1:34
 
  +
<pre>
----
 
Please note that "set numberwidth" only available in VIM 7.
+
:set numberwidth=3
  +
</pre>
Standard VIM 6.x version doesn't support this.
 
   
  +
You can use the number column for the text of wrapped lines:
twinchang--AT--hotmail.com
 
  +
<pre>
, January 1, 2006 1:41
 
  +
:set cpoptions+=n
----
 
  +
</pre>
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.
 
   
  +
Finally, you can change the color used for the line numbers. For example:
nmruser--AT--gmail.com
 
  +
<pre>
, April 24, 2006 12:39
 
 
:highlight LineNr term=bold cterm=NONE ctermfg=DarkGrey ctermbg=NONE gui=NONE guifg=DarkGrey guibg=NONE
----
 
  +
</pre>
"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!
 
   
  +
==Relative line numbers==
anonymous
 
  +
For some commands, it is easier to know how many lines a give bit of text is, relative to the current cursor position. For example, moving with <code>j</code> and <code>k</code> with a count like <code>5j</code>; or deleting 8 lines with <code>8dd</code> can be easier if you have an at-a-glance view of distance from the cursor line instead of distance from the top of the file as with <code>:set number</code>.
, July 11, 2006 12:41
 
----
 
&gt; Is it possible to set line number 1 from a certain line, [...]
 
&gt; want to count how many lines the following paragraph hold [...]
 
   
  +
To display line numbers relative to the line with the cursor, use:
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.
 
  +
<pre>
  +
:set relativenumber
  +
</pre>
   
  +
{{help|prefix=no|'relativenumber'}} is not a complete replacement for {{help|prefix=no|'number'}}; rather, these two options interact so that you can show only relative numbers (<code>number</code> off and <code>relativenumber</code> on), only absolute line numbers (<code>relativenumber</code> off and <code>number</code> on), or show the absolute number on the cursor line and relative numbers everywhere else (both <code>relativenumber</code> and <code>number</code> on).
denilsonsa--AT--gmail.com
 
, July 28, 2006 19:32
 
----
 
Is it possible to choose the color for line numbers?
 
   
  +
==References==
okocha
 
  +
*{{help|'number'}}
, August 30, 2006 7:21
 
  +
*{{help|'numberwidth'}} (Vim 7 and greater only)
----
 
  +
*{{help|'relativenumber'}} (Vim 7.3 and greater only)
highlight LineNr term=bold cterm=NONE ctermfg=DarkGrey ctermbg=NONE gui=NONE guifg=DarkGrey guibg=NONE
 
  +
*{{help|:highlight}}
that shud do.!
 
   
  +
==Comments==
codemedic--AT--codeblocks.org
 
, September 4, 2006 3:25
 
----
 
<!-- parsed by vimtips.py in 0.494542 seconds-->
 

Revision as of 22:12, 29 April 2015

Tip 19 Printable Monobook Previous Next

created February 25, 2001 · complexity basic · author scrott · version 7.3


It is sometimes useful to display line numbers so the number of each line can easily be seen.

Enabling

To display line numbers:

:set number

or:

:set nu

Disabling

This will turn off the line number display:

:set nonumber

or:

:set nonu

The following command is useful because it toggles the display of line numbers. Assuming no numbers are currently displayed, this command will display them. Entering the command again will hide them.

:set nu!

Mapping to toggle line numbers

You can also define a mapping to toggle the option, for example:

:nmap <C-N><C-N> :set invnumber<CR>

By pressing Ctrl-N twice in normal mode, Vim toggles between showing and hiding line numbers.

Enabling line numbers on startup

To enable line numbers on startup, simply add the following to your vimrc.

set number

Adding line numbers only to certain files

Create a filetype plugin for each filetype where you'd like to have numbering enabled (see :help ftplugin-overrule) and add the following line:

setl number

Changing gutter column width

If you have Vim version 7 or greater, you can change the width of the "gutter" column used for numbering:

:set numberwidth=3

You can use the number column for the text of wrapped lines:

:set cpoptions+=n

Finally, you can change the color used for the line numbers. For example:

:highlight LineNr term=bold cterm=NONE ctermfg=DarkGrey ctermbg=NONE gui=NONE guifg=DarkGrey guibg=NONE

Relative line numbers

For some commands, it is easier to know how many lines a give bit of text is, relative to the current cursor position. For example, moving with j and k with a count like 5j; or deleting 8 lines with 8dd can be easier if you have an at-a-glance view of distance from the cursor line instead of distance from the top of the file as with :set number.

To display line numbers relative to the line with the cursor, use:

:set relativenumber

'relativenumber' is not a complete replacement for 'number'; rather, these two options interact so that you can show only relative numbers (number off and relativenumber on), only absolute line numbers (relativenumber off and number on), or show the absolute number on the cursor line and relative numbers everywhere else (both relativenumber and number on).

References

Comments