created February 25, 2001 · complexity basic · author scrott · version 7.3
It is sometimes useful to display line numbers in the left margin, for reference. Or to display current line/column in the status line, `set ruler` in your ~/.vimrc file.
Enabling left-margin display[edit | edit source]
To display line numbers:
Disabling[edit | edit source]
This will turn off the line number display:
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.
Mapping to toggle line numbers[edit | edit source]
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.
If you want to be able to toggle line numbers both in normal and insert mode, you can define these two mappings (in this example, they're bound to the F3 key):
noremap <F3> :set invnumber<CR> inoremap <F3> <C-O>:set invnumber<CR>
Enabling line numbers on startup[edit | edit source]
To enable line numbers on startup, simply add the following to your vimrc.
Adding line numbers only to certain files[edit | edit source]
Create a filetype plugin for each filetype where you'd like to have numbering enabled (see :help ftplugin-overrule) and add the following line:
Changing gutter column width[edit | edit source]
If you have Vim version 7 or greater, you can change the width of the "gutter" column used for numbering:
You can use the number column for the text of wrapped lines:
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[edit | edit source]
For some commands, it is easier to know how many lines a given bit of text is, relative to the current cursor position. For example, moving with
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
To display line numbers relative to the line with the cursor, use:
'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
References[edit | edit source]
- :help 'number'
- :help 'numberwidth' (Vim 7 and greater only)
- :help 'relativenumber' (Vim 7.3 and greater only)
- :help :highlight