Vim Tips Wiki

Changes: Display line numbers


Back to page

(Enabling Line Numbers on Startup)
(Enabling Line Numbers on Startup)
(One intermediate revision by one user not shown)

Latest revision as of 17:17, March 31, 2014

Tip 19 Printable Monobook Previous Next

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

Enabling/Disabling Line Numbers in Vim Edit


This will display line numbers along the left side of a window:

:set number


:set nu


This will turn off the line number display:

:set nonumber


:set nonu


:set nu!

Creating a Mapping to Toggle Line NumbersEdit

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 StartupEdit

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

set number


set nu

Adding Line Numbers Only to Certain FilesEdit

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 WidthEdit

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 numbersEdit

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).



Around Wikia's network

Random Wiki