如何使用\打开或关闭NERDTree和Tagbar?

发布于 2024-11-19 05:37:25 字数 364 浏览 2 评论 0原文

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

  1. 仅当 NERDTree 和 Tagbar 都打开时才关闭两者
  2. 如果 NERDTree 和 Tagbar 都关闭或已经关闭则打开两者到目前为止

,在 VIMRC 中,我有:

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

这并不完全有效,因为如果一个打开,另一个关闭。它会打开关闭的那个并关闭打开的那个。

如何才能实现这一目标?

I want <leader>\ to open or close NERDTree and Tagbar, under the following conditions:

  1. Only close both if NERDTree and Tagbar are both opened
  2. Open both if NERDTree and Tagbar are closed OR if one is already opened

So far, in VIMRC, I have:

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

Which doesn't exactly work, since if one is opened, and the other closed. It would open the one that was closed and close the one that was opened.

How can this be achieved?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忘年祭陌 2024-11-26 05:37:25

您需要使用一个函数来检查插件窗口是否打开,然后采取相应的操作。这应该可以工作,并且还会跳回您开始的窗口:

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>

You need to use a function that checks whether the plugin windows are open or not and then acts accordingly. This should work and will also jump back to the window that you started in:

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>
倾`听者〃 2024-11-26 05:37:25

嗯...这在 vimrc 中适用于我

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

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

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

hmm... this works for me in vimrc

The toggle option checks if the window already exists, so no custom function needed (@JanLarres or one of the contributors) must have added it to TagBar :D

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

" TagBar
map <leader>t :TagbarToggle<CR>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文