[vim/vim] Switching buffers cause window to scroll (#7954)

22 views
Skip to first unread message

DieSpinne

unread,
Mar 12, 2021, 7:00:32 AM3/12/21
to vim/vim, Subscribed

Duplicate of the auto-closed #2477.

When I switch from buffer A to buffer B and then back again, the cursor line in A is scrolled to the center of the window. This is confusing. I would like the window to display the buffer exactly as it was displayed before. I think this is the expected behavior.


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or unsubscribe.

Tony Mechelynck

unread,
Mar 12, 2021, 8:58:57 AM3/12/21
to vim/vim, Subscribed

When I follow a hotlink in the help to a different helpfile, and then use Ctrl-O to come back, the cursor line (if not near the top or bottom of the file) is now centered. AFAICR it's always been like that. But if you set 'scrolloff' to a large value, Vim will always keep the cursor centered (scrolling the file as needed) and going to another buffer and back will bring you just as centered as you already were.

Best regards,
Tony.

DieSpinne

unread,
Mar 12, 2021, 2:01:43 PM3/12/21
to vim/vim, Subscribed

Hi Tonymec.
Once one jumps to another position in the same buffer, he knows the buffer will scroll, that's quite inevitable, so there is not much expectation to have the very same position when coming back (though it would be admitedly cool). But when switching buffers, it is actually surprising behavior. Note how other editors and web browsers won't scroll if you change tabs.

Gary Johnson

unread,
Mar 12, 2021, 2:27:46 PM3/12/21
to reply+ACY5DGEH3SPL3LSFTD...@reply.github.com, vim...@googlegroups.com
On 2021-03-12, DieSpinne wrote:
> Hi Tonymec.
> Once one jumps to another position in the same buffer, he knows the buffer will
> scroll, that's quite inevitable, so there is not much expectation to have the
> very same position when coming back (though it would be admitedly cool). But
> when switching buffers, it is actually surprising behavior. Note how other
> editors and web browsers won't scroll if you change tabs.

Vim retains the cursor position and the view of a window when
changing windows in a tab or when changing tabs. You're not
changing windows or tabs, you're changing buffers in one window.
The view into a buffer is not a property of the buffer, it is
a property of the window. You can view a buffer in more than one
window and each window can have its own view, including its own
position in the buffer and its own cursor location.

See

:help windows-intro

for a little on this.

Regards,
Gary

vim-dev ML

unread,
Mar 12, 2021, 2:28:10 PM3/12/21
to vim/vim, vim-dev ML, Your activity

DieSpinne

unread,
Mar 12, 2021, 4:39:36 PM3/12/21
to vim/vim, vim-dev ML, Comment

Thanks Gary, I follow you there. I have recently switched from the tab-centered to the buffer-centered approach to Vim and went through those clarifications, sorry if I still slip on some details.

If one understands Vim fundamentals, it is indeed a bit naive to assume the behavior I'm requesting when switching buffers. But I don't think there is much dispute on that leaving the viewport in place (or, rather, restoring it to the original offset) is the expected thing to happen when returning to a buffer, as one has to reorient himself otherwise.

In my opinion, this is another potential reason why novice users may fall to the one file = one tab trap. They observe the behavior they expect on tabs, so they stick to them. I think this proposal would be a nice addition, but I don't want to be pressing here, I'm just trying to make my point clear.


You are receiving this because you commented.

Gary Johnson

unread,
Mar 12, 2021, 6:03:18 PM3/12/21
to reply+ACY5DGCE56CP4UT24E...@reply.github.com, vim...@googlegroups.com
On 2021-03-12, DieSpinne wrote:
> Thanks Gary, I follow you there. I have recently switched from the tab-centered
> to the buffer-centered approach to Vim and went through those clarifications,
> sorry if I still slip on some details.
>
> If one understands Vim fundamentals, it is indeed a bit naive to assume the
> behavior I'm requesting when switching buffers. But I don't think there is much
> dispute on that leaving the viewport in place (or, rather, restoring it to the
> original offset) is the expected thing to happen when returning to a buffer, as
> one has to reorient himself otherwise.
>
> In my opinion, this is another potential reason why novice users may fall to
> the one file = one tab trap. They observe the behavior they expect on tabs, so
> they stick to them. I think this proposal would be a nice addition, but I don't
> want to be pressing here, I'm just trying to make my point clear.

It's interesting how different people use Vim so differently.
I hardly ever use any of the buffer commands and am only starting to
use tabs more regularly. I usually keep all the buffers I'm using
open in different windows, in different horizontal and vertical
splits. I've also used the Rolodex technique. I usually have Vim
occupying a full monitor, so I have plenty of space for windows.

I also use quickfix lists to jump among files in a single window,
but I don't edit more than one of those files at a time, so the
repositioning of the view into the file as I jump in the quickfix
list has never bothered me.

As I understand your issue, you'd like buffers to remember how they
were last viewed. The problem, of course, is that each buffer can
be viewed in multiple windows so the way they were last viewed
depends on the window in which they're being re-opened.

So perhaps you could create a window-local variable as a list of
dictionaries, one dictionary for each buffer opened in that window,
with each dictionary containing the top line and the cursor line of
the buffer when last viewed in that window. A set of autocommands
could manage that list and restore a buffer's view when re-opened in
that window.

Alternatively, you could create a couple of buffer-local variables
containing the top line and cursor line of the last window to view
that buffer. Then whenever a buffer is re-opened in a window, it is
displayed as it was last viewed. If the user is using only one or
a few windows, and never views a buffer in more than one window at
a time, this should work fine. Again, this would all be managed
with a few autocommands.

Assuming that all works and meets your expectations, the problem
becomes how to make novice users aware of the plugin.

Just some thoughts.

Regards,
Gary

vim-dev ML

unread,
Mar 12, 2021, 6:03:41 PM3/12/21
to vim/vim, vim-dev ML, Your activity

marargar

unread,
Apr 19, 2021, 7:16:42 AM4/19/21
to vim/vim, vim-dev ML, Comment

Duplicate of the auto-closed #2477.

When I switch from buffer A to buffer B and then back again, the cursor line in A is scrolled to the center of the window. This is confusing. I would like the window to display the buffer exactly as it was displayed before. I think this is the expected behavior.

This view reposition is annoying to me too. In the meantime you may try https://vim.fandom.com/wiki/Avoid_scrolling_when_switch_buffers


You are receiving this because you commented.

Jake Herrmann

unread,
Dec 13, 2025, 3:28:12 PM12/13/25
to vim/vim, vim-dev ML, Comment
jtherrmann left a comment (vim/vim#7954)

It would be great to get a builtin option added to fix this behavior. The https://vim.fandom.com/wiki/Avoid_scrolling_when_switch_buffers workaround seems to work for me after a few quick tests, though the comments note some possible issues with that approach. Regardless, it would be great to get a similar solution added to vim as a builtin option.


Reply to this email directly, view it on GitHub.
You are receiving this because you commented.Message ID: <vim/vim/issues/7954/3649779739@github.com>

Reply all
Reply to author
Forward
0 new messages