After deleting a buffer, that buffer becomes the alternate buffer?

Assume I have three open buffers, call them a, b and c. And I have only one window, which is currently displaying a.

What I want to do is

  1. :bd to close buffer a
  2. Let’s say now my only window displays buffer b.
  3. Press <C-^> in order to switch to the alternate buffer.

Desired behavior: the window now displays buffer c, the only other available buffer.

Real behavior: the window now displays the deleted buffer a, which apparently became the alternate buffer.

What do you mean by “the window now displays the deleted buffer”?

In step 1. I closed buffer a. So vim shouldn’t know about it anymore. After step 3, the content of buffer a is displayed again.

That’s pretty weird. Are you sure the contents of buffer a and the other file aren’t the same?

Yes. Have you tested it on your setup? Because this is the behavior that happens with nvim -u NORC.

My bad, I actually was able to reproduce this; just didn’t realize.

Looks like using <C-^> switches to which buffer you’re on depending on the file which was last opened, edited, or, deleted in your case (for a bit more information read :h CTRL-6. I’m pretty sure this is a feature rather than a bug :slight_smile:

Thanks a lot for investigating! I didn’t know how to find the help file for this command, this helped me a lot. I suspected that this was a feature rather than a bug, but I was wondering if there’s a way around it.

Maybe I should clarify a common use case I have for it: Often I edit one file a, then I use :E to open NerdTree and search for another file b and open it. Then I want to go back and forth several times between a and b. When I hit <C-^> to do this, I get the NerdTree buffer, which is now completely empty. So that doesn’t help. But even if I delete the NerdTree buffer with :bd, it doesn’t help because now I’m back in file b and if I do <C-^> again, it re-opens the closed NerdTree buffer.

I have in the meantime found that other people had similar problems:

Ah yeah, I could imagine myself running across that. If you haven’t found a solution yet, maybe you can just use :bn and :bp (map it to some keybinding) for the meantime? I know it doesn’t really toggle through the file which was last opened, but it might suffice according to your use case of there being 2 files.

Thanks. Your suggestion of using :bn and :bp made me search a bit more and I found an interesting command I didn’t know yet: :bwipeout. It not only removes a buffer, but it removes everything associated with it, including its position in the jumplist etc. Using this I, can easily remove the netrw buffer for good.