Sunday, November 9, 2014

Re: Changing line numbers from normal to relative

On Thu, Nov 06, 2014, Ben Fritz wrote:
> On Thursday, November 6, 2014 2:53:02 PM UTC-6, Salman Halim wrote:
> > Hi,
> >
> > I was wondering if it was possible to switch to relative line numbers once I'm in operator-pending mode and then switch back when done. For example, say I have regular line numbers turned on and I hit 'c' in normal mode on line 24. The numbering changes to relative and goes back to regular numbers after the rest of the command is executed.
> >
> > I couldn't find an autocommand, but was hoping someone else had already come up with an ingenious way to address this.
> >
>
> I did not find any autocmd for it, but I use an expression mapping returning an empty string in normal, visual, and operator-pending modes to accomplish the task:
>
> if exists('+relativenumber')
> nnoremap <expr> <C-Space> CycleLNum()
> xnoremap <expr> <C-Space> CycleLNum()
> onoremap <expr> <C-Space> CycleLNum()
>
> " function to cycle between normal, relative, and no line numbering
> func! CycleLNum()
> if &l:rnu && !&l:nu
> setlocal nu
> elseif &l:rnu && &l:nu
> setlocal nornu
> elseif &l:nu && !&l:rnu
> setlocal nonu
> else
> setlocal rnu
> endif
> " sometimes (like in op-pending mode) the redraw doesn't happen
> " automatically
> redraw
> " do nothing, even in op-pending mode
> return ""
> endfunc
> endif
>
> Thus when I hit <C-Space> in any of these modes, nothing happens, which allows me to continue with my operation, but the line numbering changes as a side effect.

Great idea! But I find it only works in the terminal when I call the map
<C-@> (even though my terminal treats C-Space as C-@).

If I use <C-Space> as the map name, when I try the <C-Space> combination
in one of those modes, I just get a bell. I tried running Vim with
-V20vim.out to log what's going on, and it didn't show that CycleLNum()
was ever called.

Two other minor quibbles here:
- it doesn't reset the old numbering style after a motion or visual
operation has been done.
- the visual representation of the cursor moves to the end of the
screen, although the actual logical cursor position in Vim isn't
changed. Surprisingly, this affects GUI vim too.

I'm using MacVim 7.4 (73), which has Vim 7.4.258 in it.

--
Eric Christopherson

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments: