代码之家  ›  专栏  ›  技术社区  ›  RusAlex

VIM关闭缓冲区但不打开窗口

  •  24
  • RusAlex  · 技术社区  · 14 年前

    如果我有两个水平/垂直拆分的缓冲区,想要关闭其中一个,但我不想关闭一个窗口。我要保持拆分窗口的位置与关闭缓冲区之前的位置相同。

    如果我按:bd,关闭缓冲区的窗口也会关闭。

    7 回复  |  直到 9 年前
        1
  •  27
  •   valid    11 年前

    nmap ,d :b#<bar>bd#<CR>

    ,d

    • nmap
    • , d
    • :b#<bar>bd#<CR>

    • :
    • b#
    • <bar> |
    • bd#
    • <CR> Return Enter

    ~/.vimrc

    :nmap ,d :b#<bar>bd#<CR>

    -- INSERT -- i n here

    • :windo b# bd
    • <
    • :nmap ,
    • :ls
        2
  •  9
  •   too much php    14 年前

    :enew

        3
  •  4
  •   icecrime    14 年前
        4
  •  2
  •   Keith Gaddis    14 年前

        5
  •  1
  •   m05quit0    10 年前

    @rusalex version+激活当前缓冲区,最终需要删除两次缓冲区。

    nmap ,d :b#<bar>bd#<bar>b<CR>
    
        6
  •  1
  •   Endre Both    9 年前

    下面是对@zenbro提供的答案的一个变体,它可以保持所有(拆分)窗口和选项卡打开,即使关闭的缓冲区是最后一个。

    该函数将所有指向当前缓冲区(即将关闭的缓冲区)的窗口切换到下一个缓冲区(如果当前缓冲区是最后一个缓冲区,则切换到新的缓冲区)。

    function! CloseBuffer()
        let curBuf = bufnr('%')
        let curTab = tabpagenr()
        exe 'bnext'
    
        " If in last buffer, create empty buffer
        if curBuf == bufnr('%')
            exe 'enew'
        endif
    
        " Loop through tabs
        for i in range(tabpagenr('$'))
            " Go to tab (is there a way with inactive tabs?)
            exe 'tabnext ' . (i + 1)
            " Store active window nr to restore later
            let curWin = winnr()
            " Loop through windows pointing to buffer
            let winnr = bufwinnr(curBuf)
            while (winnr >= 0)
                " Go to window and switch to next buffer
                exe winnr . 'wincmd w | bnext'
                " Restore active window
                exe curWin . 'wincmd w'
                let winnr = bufwinnr(curBuf)
            endwhile
        endfor
    
        " Close buffer, restore active tab
        exe 'bd' . curBuf
        exe 'tabnext ' . curTab  
    endfunction
    

    地图:

    map <silent> <F4> :call CloseBuffer()<cr>
    
        7
  •  0
  •   zenbro    10 年前

    以下是一些解决方法:

    function! CloseSplitOrDeleteBuffer()
      let curNr = winnr()
      let curBuf = bufnr('%')
      wincmd w                    " try to move on next split
      if winnr() == curNr         " there is no split"
        exe 'bdelete'
      elseif curBuf != bufnr('%') " there is split with another buffer
        wincmd W                  " move back"
        exe 'bdelete'
      else                        " there is split with same buffer"
        wincmd W
        wincmd c
      endif
    endfunction
    
    
    nnoremap <silent> Q :call CloseSplitOrDeleteBuffer()<CR>