Reply to message «Re: go to next line multiples of n», sent 19:32:26 10 June 2011, Friday by Bee:
> The only improvement I would like is to do each by a count, if count
> is not specified then count=1.
>
> {count}J
> {count}K
>
> To go to the nth block of tlines.
You have to work around the fact that with
nnoremap <expr> J smth
nnoremap K smth
when you execute «10J» or «10K» it acts like if the following mappings were
active:
nnoremap <expr> 10J 10.(smth)
nnoremap 10K 10smth
and use v:count1 variable:
nnoremap <expr> J ":\<C-u>\n".(tline*v:count1+1-line('.')%tline).'jzt'
or
nnoremap K :<C-u>exe 'normal!' (tline*v:count1+1-line('.')%tline).'kzt'<CR>
. Here the workaround is `<C-u>' used to clear the command-line. In first
variant ":\<C-u>\n" is a no-op command that just discards count. It works as
expression is calculated before count is discarded.
In second `:<C-u>' starts ex mode and clears count that was put onto cmdline
(try typing `10:', it will show `:.,.+9'). Count is still accessible via
v:count1 as «last normal-mode command» is still «:».
Original message:
> On Jun 9, 11:43 pm, ZyX <[email protected]> wrote:
> > Reply to message «Re: go to next line multiples of n»,
> > sent 09:29:14 10 June 2011, Friday
> >
> > by Bee:
> > > > > This does NOT work.
> > > > >
> > > > > :nmap J execute "normal " . tline - line('.') % tline . "j"
> >
> > In the original normal command you had problems with operator priorities:
> > if you'd run
> >
> > echo "normal " . tline - line('.') % tline . "j"
> >
> > you found what is the problem (it outputs `-1j' for the first line). In
> > this
> >
> > command you should have added parenthesis:
> > execute "normal! " . (tline - line('.') % tline) . "j"
> >
> > . I don't see any problems with> :nmap <expr> J
> > tline+1-line('.')%tline.'jzt'
> >
> > > :nmap <expr> K tline+1-line('.')%tline.'kzt'
> >
> > except that you should not use `nmap' without `nore' here: `nnoremap'
> > saves your time as number of mappings that can interfere with each other
> > grows up. Maybe this is the problem. If it is not, write what happens
> > when you use this mapping and what you expect to happen: I don't get
> > what is wrong here.
> >
> > Also, write parenthesis. It works now because concatenation operator has
> > the same priority as addition and substraction and number calculations
> > occur to be first, but it will fail if you add some string before
> > `tline' as you did in original mapping.
> >
> > Original message:
> > > On Jun 9, 10:09 pm, Christian Brabandt <[email protected]> wrote:
> > > > Hi Bee!
> > > >
> > > > On Do, 09 Jun 2011, Bee wrote:
> > > > > I would like to jump to the next (or prev) line, always a multiple
> > > > > of "tline".
> > > > >
> > > > > let tline = 16
> > > > >
> > > > > This calculates how many more lines to jump
> > > > > useful with "j"
> > > > >
> > > > > :echo tline - line(".") % tline
> > > > >
> > > > > This calculates the line to jump to
> > > > > useful with "G"
> > > > >
> > > > > :echo ( line(".") / tline + 1 ) * tline
> > > > >
> > > > > How to create a mapping?
> > > > >
> > > > > This does NOT work.
> > > > >
> > > > > :nmap J execute "normal " . tline - line('.') % tline . "j"
> > > >
> > > > You want an expression mapping:
> > > > :nmap <expr> J tline-line('.')%tline.'j'
> > > >
> > > > See also the help at :h map-expression
> > > >
> > > > regards,
> > > > Christian
> > > > --
> > >
> > > Thank you, Christian
> > >
> > > Works great but for the offset I needed to add 1:
> > > :nmap <expr> J tline+1-line('.')%tline.'jzt'
> > > :nmap <expr> K tline+1-line('.')%tline.'kzt'
> > >
> > > And now I also have the reverse.
> >
> > signature.asc
> >
> > < 1KViewDownload
>
> @ZyX
> There is NO problem with those.
> They work as I expect
> A minor problem moving back from end of file when not an even multiple
> of tline.
>
> The only improvement I would like is to do each by a count, if count
> is not specified then count=1.
>
> {count}J
> {count}K
>
> To go to the nth block of tlines.
signature.asc
Description: This is a digitally signed message part.
