2

I have found this very useful function to turn on and off relative number lines:

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

but it doesn't work, when I launch it

:call NumberToggle()

I see no changes, even when I close and reopen vim.

The source of the function is here with other useful related tricks.

2 Answers 2

4

The intention of the function as described in the blog post is not to toggle relativenumber but to switch between the number and relativenumber settings.

In Vim 7.3, the number and relativenumber settings could not be used at the same time, and I believe setting one automatically unset the other.

However, as of Vim 7.4, you can enable both settings at the same time. Doing so will use the absolute line number for the current line, and relative line numbers for all other lines.

If you don't wish to do this, and do in fact want to switch between using relative line numbers everywhere or absolute line numbers everywhere, you can amend the function accordingly:

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

Alternatively, you could just leave number on at all times, and just toggle relativenumber, which, as grodzik has explained, you can do with a simple: set relativenumber!. This will switch between displaying absolute line numbers everywhere, and displaying it only on the current line (with relative numbers elsewere).

1
  • I think this explains best what is going on in the function. I would advise Three Diag to read the Vim help on 'number' for an example of what each option does. Generally speaking the Vim help is a good place for finding answers.
    – Octaviour
    Nov 29, 2016 at 15:48
7

Why won't you use Vim's builtin feature to toggle that? Just use: set relativenumber! - this will cycle between on and off.

5
  • I want to bind it as suggested in the link, as nnoremap <C-n> :call NumberToggle()<cr> can I do that with :set ... ! ?
    – Three Diag
    Nov 28, 2016 at 21:49
  • Yet, shouldn't the above work too? What's amiss with it?
    – Three Diag
    Nov 29, 2016 at 0:10
  • 1
    It doesn't turn anything off, and toggle should at some point do that. number and relativenumber aren't excluding each other, and I think that was the assumption here. When you set relativenumber, you get relative numbers with current line being 0. When you add to that :set number you still will have relative numbers, but current line will be marked with actual number of current line in a buffer.
    – grodzik
    Nov 29, 2016 at 0:35
  • In other words, replace "set number" with "set norelativenumber". Nov 29, 2016 at 0:36
  • That assumption might be true for Vim 7.3, though
    – grodzik
    Nov 29, 2016 at 0:38

Your Answer

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

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