How can I close a buffer without closing the window?

前端 未结 16 842
鱼传尺愫
鱼传尺愫 2020-12-12 13:35

Vim\'s multilayered views (Windows, Buffers and Tabs) left me a little confused. Let\'s say I split the display (:sp) and then select a different buffer to display in each w

相关标签:
16条回答
  • 2020-12-12 14:05

    I used to use :

    :bp<bar>sp<bar>bn<bar>bd<CR>
    

    But I found certain occasions where it closed my window.

    Recently I noticed that I always use this when I am working on a project and need to quickly open my .tmux.conf .zshrc before going back to work.

    For this usage, I find better to :

    • switch back to the buffer I was previously working on with C-6
    • type :bd# to delete the previous buffer (I have mapped it like this : nnoremap <leader>d :bd#<CR>)

    It allows me to control the buffer I'm going back to and feels more natural.

    0 讨论(0)
  • 2020-12-12 14:06

    Here is a very readable vimscript function, which handles all cases well,

    • behave similar to built-in:bd (if only one window, just invoke it!),
      • issue a warning and do nothing if buffer modifed.
      • if no other buffer, create one, via :enew.
      • if alternate buffer exist and in buffer-list, switch to it, else go next, via:bn.
    • more reasonable behavior for multiple-window layout
      • not closing any window,
      • always stay on the original window.
      • for each window that displays current buffer, do as listed above, then delete old current buffer.
    nnoremap <Leader>b :call DeleteCurBufferNotCloseWindow()<CR>
    
    func! DeleteCurBufferNotCloseWindow() abort
        if &modified
            echohl ErrorMsg
            echom "E89: no write since last change"
            echohl None
        elseif winnr('$') == 1
            bd
        else  " multiple window
            let oldbuf = bufnr('%')
            let oldwin = winnr()
            while 1   " all windows that display oldbuf will remain open
                if buflisted(bufnr('#'))
                    b#
                else
                    bn
                    let curbuf = bufnr('%')
                    if curbuf == oldbuf
                        enew    " oldbuf is the only buffer, create one
                    endif
                endif
                let win = bufwinnr(oldbuf)
                if win == -1
                    break
                else        " there are other window that display oldbuf
                    exec win 'wincmd w'
                endif
            endwhile
            " delete oldbuf and restore window to oldwin
            exec oldbuf 'bd'
            exec oldwin 'wincmd w'
        endif
    endfunc
    
    0 讨论(0)
  • 2020-12-12 14:07

    use ":bd" as a command.

    0 讨论(0)
  • 2020-12-12 14:08

    To 'close' a view, use :hid[e]. Works if you have managed to split the viewport or opened multiple files. You can't hide the last buffer on display.

    1 Further tip that helped me: use :e ./path/to/file.work to open a file in viewport without splitting the window.

    P.S. At two days into vim I still have trouble finding the precise help commands. Hopefully this will help someone else keep working until they really get time to understand vim.

    0 讨论(0)
提交回复
热议问题