1

If I open a long file in vim or neovim, and my cursor is not on the first line, and I make the window shorter by either resizing the terminal, or adding another split with <C-W>s or :sp<cr>, the top few lines get shifted out the top.

It seems like the content is shifted to keep the cursor at a proportionally-the-same position, e.g. "50% of the way down the window" - but I find this kind of jarring, and would like the lines to move less when adding splits.

For example,

nvim <(seq 100)

And move cursor to middle of window with M:

single vim window with lines 1 through 19 visible and cursor on line 10

Then split the window horizontally with <C-W>s or :sp<cr>:

two vim windows, each with lines 6 through 14 visible, with cursor in bottom window on line 10

And notice that lines 1 through 5 get pushed off screen, and the cursor is still roughly 50% down both windows on line 10.

I would prefer if vim attempted to keep the top 5 lines in view, that is:

  • show lines 2 through 10 in each window, with the cursor having stayed on line 10

two vim windows both showing lines 2 through 10 with cursor in bottom window on line 10

I hope this is clear.

In other words, I would like if the line previously at the top of the window (line("w0")) gets stored before the resize and gets restored to as close to the top as possible e.g. with zt

I've looked at :help resize, but that seems to be for controlling the sizes of windows, not cursor or viewport positioning.

I've tried using:

autocmd VimResized * Hzt``

which is close to semantically what I want (previous top line as close as possible to the top of the window on window resize), but this seems to break resizing by the terminal window completely, and has no effect when splitting with <C-W>s or :sp<cr>.

As @dedowsdi points out, this is only triggered on vim application window resize, and there has no WinResized event. So this might not be the right approach.

Another idea I have is if I were able to have vim automatically type H before the resize and `` after the resize, the contents might stay put...

  • Can you provide a gif or screenshot? I'm having a hard time visualizing the behavior you're talking about, and it will be helpful to make sure others recreate the same problem in coming up with solutions. – D. Ben Knoble May 21 at 17:54
  • 1
    @D.BenKnoble I've added an example with (tall) screenshots! – jameh May 21 at 23:18
  • made the screenshots shorter – jameh May 21 at 23:38
  • 1
    @filbranden I have scrolloff=0 already – jameh May 22 at 0:18
  • 1
    VimResized is only used for vim application window, there has no WinResized event. – dedowsdi May 22 at 0:48

Your Answer

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

Browse other questions tagged or ask your own question.