0

I often use :b# to switch back to the previous buffer. I also often delete buffers that I don't need anymore. However, if you delete a buffer, doing :b# opens that deleted buffer back up.

Is there a way to open the last edited buffer that was not deleted? In other words, something that behaves like :b# but that respects deleted buffers?

1
1

Despite its name, :bdelete doesn't really delete the buffer. It only disposes file data, marks buffer "unlisted" and partially deletes metadata. However, buffer number, buffer name and local bookmarks list is preserved. For this reason, :b# and :e# still work as usual (although Vim has to re-read file contents from disk, but it could be barely noticeable nowadays).

Alternatively, you can :bwipeout buffer. But note this has a side-effect: as bookmarks will not be saved, you won't be able to restore the last position when you open the same file next time (it only works if you have viminfo file and some auto-command, as in $VIMRUNTIME/defaults.vim).

However, every buffer can have only one alternative buffer at a time, and so if you do :bw# followed by :e# then it will throw error and don't open anything at all.

If you simply want to switch to some known buffer, you can do either :b <tab> (assuming you have set wildmenu), or :ls and, while seeing buffer list and "press ENTER" prompt, input a command such as :b42.

UPD. Also to note that there exists :bfirst, :bnext, :blast, :bprev and such which take you through the buffer list. As :bdelete makes the buffer unlisted, these commands sort of "respect" deleted buffers by bypassing them altogether. However, the buffer number is assigned by the time the buffer is created and is never changed (except for :file). So traversing the buffer list by :bnext is very different from "jump to the file last used".

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.