sponsor Vim development Vim logo Vim Book Ad

basic Tip #105: combining move and scroll

 tip karma   Rating 6/3, Viewed by 1208 

created:   September 3, 2001 17:22      complexity:   basic
author:   Andrew Pimlott      as of Vim:   5.7

I sometimes found myself moving down a few lines with j, then scrolling down
about the same number of lines with <C-E> to put the cursor in roughly the
same place as it started.  I decided I wanted to map <C-J> (and <C-K>,
respectively) to the move-and-scroll operation.  First, I did

    :map <C-J> <C-E>j

This was pretty good, but behaved funny at the beginning and end of files.
Then, I realized that <C-D> already combined move and scroll, so I figured
that giving <C-D> a count of 1 would do it:

    :map <C-J> 1<C-D>

Unfortunately, this permanently attaches a count to <C-D> (ugh!), so I have
to undo that:

    :map <C-J> 1<C-D>:set scroll=0<CR>

This has the drawback of not necessarily resetting scroll to its original
value, but since I never change scroll, it's good enough for me.  It would
be nice if there were a version of <C-D> that did not have the side-affect
of changing scroll.

Happy vimming,
Andrew

 rate this tip  Life Changing Helpful Unfulfilling 

<<using vim to complement Perl's DBI::Shell | Mail signature rotation: Supersimple one-line solution >>

Additional Notes

[email protected], February 2, 2003 12:45
And for Page-Up/Page-Down, see vimtip #320. My solution:

map <PageDown> :set scroll=0<CR>:set scroll^=2<CR>:set scroll-=1<CR><C-D>:set scroll=0<CR>
map <PageUp> :set scroll=0<CR>:set scroll^=2<CR>:set scroll-=1<CR><C-U>:set scroll=0<CR>
[email protected], June 20, 2004 10:42
If you set the scroll to 1 for C-D and C-U, you can still use C-F and C-B to go forward and back one screen per press.  Best of both worlds!!
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to [email protected] after searching the archive. Help Bram help Uganda.
Sponsored by Web Concept Group Inc. SourceForge Logo