How to prevent Vim scrolling when splitting a window?
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.
As an alternative to Andy Stewart's solution here's what I use in my .vimrc
:
(3) (5)
++ +--+
|| | |
nnoremap _ Hmx``<C-w>szz<C-w><C-p>`x``<C-w><C-p>
| || | | | | |
+-+-++----+ +--------+ +--------+
(1) (2) (4) (6)
(1) Go top left save mark it to x
and go back to where you were.
(2) Create a new split. Cursor will move to this new split window.
(3) Use zz
to center the cursorline.
(4) Go back to the window you have created the split.
(5) Go back to x
mark and then go back to where you were (to simulate step (1))
(6) Go back to the split you created.
One addition I made is to add zz
onto the newly opened split to make the cursor on the new window easier to catch. My brain can automatically focus onto the center row of the new split.