18

with

setl nu!

I can toggle number (on/off), similar with relativenumber

setl rnu!

how I can toggle {off,number,relativenumber} ?

2
  • 5
    aw. why on earth was this question migrated to SU? It is a programming question related to a prorgramming tool. It doesn't get more SO than that. The programmers that focus on SO (like me) would now not be able to find this answer because 5 people don't know what vim is. Go figure. META: meta.stackexchange.com/questions/25925/vim-questions-so-or-su
    – sehe
    Sep 25, 2011 at 12:01
  • @sehe, I agree , this is a question for stackoverflow
    – juanpablo
    Sep 25, 2011 at 15:52

6 Answers 6

11
if &nu == 1
   set rnu
elseif &rnu == 1
   set nornu
else
   set nu
endif
3
  • thanks Benoit, I added your solution as a gist
    – juanpablo
    Sep 24, 2011 at 20:17
  • 1
    @JuanPablo: mmm. I like my version better :)
    – sehe
    Sep 24, 2011 at 20:19
  • 1
    @sehe: I'd say the same about my version. :-)
    – ib.
    Sep 25, 2011 at 3:51
19

Because I love a logic puzzle, and really love it when a vim command fits on a single line for succinct repeats (@: is a personal favourite):

:exec &nu==&rnu? "se nu!" : "se rnu!"

This will maintain the same cycle. I think it is mainly because let &nu=1 will implicitly set norelativenumber - for reasons probably found in the documentation :)

13
  • 4
    +1 as I can place it in my .vimrc file as one line of nmap <F3> :exec &nu==&rnu? "se nu!" : "se rnu!"<CR> to toggle through three options using F3.
    – nye17
    Sep 24, 2011 at 23:38
  • 4
    you could make that nnoremap <silent> <F3>... to reduce visual distraction and interfering with other mappings.
    – sehe
    Sep 24, 2011 at 23:56
  • I came up with almost the same (but shorter :-) line when read the question: exe'se'&nu+&rnu?'rnu!':'nu'.
    – ib.
    Sep 25, 2011 at 2:51
  • By the way, to assure yourself of the connection between number and relativenumber options, see :helpg When setting this option.
    – ib.
    Sep 25, 2011 at 2:54
  • @ib: thanks for sharing. I really love your vim code golfs - there are gems in there that really speed up my everyday work; In this case, however, I have specificly selected my version (a net 3 characters longer - besides whtiespace cramp) because it is legible. In my view, there isn't any gain from condensing it further: it does get harder to type and a lot harder to remember even if you ever had to type it from memory. This is going to be in a mapping. So Benoit's version is fine, except for it not fitting nicely on a single line
    – sehe
    Sep 25, 2011 at 12:08
8

For those who would like a more readable solution, the following is what I have in my .vimrc

" Relative or absolute number lines
function! NumberToggle()
    if(&nu == 1)
        set nu!
        set rnu
    else
        set nornu
        set nu
    endif
endfunction

nnoremap <C-n> :call NumberToggle()<CR>

The cool thing about this is that you can hit ctrl + n to toggle between relative and absolute number modes!

7

As of Vim 7.3.1115 this has become a little more complicated to do.

The reason is that besides "no line numbers" and "absolute line numbers", there are now two settings for relative line numbers: ordinary "relative line numbers", and "relative line numbers with absolute number on the cursor line".

More technically speaking, all four combinations of 'number' and 'relativenumber' are now possible.

Here's how to toggle:

  • Toggle all four settings, no numbersabsoluterelativerelative with absolute on cursor line:

    :exe 'set nu!' &nu ? 'rnu!' : ''
    
  • Toggle between no numbersabsoluterelative:

    :let [&nu, &rnu] = [&nu+&rnu==0, &nu]
    
  • Toggle between no numbersabsoluterelative with absolute on cursor line:

    :let [&nu, &rnu] = [!&rnu, &nu+&rnu==1]
    
0

Another alternative like Eric Anderson above:

"Relative with start point or with line number or absolute number lines
function! NumberToggle()
    if(&number == 1)
        set number!
        set relativenumber!
      elseif(&relativenumber==1)
        set relativenumber
        set number
      else
        set norelativenumber
        set number                                                  
    endif
endfunction

nnoremap <C-n> :call NumberToggle()<CR>
0
" put this in your .vimrc or source it from your .vimrc


  function! ToggleRelativeNumber()

      if &relativenumber

           set norelativenumber

      else

           set relativenumber

      endif

  endfunction

  nmap ;r :call ToggleRelativeNumber()<CR>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .