How to prevent Vim scrolling when splitting a window?

vim

Solution

From Vim 9.0.0667 and Neovim 0.9 you can try

`set splitkeep=screen`

Read more at `h splitkeep`

Problem

I want to stop Vim from scrolling (if that's the right word) when I split a window horizontally. Let's say I edit a 10 line file in Vim. I have a single window onto the buffer and the window is 40 lines high. There's more than enough room for two windows, one on top of the other, with both showing the whole buffer. And let's say `scrolloff` is set to 4. When I split the window horizontally, the original window is scrolled so that exactly `scrolloff` lines are shown between the top of the window and the line the cursor is on -- if there were more than `scrolloff` lines between the top of the window and the cursor line -- even though there is no need to scroll. Put another way, if the cursor is on line 1, 2, 3, 4 or 5 when I split the window, the original window doesn't "move" (good). But if the cursor is on line 6, the window scrolls so that line 2 becomes the top-most visible line...ensuring `scrolloff` lines (4) are visible above the cursor line (annoying). Similarly if the cursor is on line 7 when I split the window, the original window scrolls to that line 3 becomes the top-most visible line. And so on. Is there a way to configure Vim never to scroll the original window when I split it horizontally? I imagine it's possible to map `<C-W>s` to a function which does what I want, but I'd prefer to solve this by configuration if possible.

Original source

Related problems