vim : toggle number with relativenumber
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 3
--
Chapters
00:00 Vim : Toggle Number With Relativenumber
00:19 Answer 1 Score 19
00:41 Accepted Answer Score 11
00:48 Answer 3 Score 8
01:03 Answer 4 Score 7
01:53 Answer 5 Score 0
01:58 Thank you
--
Full question
https://superuser.com/questions/339593/v...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#vim #vimrc
#avk47
ANSWER 1
Score 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 :)
ACCEPTED ANSWER
Score 11
if &nu == 1
set rnu
elseif &rnu == 1
set nornu
else
set nu
endif
ANSWER 3
Score 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!
ANSWER 4
Score 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 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]