打开另一个带有分割窗口的文件,并在它们之间切换

打开另一个带有分割窗口的文件,并在它们之间切换

如果我用以下命令打开 vim: $ vim -o a.ext b.ext

我得到一个看起来像这样的窗口

+----------------------+
|           a          |
|                      |
| a.ext ---------------+
|           b          |
|                      |
+ b.ext ---------------+

假设我想打开另一个文件,c.ext.所以我:e c.ext在顶部面板上做了类似的事情。

+----------------------+
|           c          |
|                      |
| c.ext ---------------+
|           b          |
|                      |
+ b.ext ---------------+

但现在该a.ext文件无法访问,并且我无法使用:n.正确的打开方式是什么c.ext,以便我可以重新a.ext使用:n

答案1

我认为您已经可以很好地打开文件,但它们位于单独的缓冲区中,您需要使用:bn(或完整:bnext:bprev)命令导航到给定窗格中的下一个和上一个缓冲区。

答案2

我会尝试 ':e #' 返回到之前打开的文件。

答案3

在评论中,您提出了以下问题:

有没有办法让它只循环该窗格中的文件(不是当前打开的每个文件)?

我不认为 Vim 会跟踪窗口(“窗格”)之前访问过的所有缓冲区。然而,Vim 是可编写脚本的……

这是一个通过使用提供此功能版本的实现自动命令记录(在窗口局部变量) 显示窗口中哪些缓冲区已被激活。

(缩写)命令是:

  • :Hb列出该窗口的历史缓冲区。
  • :Hbn[!] [N]切换到第 N 个下一个历史缓冲区。
    (类似于:bn,但仅限于当前窗口的“历史”缓冲区)
  • :Hbp[!] [N]切换到前 N 个历史缓冲区。
    (类似于:bp,但仅限于当前窗口的“历史”缓冲区)
  • :Hbf [N](“忘记”)从当前窗口的历史缓冲区列表中删除当前缓冲区(或缓冲区号N)。
    如果您不切换到另一个缓冲区来离开并重新进入此窗口,则当前缓冲区将重新添加到历史缓冲区列表中。

以下代码可以放入您的文件.vimrc或单独的文件中(例如,plugin/buffer-history/buffer-history.vim您的文件之一下的某处)runtimepath目录):

augroup UL17179_BufferHistory
    autocmd!
    autocmd BufEnter * call s:RecordBufEnter(0)
    " Grab WinEnter, since BufEnter is not triggered when doing
    " a bare ':split'. This also means that 'forgetting' a buffer is
    " only effective if you switch to another buffer before
    " switching away from the window.
    autocmd WinEnter * call s:RecordBufEnter(1)
augroup END

function! s:EnsureBufferHistory()
    if ! exists('w:BufferHistory')
        let w:BufferHistory = []
    endif
    return w:BufferHistory
endfunction

function! s:RecordBufEnter(w)
    let l = s:EnsureBufferHistory()
    let b = winbufnr(0)
    let i = index(l, b)
    if i >= 0
        unlet l[i]
    endif
    let l += [b]
    redraw
endfunction

function! s:ForgetBuffer(...)
    let l = s:EnsureBufferHistory()
    for b in a:000
        let b = b ? b+0 : winbufnr(0)
        let i = index(l, b)
        if i >= 0
            call remove(l, i)
        else
            try
                echohl WarningMsg
                echomsg 'Buffer' b 'not in history list.'
            finally
                echohl None
            endtry
        endif
    endfor
endfunction

function! s:ShowBuffers()
    let l = s:EnsureBufferHistory()
    for b in l
        echomsg b bufname(b)
    endfor
endfunction

function! s:HistoricalBufferNr(...)
    let  direction = a:0 >= 1 && !a:1 ? -1 : 1
    let move_count = a:0 >= 2 ? max([1, a:2]) : 1

    let current_bn = winbufnr(0)
    let historical_buffers = copy(filter(s:EnsureBufferHistory(),
                \ 'bufexists(v:val)'))
    let i = index(historical_buffers, current_bn)
    if i < 0
        let other_historical_buffers = historical_buffers
    elseif i == 0
        let other_historical_buffers = historical_buffers[1:]
    else
        let other_historical_buffers = historical_buffers[i+1:] +
                    \ historical_buffers[:i-1]
    endif

    if len(other_historical_buffers) <= 0
        try
            echohl ErrorMsg
            echomsg 'This window has no historical buffers!'
        finally
            echohl None
        endtry
        return 0
    endif
    if direction > 0
        let i = (move_count - 1) % len(other_historical_buffers)
    else
        let l = len(other_historical_buffers)
        let i = ((l - 1) * move_count ) % l
    endif
    return other_historical_buffers[i]
endfunction

" If the 1) user does not give a bang and
"        2) we run 'buffer N' (no bang) from inside the function and 
"        3) switching away from the buffer would require a bang,
" then the user will see an ugly 'Error detected while processing
" function' prefix before the usual E37 error message. Hoisting the
" 'buffer<bang> N' into the user-defined command means the user will
" just see a normal E37 message.
command! -nargs=0 -count=1 -bang -bar
            \ HistoricalBufferNext
            \ let s:new_bn = s:HistoricalBufferNr(1, <count>)
            \ | if s:new_bn | exe 'buffer<bang>' s:new_bn | endif
command! -nargs=0 -count=1 -bang -bar
            \ Hbn
            \ HistoricalBufferNext<bang> <count>
command! -nargs=0 -count=1 -bang -bar
            \ HistoricalBufferPrev
            \ let s:new_bn = s:HistoricalBufferNr(0, <count>)
            \ | if s:new_bn | exe 'buffer<bang>' s:new_bn | endif
command! -nargs=0 -count=1 -bang -bar
            \ Hbp
            \ HistoricalBufferPrev<bang> <count>
command! -nargs=* -count=0 -bar
            \ HistoricalBufferForget
            \ call s:ForgetBuffer(<count>, <f-args>)
command! -nargs=* -count=0 -bar
            \ Hbf
            \ HistoricalBufferForget <count> <args>
command! -nargs=0 -bar
            \ HistoricalBuffers
            \ call s:ShowBuffers()
command! -nargs=0 -bar
            \ Hb
            \ HistoricalBuffers

相关内容