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
: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.
nvim <(seq 100)
And move cursor to middle of window with
Then split the window horizontally with
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
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
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
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...