Monday, August 22, 2011

Re: Tweaking $ command

Reply to message «Re: Tweaking $ command»,
sent 22:17:27 21 August 2011, Sunday
by AK:

> The 'k' was fixing downward movement of $ command. After replacing with
> normal! $, it works without that fix.
`$' does not move downward on its own. I don't see why it should move downward
(unless you kill that condition: in this case it will move because 0-1==-1 and
`-' moves, but upward).

> > 2. :h map-<expr>:
> > nnoremap $ "<Bar>$".((v:count>1)?(((v:count-1).'h'):(''))
>
> That doesn't work for me, using vim 7.3 (even after adding the ')').
> However, I prefer the function version because it's more readable.
Oops, I forgot to pull fixes to the message after testing:
nnoremap <expr> $ "<Bar>$".((v:count>1)?((v:count-1).'h'):(''))

> I see, but <c-u> seems like a pretty bad kludge. I remember using it at
> least a couple of times before, and I still did not remember it because
> it's pretty counter-intuitive. I think it would be better if there was
> an arg to *map to turn it off, like <no-prepend-range>, because users
> would look into *map arguments help for explanation of this behaviour.
It is not related to :*map at all, it is property of `:'. By the way, you saw
`<Bar>' above? It is for the same job here: drop count. Another possibility is
<C-\><C-n>: in this case {count} is ignored.

> > Because v:count is reset each time normal-mode command is executed. Guess
> > why in
> >
> > > exe "normal \<End>"
> >
> > command is called *normal*?
>
> That doesn't seem reasonable, why should Vim assume the count applies to
> the first (of possibly dozens) normal command in a function?
I don't said it is *applied*. I said it is *reset* when normal-mode command is
executed. `normal' does execute the normal-mode command as clearly stated by its
name.

> Anyway, thanks a ton for your help and here's what I ended up with if
> anyone wants to use this too:
>
> func! EndOfLine()
> " Go to Nth char before end of line
> normal! $
> if v:count | exe "normal ".v:count."h" | endif
> endfu
> nnoremap $ :<c-u>call EndOfLine()<cr>
This won't work for {count}$, see above. Also, don't use :normal without a bang,
*map without `nore' and feedkeys without `, "n"' unless you can say why are you
doing so. Defaults are for writing quick one-liners, *noremap, :normal! and
feedkeys(..., "n") are for scripts that should be immune to presense of user
mappings.

Original message:
> On 08/20/2011 07:57 PM, ZyX wrote:
> > Reply to message «Tweaking $ command»,
> > sent 03:05:13 21 August 2011, Sunday
> >
> > by AK:
> >> Is there a better way to do this?
> >
> > 0. You said you want just
> >
> > > to change $ command to go to N chars before the end of line
> >
> > but what in this case `k' is doing in the second :normal command?
>
> The 'k' was fixing downward movement of $ command. After replacing with
> normal! $, it works without that fix.
>
> > 1. You must not use :normal here (you could replace
> >
> > exe "normal \<End>"
> >
> > with
> >
> > normal! $
> >
> > ): use :normal! (with bang).
> >
> > 2. :h map-<expr>:
> > nnoremap $ "<Bar>$".((v:count>1)?(((v:count-1).'h'):(''))
>
> That doesn't work for me, using vim 7.3 (even after adding the ')').
> However, I prefer the function version because it's more readable.
>
> >> Why is<c-u> needed there?
> >
> > Try typing `2:', then read a paragraph before :h function-range-example
> > (second paragraph in :h :call).
>
> I see, but <c-u> seems like a pretty bad kludge. I remember using it at
> least a couple of times before, and I still did not remember it because
> it's pretty counter-intuitive. I think it would be better if there was
> an arg to *map to turn it off, like <no-prepend-range>, because users
> would look into *map arguments help for explanation of this behaviour.
>
> >> If I
> >> remapped $, why does it keep trying to do built-in $ handling of count?
> >
> > Because v:count is reset each time normal-mode command is executed. Guess
> > why in
> >
> > > exe "normal \<End>"
> >
> > command is called *normal*?
>
> That doesn't seem reasonable, why should Vim assume the count applies to
> the first (of possibly dozens) normal command in a function?
>
> Is this sort of questions ok to bring up on vim-use list?
>
> Anyway, thanks a ton for your help and here's what I ended up with if
> anyone wants to use this too:
>
> func! EndOfLine()
> " Go to Nth char before end of line
> normal! $
> if v:count | exe "normal ".v:count."h" | endif
> endfu
> nnoremap $ :<c-u>call EndOfLine()<cr>
>
>
> -ak

No comments: