m (Line numbers... moved to Display line numbers: Page moved by JohnBot to improve title) |
m (Reverted edits by 155.63.240.39 (talk | block) to last version by 189.112.39.13) |
||
(19 intermediate revisions by 13 users not shown) | |||
Line 1: | Line 1: | ||
+ | {{TipImported |
||
− | {{Tip |
||
|id=19 |
|id=19 |
||
+ | |previous=18 |
||
⚫ | |||
+ | |next=20 |
||
|created=February 25, 2001 |
|created=February 25, 2001 |
||
|complexity=basic |
|complexity=basic |
||
|author=scrott |
|author=scrott |
||
− | |version= |
+ | |version=7.3 |
|rating=446/180 |
|rating=446/180 |
||
+ | |category1=Options |
||
− | |text= |
||
+ | |category2= |
||
}} |
}} |
||
+ | == Enabling/Disabling Line Numbers in Vim == |
||
+ | ===Enabling=== |
||
− | I have started doing all my code reviews on a laptop because of the number command. |
||
− | |||
This will display line numbers along the left side of a window: |
This will display line numbers along the left side of a window: |
||
− | + | <pre>:set number</pre> |
|
+ | or: |
||
+ | |||
+ | <pre>:set nu</pre> |
||
+ | |||
+ | ===Disabling=== |
||
+ | This will turn off the line number display: |
||
+ | |||
+ | <pre>:set nonumber</pre> |
||
+ | |||
+ | or: |
||
+ | |||
+ | <pre>:set nonu</pre> |
||
+ | |||
+ | or: |
||
+ | <pre>:set nu!</pre> |
||
+ | |||
+ | ===Creating a Mapping to Toggle Line Numbers=== |
||
You can also define a mapping to toggle the option, for example: |
You can also define a mapping to toggle the option, for example: |
||
− | + | <pre>:nmap <C-N><C-N> :set invnumber<CR></pre> |
|
By pressing Ctrl-N twice in normal mode, Vim toggles between showing and hiding line numbers. |
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]] |
||
+ | |||
+ | <pre>set number</pre> |
||
+ | |||
+ | or: |
||
+ | |||
+ | <pre>set nu</pre> |
||
+ | |||
+ | ==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 to it: |
||
+ | |||
+ | <code>setl number</code> |
||
+ | |||
+ | ==Changing Gutter Column Width== |
||
If you have Vim version 7 or greater, you can change the width of the "gutter" column used for numbering: |
If you have Vim version 7 or greater, you can change the width of the "gutter" column used for numbering: |
||
− | + | <pre>:set numberwidth=3</pre> |
|
You can use the number column for the text of wrapped lines: |
You can use the number column for the text of wrapped lines: |
||
− | + | <pre>:set cpoptions+=n</pre> |
|
Finally, you can change the color used for the line numbers. For example: |
Finally, you can change the color used for the line numbers. For example: |
||
− | + | <pre>:highlight LineNr term=bold cterm=NONE ctermfg=DarkGrey ctermbg=NONE gui=NONE guifg=DarkGrey guibg=NONE</pre> |
|
+ | |||
⚫ | |||
+ | 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>. |
||
+ | |||
+ | To display line numbers relative to the line with the cursor, use: |
||
+ | |||
+ | <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). |
||
==References== |
==References== |
||
*{{help|'number'}} |
*{{help|'number'}} |
||
*{{help|'numberwidth'}} (Vim 7 and greater only) |
*{{help|'numberwidth'}} (Vim 7 and greater only) |
||
+ | *{{help|'relativenumber'}} (Vim 7.3 and greater only) |
||
*{{help|:highlight}} |
*{{help|:highlight}} |
||
− | == |
+ | ==Comments== |
− | [[Category:Options]] |
Revision as of 15:48, 14 August 2014
created February 25, 2001 · complexity basic · author scrott · version 7.3
Enabling/Disabling Line Numbers in Vim
Enabling
This will display line numbers along the left side of a window:
:set number
or:
:set nu
Disabling
This will turn off the line number display:
:set nonumber
or:
:set nonu
or:
:set nu!
Creating a 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
or:
set nu
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 to it:
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
- :help 'number'
- :help 'numberwidth' (Vim 7 and greater only)
- :help 'relativenumber' (Vim 7.3 and greater only)
- :help :highlight