r/vim • u/myusuf3 • Jun 27 '12
numbers.vim -- better line numbers for vim
http://myusuf3.github.com/numbers.vim/1
Jun 27 '12 edited Jun 28 '12
Interesting. Would you mind explaining why you chose to associate insert mode with absolute numbers and normal mode with relative numbers?
So far I've been quite happy with this little snippet in my vimrc:
" toggle relative number
if exists('&relativenumber')
function! s:ToggleRelativeNumber()
if &relativenumber
set norelativenumber
let &number = exists("b:togglernu_number") ? b:togglernu_number : 1
else
let b:togglernu_number = &number
set relativenumber
endif
endfunction
noremap <silent> <Leader>m :<C-U>call <SID>ToggleRelativeNumber()<CR>
endifEdit: Added protection for uninitialized b:togglernu_number variable.
1
1
u/breue Jun 27 '12
I use a very similar mapping to this, though mine is mapped to <leader>#
1
u/gfixler Jun 27 '12
This can be much simpler than all of the above:
" relative/absolute number toggling map <Leader>na :se <c-r>=&nu?"no":""<CR>nu<CR> map <Leader>nr :se <c-r>=&rnu?"no":""<CR>rnu<CR>1
Jun 28 '12
Ohoo, "can be much simpler", you say, let me take that as a challenge to a little game of vimgolf.
noremap <Leader>na :se nu!<CR> noremap <Leader>nr :se rnu!<CR>Take that, gfixler! I win. Point is, Vim lets you add a bang to all boolean options for toggling. But your use of the expression register is very neat. I would never have thought of using it to build the option name.
As for my function: I assume 'relativenumber' is an option that you switch on, then do some operation, then switch it off again. If you do the toggling through my ToggleRelativeNumber() it remembers whether you had 'number' on or off and then switches back to the remembered setting when you turn relative numbers back off again.
So if you have 'number' enabled you can quickly change to relative numbers and then go back to normal line numbering, and if you prefer to work without line numbers you can quickly switch relative numbers on, and completely off again.
The variable b:togglernu_number makes sure the 'number' setting is remembered for every buffer individually. Cheers!
2
u/gfixler Jun 28 '12
Sir, you have my golf club.
Now try to reduce this trinary toggle I wrote last night :)
1
u/hiffy Jun 27 '12
I use pathogen, used the git clone command to dump it in my bundle folder, restarted vim --
E492: Not an editor command: NumbersToggle
I use MacVim, on a vim 7.3 - any ideas?
1
u/myusuf3 Jun 27 '12
do you have patch patch338 applied?
1
u/hiffy Jun 27 '12
My MacVim stopped autoupdating since I'm still on Snow Leopard. I found a more recently release and damn son this is v. cute. Gonna keep on playing with it - maybe I'll actually use multiline commands for once.
1
6
u/macbony Jun 28 '12
Any reason to use this over:
silent! autocmd InsertEnter * :set number
silent! autocmd InsertLeave * :set relativenumber?
2
u/acedio Jun 27 '12
I think this might be just what I'm looking for. I'll try it out at work today :)
2
2
u/h0st1le Jun 27 '12
Looks a lot like this one, which I have been using for a while now:
https://github.com/jeffkreeftmeijer/vim-numbertoggle
he goes over the logic of his choices here:
http://jeffkreeftmeijer.com/2012/relative-line-numbers-in-vim-for-super-fast-movement/
Also includes the mappable number toggle function.
1
u/gfixler Jun 27 '12
My version:
" relative/absolute number toggling map <Leader>na :se <c-r>=&nu?"no":""<CR>nu<CR> map <Leader>nr :se <c-r>=&rnu?"no":""<CR>rnu<CR>Each will toggle itself, or replace the other.
1
u/gamzer Jun 27 '12
With relativenumber I don't see the cursor while I hold down "j" or "k". Only the line numbers will display quickly enough.
1
u/ocd_see Jun 30 '12
What is the bottom-most bar? Is it a feature of macvim? Is it possible to get something similar in gVim?
2
u/[deleted] Jun 27 '12
What is the rationale behind the insert = absolute / normal = relative separation?