I see that for some reason people like to use
:bdelete to close buffers. That's a very strange practice, IMO. Let's recall what is that
:bdelete all about.
A buffer in Vim can 1) exist (
bufexists() returns true); 2) be "loaded" (
bufloaded() returns true), and 3) be "listed" (
buflisted() returns true). Please, note that (2) and (3) are fully independent one of another, and any combination of (2) and (3) is legal for any buffer.
"Loaded" indicates that the buffer already has some contents attached and needs not to be read from disk (or whatever else) before showing it in some window. However, in practice one may also view an "unloaded" buffer as some sort of MRU record (a number, file name, some bookmarks and such, but no content at all).
At the same time the "listed" flag only indicates that the buffer should appear in the output of
:ls command. But note that even if some buffer is unlisted, it still exists (and can even be "loaded") and can be seen in the output of
:bdelete is basically a shortcut for
:set nobuflisted together. Moreover, the event called
BufDelete is actually triggered when some buffer is set to
nobuflisted, no matter if it's loaded or not. Read
:h BufDelete carefully, it's all written there!
:bdelete is a useless command, IMO. One "hides" buffers when he doesn't need to work on them anymore; one also "unloads" buffers to spare a bit of RAM; and on rare occasions one also "wipes" buffers to clean up some editing history. But what's good in
:bdelete is beyond my reach.
So it really looks strange and counter-intuitive to require
:bdelete (and to rely ultimately on
buflisted) in this case. And on the other hand it seems quite okay to trap
BufUnload as it was done.
Now to the question "why it doesn't work as I expect". The problem is that when your buffer gets hidden (i.e. not shown in any open window anymore) for any reason, Vim must decide what to do with the loaded content (after all, it takes some RAM and Vim is really peculiar about that).
So the first thing Vim inspects is a buffer-local
:h 'bufhide' option, and if it's empty then it's a global
:h 'hidden' option. By default nothing is set, and so Vim disposes the buffer content to save a couple of RAM bytes (and this is where
BufUnload is triggered).
Now you want to keep some "pdf"-file open for a long time, and it's only natural to assume that you are also keen to keep "tex"-file loaded in memory too. To achieve this you should either globally
:set hidden to keep all files in memory as long as possible (not a problem nowadays, is it?), or set it for tex files only:
Now to trigger
BufUnload for real you need
:bunload (these commands always unload buffer contents, no matter what your options are). While
ZZ etc. etc. will keep "tex" in memory, and so "pdf" remains open too.