# #StackBounty: #buffers #autocmd Run autocmd when creating/deleting a buffer

### Bounty: 50

I have two functions `tex#PDFOpen()` and `tex#PDFClose()` to automatically open and close the pdf file created by a TeX document.

The way I assign them to autocommands is via the following autogroup in `~/.config/nvim/ftplugin/tex.vim`

``````augroup TeXGroup
autocmd BufEnter <buffer> call tex#PDFOpen()
autocmd BufUnload <buffer> call tex#PDFClose()
augroup END
``````

Now if I open a .tex file `tex#PDFOpen()` is called, but if edit another buffer with `:e ...`, then `tex#PDFClose()` is called, which I don’t want. If I switch back to the buffer with the .tex file, `tex#PDFOpen()` is called again, which I also don’t want.

Basically, `tex#PDFOpen()` should only be called

1. on startup, if the argument of `nvim` is a .tex file;
2. when editing a .tex file in a new buffer.

Similarly, `tex#PDFClose()` should only be called

1. on exit, if one of the buffers contains a .tex file;
2. when closing a buffer with a .tex file with `:bd`.

I’ve tried every event listed here, but none of them have worked.

Get this bounty!!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.