Sometimes during my workflow, I might want to open/search for a file on the go. I'd like to map a single key (say F6) to open and close NERDTree (ie. I want to toggle NERDTree view). Also, I'd like NERDTree to display hidden files and folders in its toggled view and I'd also like it to close automatically when I open a file for editing if that is possible. Thanks !
nmap <F6> :NERDTreeToggle<CR>
<F6> is the key you want to map.
To open NerdTree :
To switch beetween nerdtree and file opened windows :
ctrl + ww (double w)
ctrl + w + (h/j/k/l)
To close NerdTree (in nerdtree window) :
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.
The following function tracks open netrw buffers and calls
Lexplore accordingly. Add it to your
.vimrc and pick your mapping key.
let g:NetrwIsOpen=0 function! ToggleNetrw() if g:NetrwIsOpen let i = bufnr("$") while (i >= 1) if (getbufvar(i, "&filetype") == "netrw") silent exe "bwipeout " . i endif let i-=1 endwhile let g:NetrwIsOpen=0 else let g:NetrwIsOpen=1 silent Lexplore endif endfunction noremap <silent> <F2> :call ToggleNetrw()<CR>
This would do exactly what you've asked for:
nnoremap <silent> <expr> <F6> g:NERDTree.IsOpen() ? "\:NERDTreeClose<CR>" : bufexists(expand('%')) ? "\:NERDTreeFind<CR>" : "\:NERDTree<CR>"