Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scrolling long wrapped lines in Vim

Tags:

vim

Problem: When writing prose scrolling works in unexpected way when using j to scroll down. As I scroll between two long paragraphs (wrapped lines in vim) of text using j, when reaching the next long paragraph (line) the text "jumps" from the bottom of the screen to the top, aligning the first word of the paragraph (line) with the top of the screen (see screenshots bellow).

Affected: Folks who love Vim and want to use it to write prose.

Expected: I would like the paragraph (line) to jump to the middle of the screen OR to continue scrolling with the cursor at the bottom. The jump is too jarring otherwise--I am losing context. Two questions: 1) Is it possible to change the default behavior in .vimrc? 2) If not, how feasible would it be to write a plug-in altering the behavior?

Related Resources:

  • http://vim.1045645.n5.nabble.com/Long-lines-and-scrolling-td1183898.html
  • http://vim.wikia.com/wiki/Move_through_wrapped_lines
  • http://vim.1045645.n5.nabble.com/Scrolling-Long-Lines-Revisited-Again-td5031203.html

For example: here I am scrolling down some dummy text: frame1.

After jj the next paragraph has moved up to the top of the screen: frame2

My .vimrc is here. Prose mode is:

command! Prose setlocal linebreak nolist wrap wrapmargin=0
nnoremap k gk
nnoremap j gj
like image 229
denten Avatar asked Mar 18 '23 11:03

denten


2 Answers

It will help to brief yourself on VIM's motion commands { and } are good ones with literary text. However in the case you describe these are not paragraphs they are lines. VIM manages them as lines. As such you had to go so far as to remap j and k which is the reverse of usual. My suggestion is to break the lines into paragraphs by way of textwidth.

Finally VIM has screen commands to manipulate the viewable area. Since VIM is attempting to show you as much context as possible (in your case a extremely long line) it takes up the whole screen and you see the "jump" To reverse this use the zz (AKA z<Enter>) command to move the screen viewable area to match the cursor in the center. The others are zt to place the viewable area to the top and zb to place the viable area to the bottom.

Hope these suggestions help and happy VIMing.

like image 129
Sukima Avatar answered Apr 09 '23 21:04

Sukima


As mentioned by @denten they are not paragraphs they are long lines.

When you navigate using j or k; you are moving vertically through the same column. But with your mapping (j to gj and k to gk) you are moving vertically as visually wrapped by the editor.

I suggest to remove that mapping and use gj & gk manually as required.

Additionally if scrolling is your requirement, consider mapping PageUp & PageDown with the following:

" Slow PageUp/PageDown
nnoremap <silent> <PageUp> <c-y>
nnoremap <silent> <PageDown> <c-e>

Or if the idea of using PageUp or PageDown feel awkward, feel free to go with the actual keys to scroll: Control and y for up; and Control and e for down.

like image 42
Jikku Jose Avatar answered Apr 09 '23 20:04

Jikku Jose