I'm trying to find a way to have a normal mode mapping that can toggle NERDTree, but when toggling on, tell NERDTree to find the current file.
I know about NERDTreeToggle
and NERDTreeFind
, and what I'm looking to do is essentially a combination of those two commands.
Here's my use case:
<C-\>
and NERDTree will open to the current file.<C-\>
again and NERDTree will close.If you want to close NERDTree window make sure you are inside the NERDTree menu. You can switch windows by using ctrl+w then type :q to close NERDTree window so that you will be left with the window for editing your code.
In NERDTree, press m to bring up the NERDTree Menu, and then you should see an option, labeled o , to open the current node with the system editor associated with that file or directory.
I normally switch screen in vim using ctrl+h or ctrl+l to switch between NerdTree and vim main window. Vim8 got :term command to bring up terminal which is great!
The first answer didn't work for me so I came up with this:
function MyNerdToggle()
if &filetype == 'nerdtree'
:NERDTreeToggle
else
:NERDTreeFind
endif
endfunction
nnoremap <C-\> :call MyNerdToggle()<CR>
This will do exactly what you want:
nnoremap <silent> <expr> <C-\> g:NERDTree.IsOpen() ? "\:NERDTreeClose<CR>" : bufexists(expand('%')) ? "\:NERDTreeFind<CR>" : "\:NERDTree<CR>"
function! NerdTreeToggleFind()
if exists("g:NERDTree") && g:NERDTree.IsOpen()
NERDTreeClose
elseif filereadable(expand('%'))
NERDTreeFind
else
NERDTree
endif
endfunction
nnoremap <C-\> :call NerdTreeToggleFind()<CR>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With