2009-12-08 65 views
27

有沒有人知道如何在不使用NERDTree的情況下關閉VIM中的緩衝區? NERD Tree通常會將您的顯示器分成兩個垂直窗口(左側的瀏覽器,右側的主窗口)。如果你關閉一個緩衝區,那麼你將被縮小到一個巨大的文件瀏覽窗口。如果您選擇另一個文件,則會打開第二個窗口,但是會將其分開。有任何想法嗎?VIM和NERD樹 - 正確關閉緩衝區

回答

12

我不使用NERD樹,但如果我理解正確,您希望在不關閉窗口的情況下關閉緩衝區。如果我的推理是正確的,請嘗試使用此腳本。

" Delete buffer while keeping window layout (don't close buffer's windows). 
" Version 2008-11-18 from http://vim.wikia.com/wiki/VimTip165 
if v:version < 700 || exists('loaded_bclose') || &cp 
finish 
endif 
let loaded_bclose = 1 
if !exists('bclose_multiple') 
let bclose_multiple = 1 
endif 

" Display an error message. 
function! s:Warn(msg) 
echohl ErrorMsg 
echomsg a:msg 
echohl NONE 
endfunction 

" Command ':Bclose' executes ':bd' to delete buffer in current window. 
" The window will show the alternate buffer (Ctrl-^) if it exists, 
" or the previous buffer (:bp), or a blank buffer if no previous. 
" Command ':Bclose!' is the same, but executes ':bd!' (discard changes). 
" An optional argument can specify which buffer to close (name or number). 
function! s:Bclose(bang, buffer) 
if empty(a:buffer) 
let btarget = bufnr('%') 
elseif a:buffer =~ '^\d\+$' 
let btarget = bufnr(str2nr(a:buffer)) 
else 
let btarget = bufnr(a:buffer) 
endif 
if btarget < 0 
call s:Warn('No matching buffer for '.a:buffer) 
return 
endif 
if empty(a:bang) && getbufvar(btarget, '&modified') 
call s:Warn('No write since last change for buffer '.btarget.' (use :Bclose!)') 
return 
endif 
" Numbers of windows that view target buffer which we will delete. 
let wnums = filter(range(1, winnr('$')), 'winbufnr(v:val) == btarget') 
if !g:bclose_multiple && len(wnums) > 1 
call s:Warn('Buffer is in multiple windows (use ":let bclose_multiple=1")') 
return 
endif 
let wcurrent = winnr() 
for w in wnums 
execute w.'wincmd w' 
let prevbuf = bufnr('#') 
if prevbuf > 0 && buflisted(prevbuf) && prevbuf != w 
buffer # 
else 
bprevious 
endif 
if btarget == bufnr('%') 
" Numbers of listed buffers which are not the target to be deleted. 
let blisted = filter(range(1, bufnr('$')), 'buflisted(v:val) && v:val != btarget') 
" Listed, not target, and not displayed. 
let bhidden = filter(copy(blisted), 'bufwinnr(v:val) < 0') 
" Take the first buffer, if any (could be more intelligent). 
let bjump = (bhidden + blisted + [-1])[0] 
if bjump > 0 
execute 'buffer '.bjump 
else 
execute 'enew'.a:bang 
endif 
endif 
endfor 
execute 'bdelete'.a:bang.' '.btarget 
execute wcurrent.'wincmd w' 
endfunction 
command! -bang -complete=buffer -nargs=? Bclose call <SID>Bclose('<bang>', '<args>') 
nnoremap <silent> <Leader>bd :Bclose<CR> 
nnoremap <silent> <Leader>bD :Bclose!<CR> 
+0

真棒......那正是我一直在尋找 – 2010-03-19 18:56:22

+0

thakns,偉大的工程 - 而不僅可用於NERDTree! – nihique 2011-07-30 22:45:43

5

:bd適用於Vim 7.3和NERDTree 4.1.0。

+0

是的,剛剛更新了MacVim 7.3,現在它也適用於我。 – mxgrn 2010-11-11 19:08:40

+2

嗯,我在Ubuntu 12.04上的Vim 7.3和NERDTree 4.1上,我仍然有OP的問題。 – Milimetric 2012-09-19 02:49:18

+0

在Windows 7機器上,在Vim 7.4,NERDTree 4.2.0上不起作用。 – Juan 2015-05-07 17:27:16

8

bufkill插件似乎也解決了這個問題。

22

試試這個映射:nnoremap <leader>q :bp<cr>:bd #<cr>

+0

的偉大工程,這麼簡單的:) – newUserNameHere 2014-11-30 17:56:35

+0

絕對華麗的解決方案。這是在我的舌頭上。 – cbartondock 2017-04-26 20:08:24