简体   繁体   English

切换缓冲区时Vim保持窗口位置

[英]Vim keep window position when switching buffers

A problem I've been having with Vim in general is that when I switch buffers in a window (either :[n]b or MiniBufExpl) the cursor position stays the same, but the window always positions itself so the row the cursor on is in the middle. 我在Vim中遇到的一个问题是,当我在一个窗口中切换缓冲区时( :[n]b或MiniBufExpl),光标位置保持不变,但窗口始终处于自身位置,因此光标所在的行是在中间。

This is really annoying me since I visually remember where the top/bottom parts of the window are, not where they would be should the cursor be positioned in the middle of the window. 这真让我感到烦恼,因为我在视觉上记得窗口的顶部/底部部分在哪里,而不是光标位于窗口中间的位置。

Is there a setting I can change to preserve a window's position over a buffer? 是否有一个设置我可以更改以保持窗口在缓冲区上的位置?

It's interesting to note that it didn't bother me until I've read your question, lol. 有趣的是,在我读完你的问题之前,它并没有打扰我,哈哈。

Try this: 尝试这个:

if v:version >= 700
  au BufLeave * let b:winview = winsaveview()
  au BufEnter * if(exists('b:winview')) | call winrestview(b:winview) | endif
endif

That script posted by @dnets always sets the cursor at the top of the screen for me, albeit at the same position in the file. @dnets发布的脚本总是将光标设置在屏幕顶部,虽然文件位于同一位置。

I changed it to this (copied from http://vim.wikia.com/wiki/Avoid_scrolling_when_switch_buffers ) 我把它改成了这个(复制自http://vim.wikia.com/wiki/Avoid_scrolling_when_switch_buffers

" Save current view settings on a per-window, per-buffer basis.
function! AutoSaveWinView()
    if !exists("w:SavedBufView")
        let w:SavedBufView = {}
    endif
    let w:SavedBufView[bufnr("%")] = winsaveview()
endfunction

" Restore current view settings.
function! AutoRestoreWinView()
    let buf = bufnr("%")
    if exists("w:SavedBufView") && has_key(w:SavedBufView, buf)
        let v = winsaveview()
        let atStartOfFile = v.lnum == 1 && v.col == 0
        if atStartOfFile && !&diff
            call winrestview(w:SavedBufView[buf])
        endif
        unlet w:SavedBufView[buf]
    endif
endfunction

" When switching buffers, preserve window view.
if v:version >= 700
    autocmd BufLeave * call AutoSaveWinView()
    autocmd BufEnter * call AutoRestoreWinView()
endif

And it now works as I want, screen and cursor position saved. 它现在可以正常工作,保存屏幕和光标位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM