1

When switching buffers with :e# or <ctrl>-^, I commonly see the line with the cursor (the current line) move to the middle of the window (maybe related to the scroll_cursor_halfway function mentioned in this question?). I'm used to this, and it can be very convenient for visually comparing two slightly different versions of the same file by quickly alternating between them so that a visual beat is created for any mismatching text.

However, sometimes it doesn't happen for some unknown reason; the line with the cursor is left on, say, the bottom row of the window, or wherever it was. I understand why this would happen if I'm near the top or bottom of the buffer's contents. But can happen even when the current line is in the middle of a large buffer.

Why does this happen and how can I make it more consistent?

3
  • What is that scroll_cursor_halfway function?
    – romainl
    Commented Sep 30, 2016 at 7:51
  • @romainl see my edit
    – jrw32982
    Commented Sep 30, 2016 at 17:34
  • Comparing files with "visual beat"? Isn't vimdiff better at this? Commented Apr 2, 2017 at 8:14

2 Answers 2

1

I can't answer why it happens, but the way I'm currently making it more consistent:

map <SPACE> :e#<CR>z.0

This maps the spacebar to switch to the alternate file, then move the line with the cursor to the middle of the screen and finally move to the first position in the line.

0

You are probably looking for "Avoid scrolling when switch buffers"

When switching buffers using the Ctrl-^ and :bp commands, Vim will keep the cursor on the line number where it was before switching the buffer but it might change the position of the current line relative to the screen. For example, the cursor may be in line 1234 of the file, with that line at the top of the screen. The user may switch to another buffer, then switch back (:bn :bp), and find that the current line has been repositioned to the middle of the screen.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.