I am using NERDTree to open files. If I am on a file path in vim and I hit gf this works fine for files. For folders, gf opens a new NERDTree buffer in this folder which is fine. I can then choose a file. I am having a hard time to make this NERDTree buffer disappear again: Hitting :bd as suggested here does not do anything.

Recently, I found out the following: When doing :buffers I can find the number of the buffer that I want to delete. Suppose the NERDTree buffer is 5, I can do then :5bd, and the NERDTree buffer is gone.

This works fine. However, when I try to make it easier by getting the number of the current buffer, from :execute bufnr("%") "bd" <CR>, it does not work.

In that context, I saw that the numbers from :buffer and :echo bufnr("%") are different for these NERDTree buffers.

Did anyone ever encounter this and/or has a solution?

Cheers and many thanks,

Tim

  • What is the reason you want to get rid of Nerdtree buffer? Is your objective is to make the file opened via Nerdtree the only file shown in vim screen? – tivn Sep 17 at 10:38
  • Hi tivn, the reason I want to get rid of the Nerdtree buffer is that I just do not need it anymore after the operation (opening a specific file) is done. When cycling through the open buffers with :bprevious/:bnext, I usually do not need the Nerdtree buffer to be open all the time. – Timmi Sep 18 at 11:22
  • You're not meant to close the NERDTree buffer (or most plugin-created buffers) in that manner. Use :NERDTreeToggle or :NERDTreeClose as @tivn suggested. – ZeroKnight Sep 19 at 6:59

NERDTree provides two command that basically can be used to make it disappear: :NERDTreeClose and :NERDTreeToggle. Can you try them?

NERDTree buffer is unlisted -- which can be confirmed by indicator u when you type :buffers! . That means it is normally not included in :bprevious and :bnext. So just by making its window disappear using two commands above is sufficient I guess.

  • Hi tivn, many thanks for your suggestion. I tried it and :NERDTreeClose works for the first time, opening the NERDTree buffer as I described. However, when opening again a folder with gf it opens a new NERDTree buffer which I cannot close anymore. This buffer then also prevents me from cyclig through it: I cannot use :bprevious/:bnext to cycle through it. So what you say is sufficient for only opening one folder. Do you have an idea what to do when opening a second NERDTree buffer via gf? Many thanks in advance. – Timmi Sep 19 at 14:13
  • @Timmi Sorry, what you said is quite confusing to me. Can you type :buffers! (note the exclamation mark) at the time you are being prevented and then take a screenshot of this? – tivn Sep 19 at 17:07
  • Hi @tivn, many thanks for your patience. I did what you suggested and when prevented to cycle through all open buffers, I obtain: timsdata.firewall-gateway.com/index.php/s/NZwSzSMNHbnXwmq (sorry if this is not the correct way to provide the screenshot!) Thesis.html and myfile are files. test is a folder. Do you need any other information? Many thanks in advance! – Timmi Sep 20 at 19:20

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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