如何使用<leader> \?</leader>打开或关闭NERDTree和Tagbar

时间:2011-07-08 11:53:50

标签: vim vim-plugin nerdtree tagbar

我希望<leader>\在以下条件下打开或关闭NERDTree和Tagbar:

  1. 如果NERDTree和Tagbar均已打开,则仅关闭两者
  2. 如果NERDTree和Tagbar已关闭或已打开,则打开两个
  3. 到目前为止,在VIMRC,我有:

    nmap <leader>\ :NERDTreeToggle<CR> :TagbarToggle<CR>
    

    哪个不正常,因为如果一个打开,另一个关闭。它将打开一个已关闭的并关闭已打开的那个。

    如何实现这一目标?

2 个答案:

答案 0 :(得分:13)

您需要使用一个函数来检查插件窗口是否打开,然后相应地执行操作。这应该可以工作,也会跳回到你开始的窗口:

function! ToggleNERDTreeAndTagbar()
    let w:jumpbacktohere = 1

    " Detect which plugins are open
    if exists('t:NERDTreeBufName')
        let nerdtree_open = bufwinnr(t:NERDTreeBufName) != -1
    else
        let nerdtree_open = 0
    endif
    let tagbar_open = bufwinnr('__Tagbar__') != -1

    " Perform the appropriate action
    if nerdtree_open && tagbar_open
        NERDTreeClose
        TagbarClose
    elseif nerdtree_open
        TagbarOpen
    elseif tagbar_open
        NERDTree
    else
        NERDTree
        TagbarOpen
    endif

    " Jump back to the original window
    for window in range(1, winnr('$'))
        execute window . 'wincmd w'
        if exists('w:jumpbacktohere')
            unlet w:jumpbacktohere
            break
        endif
    endfor
endfunction
nnoremap <leader>\ :call ToggleNERDTreeAndTagbar()<CR>

答案 1 :(得分:-1)

嗯......这对我来说在vimrc

toggle选项检查窗口是否已存在,因此不需要自定义函数(@JanLarres或其中一个贡献者)必须将其添加到TagBar:D

" NERDTree
map <leader>n :NERDTreeToggle<CR>

" TagBar
map <leader>t :TagbarToggle<CR>