Monday, August 22, 2011

Re: Tweaking $ command

Reply to message «Re: Tweaking $ command»,
sent 22:26:44 22 August 2011, Monday
by AK:

> It does move downward. 3$ moves 2 lines downward, etc.
Now see this in help. For some reason it was not working before while testing
(perhaps forgot to add more lines).

> Ok, I see.. but why does map or function have to do anything with that,
> they already get v:count and v:linenumber1 etc (IIRC), why isn't it up
> to them to choose how to use or not use them?
You choose: by <C-u>. I don't like it either: it cost me much time trying to get
rid of this in <expr> mapping for visual mode.

> > 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.
>
> Either reset or applied, I don't see the logic in having that happen
> with the first normal command in a function.
I don't rememeber it applied anywhere (though I don't like :normal! very much so
I can't say I tested this on all commands). On my setup it is simply ignored and
reset.

> > This won't work for {count}$, see above.
>
> ?? It does work for me in Vim 7.3. For both $ and {count}$ .
vim-7.3.266 (~amd64) USE="X acl bash-completion cscope gpm nls perl python ruby
vim-pager -debug -minimal", Gentoo

With the following script

vim -u NONE -N \
--cmd 'exe "fu_()\nnorm!$\nif v:count|exe\"norm\".v:count.\"h\"|en\nendf"' \
--cmd 'nnoremap $ :<C-u>call _()<CR>' \
--cmd 'call setline(".", range(100000, 100500))'

3$ acts just like $. Maybe you should update. Or maybe there is something in
'cpoptions', so try the above.

Original message:
> On 08/22/2011 01:58 PM, ZyX wrote:
> > 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).
>
> It does move downward. 3$ moves 2 lines downward, etc.
>
> >>> 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'):(''))
>
> Thanks, that works and good to know. I think I still prefer function
> version for readability.
>
> >> 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.
>
> Ok, I see.. but why does map or function have to do anything with that,
> they already get v:count and v:linenumber1 etc (IIRC), why isn't it up
> to them to choose how to use or not use them?
>
> >>> 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.
>
> Either reset or applied, I don't see the logic in having that happen
> with the first normal command in a function.
>
> >> 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.
>
> ?? It does work for me in Vim 7.3. For both $ and {count}$ .
>
>
> -ak

No comments: