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 !
In your vimrc
add
nmap <F6> :NERDTreeToggle<CR>
where <F6>
is the key you want to map.
-
Works great but how do I show hidden files and folders at the same time? – evil_potato Oct 19 '16 at 9:04
-
3@evil_potato
shift + i
will toggle hidden files. If you want to set as a default behaviour addlet NERDTreeShowHidden=1
to your.vimrc
– Atropo Oct 19 '16 at 9:30 -
3@evil_potato Also to close automatically when open a file for editing add
let NERDTreeQuitOnOpen=1
to your.vimrc
– dNitro Oct 19 '16 at 10:57
To open NerdTree :
:NERDTreeFocus
To switch beetween nerdtree and file opened windows :
ctrl + ww (double w)
or
ctrl + w + (h/j/k/l)
To close NerdTree (in nerdtree window) :
:q
This would do exactly what you've asked for:
nnoremap <silent> <expr> <F6> g:NERDTree.IsOpen() ? "\:NERDTreeClose<CR>" : bufexists(expand('%')) ? "\:NERDTreeFind<CR>" : "\:NERDTree<CR>"
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.
-
This is exactly what I was looking for but what about opening it again, do I need to map another key or is there a native shortcut that will open NERDTree again? – Deesbek Apr 12 '18 at 10:01
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>
-
4Could you explain how it is better than the command
NERDTreeToggle
which comes with NerdTree? – statox Sep 12 '17 at 8:01 -
@statox - the idea is to open with a singe key (or probably two keys combination) - instead of typing ":" + "N" + "E" + "R" + "D" + "T" + "r" + "e" + "e" and hit
Enter
. Exactly what I'm after right now :) – pesho hristov Jan 31 '20 at 8:43 -
2@peshohristov If the goal is to reduce the number of keystrokes I think a mapping in the lines of
nnoremap <single-key> :NERDTreeToggle<CR>
does the trick, that is why I asked OP to give more details on the usefulness of their answer. – statox Jan 31 '20 at 8:48
If you're stuck with a Mac with the absent function keys (touchbar), you can toggle NERDTree by mapping the Ctrl
key like so within your .vimrc
file:
map <C-n> :NERDTreeToggle<CR>
Save the changes, open Vim, and then toggle NERDTree with Ctrl
+n
.
:h
;) – nobe4 Oct 19 '16 at 9:28