Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent Vim scrolling when splitting a window?

Tags:

vim

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.

like image 543
Andy Stewart Avatar asked Oct 15 '12 14:10

Andy Stewart


2 Answers

A recently available option is the stable-windows plugin. I've tried it and it's working well.

like image 103
Puhlze Avatar answered Oct 07 '22 17:10

Puhlze


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.

like image 36
Andy Stewart Avatar answered Oct 07 '22 16:10

Andy Stewart