vim : toggle number with relativenumber

15

5

with

setl nu!

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

setl rnu!

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

juanpablo

Posted 2011-09-24T19:46:23.810

Reputation: 5 216

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: http://meta.stackexchange.com/questions/25925/vim-questions-so-or-su

– sehe – 2011-09-25T12:01:33.753

@sehe, I agree , this is a question for stackoverflow – juanpablo – 2011-09-25T15:52:22.793

Answers

9

if &nu == 1
   set rnu
elseif &rnu == 1
   set nornu
else
   set nu
endif

Benoit

Posted 2011-09-24T19:46:23.810

Reputation: 6 563

thanks Benoit, I added your solution as a gist

– juanpablo – 2011-09-24T20:17:35.680

1@JuanPablo: mmm. I like my version better :) – sehe – 2011-09-24T20:19:41.600

1@sehe: I'd say the same about my version. :-) – None – 2011-09-25T03:51:27.093

17

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

sehe

Posted 2011-09-24T19:46:23.810

Reputation: 1 796

3+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. – None – 2011-09-24T23:38:58.403

3you could make that nnoremap <silent> <F3>... to reduce visual distraction and interfering with other mappings. – sehe – 2011-09-24T23:56:45.753

I came up with almost the same (but shorter :-) line when read the question: exe'se'&nu+&rnu?'rnu!':'nu'. – None – 2011-09-25T02:51:36.170

By the way, to assure yourself of the connection between number and relativenumber options, see :helpg When setting this option. – None – 2011-09-25T02:54:15.580

@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 – 2011-09-25T12:08:38.970

@ sehe: who is ib user? why don't have perfil ? – juanpablo – 2011-09-25T16:14:15.317

@juanpablo: as you must be aware, some parallel universe mishap defied logic and astrally projected his holy highness IB into a galaxy in which he actually doesn't exist. In other words: migrated – sehe – 2011-09-25T16:19:26.063

I like this version, but I find it obfuscated. – Benoit – 2011-09-25T17:01:02.150

@Benoit , yes this version is more elegant, but maybe is confused for any vim user. – juanpablo – 2011-09-25T17:16:30.193

If the reason for this answer is to have a one-liner then you can do: :if &nu == 1 | set rnu | elseif &rnu == 1 | set nornu | else | set nu | endif ; then @: just works (I also use it a lot) – Benoit – 2011-09-25T17:18:40.267

@Benoit: yeah I sort of got that. In that case, however, the if-else is no longer any more legible and I'd argue that my version is then more concise: the binary switch on 'se rnu!' or 'se nu!': it is instantly clear that one of the two is happening. Also, note (a) that I already argued that your version is fine since it is just going to be a mapping, and (b) see the first sentence of my answer :) You could be preaching to the choir. – sehe – 2011-09-25T18:15:36.540

@sehe: I had not seen your previous comment. I agree that the if-else is not really legible on a single line (but IMO still more understandable than a golfed one!) – Benoit – 2011-09-25T20:34:02.037

You think I golfed it? I just wrote what I wanted it to do :) Sorry that it comes out like that... it happens to me all the time (fyi: the brainwave that makes that happen is that I usually think: (bool)a!=(bool)b for XOR pretty quickly. Blame languages without XOR operator) – sehe – 2011-09-26T10:11:05.993

8

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]
    

glts

Posted 2011-09-24T19:46:23.810

Reputation: 216

7

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!

Eric Anderson

Posted 2011-09-24T19:46:23.810

Reputation: 170

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>

clasan

Posted 2011-09-24T19:46:23.810

Reputation: 11

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>

heronsrise

Posted 2011-09-24T19:46:23.810

Reputation: 1