17

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 !

  • 1
    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
24

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 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
9

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
3

This would do exactly what you've asked for:

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

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
0

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>
  • 4
    Could you explain how it is better than the command NERDTreeToggle which comes with NerdTree? – statox Sep 12 '17 at 8:01

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.