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>
D. Ben Knoble
  • 26,070
  • 3
  • 29
  • 65

1 Answers1

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
  &quot; Remove all vimrc autocommands
  autocmd!
  au BufNewFile,BufRead *.html so &lt;sfile&gt;:h/html.vim
augroup END

Chris Heithoff
  • 1,312
  • 3
  • 12