How can I close a buffer without closing the window?

前端 未结 16 841
鱼传尺愫
鱼传尺愫 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 13:45

    I messed with this a bit and finally came up with:

    :bp | sp | bn | bd
    

    Here's the copy/paste version for key mapping:

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

    I've tested it a fair bit and it works consistently in various conditions. When used on the last buffer it will leave you with a new blank buffer.

    Throw this in your .vimrc:

    map <leader>q :bp<bar>sp<bar>bn<bar>bd<CR>
    

    Restart Vim, or just :source ~/.vimrc for changes to take effect. Next time you want to close a buffer just type: \q (if \ is your leader key)

    0 讨论(0)
  • 2020-12-12 13:45

    Would

    :enew
    

    do what you want? it will edit a new, unnamed buffer in the current window leaving the existing file open in any other windows.

    0 讨论(0)
  • 2020-12-12 13:46

    There's a script on the Vim wiki to do this. I don't think there is a builtin that does what you want.

    The latest version of vim-bufkill is on github.

    0 讨论(0)
  • 2020-12-12 13:48
    nmap <leader>d :bprevious<CR>:bdelete #<CR>
    

    Works as it should until one buffer is open in several windows. Good enough unless you want to use the bigger scripts out there.

    Edit: this is what i use right now:

    function! BufferDelete()
        if &modified
            echohl ErrorMsg
            echomsg "No write since last change. Not closing buffer."
            echohl NONE
        else
            let s:total_nr_buffers = len(filter(range(1, bufnr('$')), 'buflisted(v:val)'))
    
            if s:total_nr_buffers == 1
                bdelete
                echo "Buffer deleted. Created new buffer."
            else
                bprevious
                bdelete #
                echo "Buffer deleted."
            endif
        endif
    endfunction
    
    0 讨论(0)
  • 2020-12-12 13:48

    A simple version I use personally is

    :bp|bd#
    

    It goes to the previous buffer and deletes the other buffer (which is actually the original where we jumped from). This does what you would expect in 99% of cases without any complicated scripts.

    As a keyboard shortcut I use the following

    nnoremap <silent> <Leader>c :bp<BAR>bd#<CR>
    
    0 讨论(0)
  • 2020-12-12 13:48

    If you're like me and you came here trying to do the opposite, close the window without closing the buffer, you can do that via:

    :close

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