109

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

4 Answers 4

132

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
4
  • Not sure exactly but the first changelog its mentioned in is 7.3. ftp.vim.org/pub/vim/patches/7.3/README Feb 3, 2015 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, 2020 at 15:42
  • 2
    you might enjoy the hybrid mode :set nu rnu Apr 14, 2021 at 8: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. Mar 26, 2022 at 14:03
24

You can toggle relative numbering on and off using:

:set rnu    " toggle relative numbering on
:set rnu!   " toggle relative numbering off
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.

5
function! NumberToggle()
  if(&relativenumber == 1)
    set norelativenumber
  else
    set relativenumber
  endif
endfunc

nnoremap <leader>nt :call NumberToggle()<cr>
7
  • 2
    Does the norelativenumber just turn off numbering altogether or does it switch to conventional numbering? Feb 3, 2015 at 16:59
  • 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. Feb 3, 2015 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? Feb 3, 2015 at 17:29
  • 1
    that is most correct
    – shawndumas
    Feb 3, 2015 at 20:43
  • 1
    simply set rnu! toggles relative and absolute numbers too Dec 23, 2015 at 22:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.