0

I use vim pretty extensively for latex compiling. I've a function which automatically compiles the current tex file. I use pdflatex as a compiler, which creates a bunch of different files that it needs to compile the tex file nicely. But these extra files are just visual clutter for me so I've created a shell script that cd-s into the directory and removes the extra tex dependencies.

I've put this line of code in my vimrc to automatically execute that script.

autocmd BufLeave *.tex ! <path-to-the-shell-script>

My problem is this script runs as many times as I compile the tex file, while it does the trick just fine in only one execution. Note that, there is no problem with the shell script, if I run it from the terminal it works perfectly fine but when running it with the above autocmd it seems to run multiple times (as many times I compile the tex file).

I've also tried VimLeave and BufWinLeave, the problem still persists. I've changed the autocmd to run silently, but I was wondering if there's a way to fix this all together?

autocmd BufLeave *.tex silent ! <path-to-the-shell-script>
4
  • This approach seems a bit strange to me. Just spitballin' here but perhaps create a wrapper script that calls pdflatex and does the clean up and make that wrapper script your "compiler"...
    – B Layer
    Sep 20, 2021 at 10:22
  • Fwiw i switched to latexmk after a few years of custom build scripts and i am very happy.
    – D. Ben Knoble
    Sep 20, 2021 at 11:41
  • 1
    As for multiple runs, what does :autocmd show? Possibly you have it installed multiple times
    – D. Ben Knoble
    Sep 20, 2021 at 11:43
  • 1
    Does this answer your question? Why should I use augroup? Sep 20, 2021 at 12:57

1 Answer 1

1

It's likely that your .vimrc is getting sourced multiple times. Because it's possible to add multiple autocommands to the same event, you can end up autocommand duplication.

This example from the help page explains how to define autocommands in a group to avoid this problem.

`:autocmd` adds to the list of autocommands regardless of whether they are
already present.  When your .vimrc file is sourced twice, the autocommands
will appear twice.  To avoid this, define your autocommands in a group, so
that you can easily clear them: >

    augroup vimrc
      " Remove all vimrc autocommands
      autocmd!
      au BufNewFile,BufRead *.html so <sfile>:h/html.vim
    augroup END

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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