11

When using NERDTree plugin the default behavior is to open a new file and keeping the NERDTree buffer open. What I want to do is to keep this default behavior but also overriding it when I want so the NERDTree buffer will be closed after I opened a file.

I know I can use let NERDTreeQuitOnOpen=1 to automatically close the NERDTree buffer when I open a new file but this option totally overrides the default behavior (which is not what I want).

Ideally I would like to know if there is a built-in feature in NERDTree to do what I want. (I've been searching but didn't find anything like that so I suppose it doesn't exists actually)

And if that doesn't exists I had two ideas:

  • First one is to create a function which would set NERDTreeQuitOnOpen to 1, open the file and then set again NERDTreeQuitOnOpen to 0.

  • Second one is to create the following mapping: map d<CR> <CR> :NERDTree <CR> :bd<CR> which opens the file (first <CR>), goes back to NERDTree buffer (:NERDTree <CR>) and close this buffer (:bd <CR>). I should improve it so the mapping only exists in the NERDTree buffer but I feel that's a pretty ugly solution and I thik it can be improved.

Which idea would be the best and why? Or is there an even better option?

4
  • 1
    Obligatory article: Oil and vinegar - split windows and the project drawer Commented Jun 1, 2015 at 14:38
  • @Peter: Thanks for your comment, the article is pretty interesting and I'll probably give a try to netrw and the concept of split explorer. I think I understand the advantage of what is explained in the article but the problem is that it doesn't actually solve my problem it just advise to change the tool I use. I'm not against changing my habits but I'd like to solve my problem first and then try a new tool :)
    – statox
    Commented Jun 1, 2015 at 15:09
  • Related, unanswered question on SU: superuser.com/q/821720/334516. Maybe map d<cr> <cr>:NERDTreeClose<cr> for the mapping?
    – muru
    Commented Jun 1, 2015 at 20:36
  • @muru: Thanks you, your mapping works fine and it seems less ugly than mine :) About the question on SU, I'm not sure it is totally related since he wants to open the file in the buffer of NERDTree whereas I want to open it in a new buffer and then close NERDTree buffer: He ends up with a split windows whereas I want to end up with 2 buffers not in a split window.
    – statox
    Commented Jun 2, 2015 at 7:37

1 Answer 1

5

As the question doesn't seem to generate a lot of answer I'll just answer it with the suggestion of @muru which is what I have used for a week before I switched to the netrw explorer. (Of course if a better option answer comes up I'll gladly unmark mine as accepted and accept the new one)

The idea is to use the folowing mapping:

map d<cr> <cr>:NERDTreeClose<cr>

Which opens the file as it is done usually and then calls the function which close the NERDTree buffer no matter where the cursor is.

EDIT: I recently switched back to NERDTree and reworked my solution: The mapping I suggested before was working but as it existed in every buffer I had to double press d each time I wanted to delete something which is pretty annoying. (Maybe I could have changed the timeout settings but I like the way it is currently set for me, also I didn't want to map it on another key combination).

So I replaced my first solution by the following lines to my .vimrc:

autocmd BufEnter NERD_tree_* nmap  d<CR> <CR> :NERDTreeToggle <CR>
autocmd BufLeave NERD_tree_* unmap d<CR>

This way the mapping is created only in the NERDTree buffers and it doesn't mess with my workflow in the other buffers.

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.