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.
A recently available option is the stable-windows plugin. I've tried it and it's working well.
I've found a solution, borrowing the answer from here.
In my .vimrc
I have:
nnoremap <C-W>s Hmx`` \|:split<CR>`xzt``
And now when I split the window horizontally with <C-W>s
, the original window doesn't scroll at all.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With