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 !

  • 2
    Hi, welcome to the site! Please keep in mind that it's better to ask multiple questions instead of one with different points. i.e. here you could ask another question regarding the hidden files and folders, because the main question is definitely about the toggle (which you already got an answer for). Also don't forget to read the plugin documentation and to spend hours in :h ;) – nobe4 Oct 19 '16 at 9:28

In your vimrc add

nmap <F6> :NERDTreeToggle<CR>

where <F6> is the key you want to map.

| improve this answer | |
  • 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 add let 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 :


To switch beetween nerdtree and file opened windows :

ctrl + ww (double w)


ctrl + w + (h/j/k/l)

To close NerdTree (in nerdtree window) :

| improve this answer | |

This would do exactly what you've asked for:

nnoremap <silent> <expr> <F6> g:NERDTree.IsOpen() ? "\:NERDTreeClose<CR>" : bufexists(expand('%')) ? "\:NERDTreeFind<CR>" : "\:NERDTree<CR>"
| improve this answer | |

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.

| improve this answer | |
  • 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 
            let i-=1
        let g:NetrwIsOpen=0
        let g:NetrwIsOpen=1
        silent Lexplore

noremap <silent> <F2> :call ToggleNetrw()<CR>
| improve this answer | |
  • 4
    Could 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 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 at 8:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.