15
5
with
setl nu!
I can toggle number (on/off), similar with relativenumber
setl rnu!
how I can toggle {off,number,relativenumber} ?
15
5
with
setl nu!
I can toggle number (on/off), similar with relativenumber
setl rnu!
how I can toggle {off,number,relativenumber} ?
9
if &nu == 1
set rnu
elseif &rnu == 1
set nornu
else
set nu
endif
1@JuanPablo: mmm. I like my version better :) – sehe – 13 years ago
1@sehe: I'd say the same about my version. :-) – None – 13 years ago
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 :)
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 – 13 years ago
3you could make that nnoremap <silent> <F3>
... to reduce visual distraction and interfering with other mappings. – sehe – 13 years ago
I came up with almost the same (but shorter :-) line when read the question: exe'se'&nu+&rnu?'rnu!':'nu'
. – None – 13 years ago
By the way, to assure yourself of the connection between number
and relativenumber
options, see :helpg When setting this option
. – None – 13 years ago
@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 – 13 years ago
@ sehe: who is ib
user? why don't have perfil ? – juanpablo – 13 years ago
@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 – 13 years ago
I like this version, but I find it obfuscated. – Benoit – 13 years ago
@Benoit , yes this version is more elegant, but maybe is confused for any vim user. – juanpablo – 13 years ago
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 – 13 years ago
@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 – 13 years ago
@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 – 13 years ago
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 – 13 years ago
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 numbers → absolute → relative → relative with absolute on cursor line:
:exe 'set nu!' &nu ? 'rnu!' : ''
Toggle between no numbers → absolute → relative:
:let [&nu, &rnu] = [&nu+&rnu==0, &nu]
Toggle between no numbers → absolute → relative with absolute on cursor line:
:let [&nu, &rnu] = [!&rnu, &nu+&rnu==1]
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!
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>
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 – 13 years ago@sehe, I agree , this is a question for stackoverflow – juanpablo – 13 years ago