110

A lot of vim commands can take a number referring to the number of lines that the command will act on.

Is it possible to show the line numbers relative to the current line? Something like the following:

3: some text here
2: more text
1: This is the line above where the cursor is
0: The cursor is on this line
1: This is the line after the cursor
2: More text here
peterh
  • 1,147
  • 3
  • 16
  • 35
Nick J Adams
  • 1,265
  • 2
  • 8
  • 11

4 Answers4

133

I just replied to a similar question here: How can I add line numbers to Vim?

Beginning with version 7.3, you can use the following:

set relativenumber

I actually use both relativenumber and number in my vimrc which will use relative numbers for all lines except the current line.

set number                     " Show current line number
set relativenumber             " Show relative line numbers
Collin Peters
  • 2,246
  • 1
  • 15
  • 9
  • Not sure exactly but the first changelog its mentioned in is 7.3. ftp://ftp.vim.org/pub/vim/patches/7.3/README – Collin Peters Feb 03 '15 at 21:10
  • @aturegano your edits were quite substantial, and I felt they would be better as a new answer than as a part of this one. Others may disagree (your edit is still pending review), but please consider it! – D. Ben Knoble Feb 21 '20 at 15:42
  • 3
    you might enjoy the hybrid mode :set nu rnu – user1068352 Apr 14 '21 at 08:36
  • @user1068352 's comment could also be :set number relativenumber. It's useful those shorter versions of commands, but I like the full version to keep them in mind easier. – Enrique René Mar 26 '22 at 14:03
25

You can toggle relative numbering on and off using:

:set rnu    " toggle relative numbering on
:set rnu!   " toggle relative numbering off
statox
  • 49,782
  • 19
  • 148
  • 225
Matthew Rankin
  • 361
  • 2
  • 6
14

If you want to use a hotkey for toggling relative line number, here is a snippet in my vimrc:

" Toggle relative line number
nmap <C-L><C-L> :set invrelativenumber<CR>

In this case, I use ctrl-L twice to toggle it.

Sean Lee
  • 141
  • 1
  • 2
5
function! NumberToggle()
  if(&relativenumber == 1)
    set norelativenumber
  else
    set relativenumber
  endif
endfunc

nnoremap <leader>nt :call NumberToggle()<cr>
shawndumas
  • 271
  • 1
  • 6
  • 2
    Does the norelativenumber just turn off numbering altogether or does it switch to conventional numbering? – Nick J Adams Feb 03 '15 at 16:59
  • set number is the setting that controls the presences of numbers so it will only switch between rel and non-rel – shawndumas Feb 03 '15 at 17:05
  • 2
    The numbers.vim plugin also adds some more advanced relative number functionality, e.g. having the line your cursor occupies printing the non-relative number, and switching to non-relative numbering throughout the whole file when the vim window loses focus. – bronzehedwick Feb 03 '15 at 17:22
  • 1
    @shawndumas so is there a precedence of which numbers are shown? If for instance I had set number in my vimrc and set relativenumber / norelativenumber was called could it toggle between relative and standard numbering? – Nick J Adams Feb 03 '15 at 17:29
  • 1
    that is most correct – shawndumas Feb 03 '15 at 20:43
  • 1
    simply set rnu! toggles relative and absolute numbers too – Frido Emans Dec 23 '15 at 22:30
  • Is there any benefit to using solution this over the accepted/other answers? – Matt C Apr 04 '18 at 15:54