Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

with

setl nu!

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

setl rnu!

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

share|improve this question

migrated from stackoverflow.com Sep 25 '11 at 9:54

This question came from our site for professional and enthusiast programmers.

4  
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 '11 at 12:01
    
@sehe, I agree , this is a question for stackoverflow –  juanpablo Sep 25 '11 at 15:52

3 Answers 3

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

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]
    
share|improve this answer

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

share|improve this answer
2  
+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 '11 at 23:38
3  
you could make that nnoremap <silent> <F3>... to reduce visual distraction and interfering with other mappings. –  sehe Sep 24 '11 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 '11 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 '11 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 '11 at 12:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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