How to open or close NERDTree and Tagbar using <leader> \?
I want <leader>\ open or close NERDTree and Tagbar under the following conditions:
- Only close both if NERDTree and Tagbar open.
- Open both if NERDTree and Tagbar are closed OR if it is already open
So far in VIMRC I have had:
nmap <leader>\ :NERDTreeToggle<CR> :TagbarToggle<CR> This does not work because if it is open and the other is closed. He will open the one that was closed and closed the one that was open.
How can this be achieved?
+4
2 answers
You need to use a function that checks if the plugin's windows are open or not, and then acts accordingly. This should work, as well as returning to the window 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> +13