微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

VIM和NERD树 – 正确关闭缓冲区

有没有人知道如何使用NERDTree时不关闭所有的窗口,关闭VIM中的缓冲区? NERD树通常将显示分解为两个垂直窗口(左侧的浏览器,然后右侧的主窗口)。如果你关闭一个缓冲区,那么你被缩减到一个巨大的文件浏览窗口。如果选择另一个文件,则打开第二个窗口,但将其水平分开。有任何想法吗?
我不使用NERD Tree,但如果我理解正确,你希望关闭一个缓冲区而不关闭窗口。如果我的推理是正确的,请尝试这个脚本。
" 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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐