7

This has been asked on stackoverflow before, but it seems it still has no solution. Perhaps that has changed with Vim 8.0.

I'd like to be able to scroll (i.e. scroll-wheel, or <C-y>/<C-e> in normal mode) by visual lines when line-wrapping is enabled, rather than by line number. This can come in handy for text documents (in my case, LaTeX files).

Any chance Vim 8.0 allows this somehow? Or can anyone suggest a vimscript function hack that might accomplish the same thing?

  • not possible as far as i know – Christian Brabandt Feb 8 '17 at 20:49
  • @ChristianBrabandt was wondering though if there was some way to cook up a vimscript function that remaps <C-y> and <C-e> (in this case, we'd want the map to be recursive/global, so would use map). Maybe there's some way to determine "how many wrapped lines does this line have?" Seems like the fact that Vim gives the option to display partial lines going on/offscreen implies there must be some convoluted way to do it. – Luke Davis Feb 10 '17 at 1:54
  • 1
    Not really, because Vim does not allow to only display partial rows. That's why smooth scrolling does not really work well in Vim. And there is nothing, VimL functions can do against it. – Christian Brabandt Feb 10 '17 at 6:20
  • @ChristianBrabandt well actually it sort of can with set display=lastline, but I see what you mean; windows must always start on the first wrapped line of a line. Seems to me that aspect is what makes this impossible. – Luke Davis Feb 13 '17 at 1:20
  • @ChristianBrabandt FWIW I came up with a hack that does (for example) as close to half-page scrolling as possible across wrapped lines, and prevents the cursor from jumping around. I now absolutely depend on it for editing LaTeX documents. Check out the answer below. – Luke Davis May 7 '17 at 20:46
5

I've released a vim plugin that pretty closely matches this behavior.

Instead of scrolling by exactly N lines, this maps <C-u>, <C-d> (half window) and <C-j>, <C-k> (quarter window) to scroll as close to N lines as possible due to the wrapped lines, then moves the cursor back to the original relative line position in the Vim window. To match the vim behavior, if the top or bottom of the window are on line 1 or the final line, scrolling does not preserve the original cursor location and the cursor starts moving up the window until reaching the boundary.

This plugin also introduces a :WrapToggle command (mapped to <Leader>w) that toggles wrapping on and off, and swaps the various j, gj, k, gk maps so that the basic motion commands are always with respect to visual lines, and the g prefixed maps are with respect to actual lines when line wrapping is enabled.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.