Sometimes in my workflow, I might want to open or search for a file on the go.

I'd like to map a single key (say F6) to open and close NERDTree (i.e., I want to toggle the 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.

  • 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
    Commented Oct 19, 2016 at 9:28

6 Answers 6


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? Commented Oct 19, 2016 at 9:04
  • 6
    @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
    Commented Oct 19, 2016 at 9:30
  • 4
    @evil_potato Also to close automatically when open a file for editing add let NERDTreeQuitOnOpen=1 to your .vimrc
    – dNitro
    Commented Oct 19, 2016 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) :


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'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.

  • This <C-n> worked on Linux as well, <F6> didn't work for me. Commented Feb 28, 2021 at 13:05

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
    Commented Apr 12, 2018 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>
  • 6
    Could you explain how it is better than the command NERDTreeToggle which comes with NerdTree?
    – statox
    Commented Sep 12, 2017 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 :) Commented Jan 31, 2020 at 8:43
  • 4
    @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
    Commented Jan 31, 2020 at 8:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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