I'm setting up an environment to automatically recompile .tex files on save. I've found solution for this:

autocmd BufWritePost *.tex :! latexmk -pdf

But is there any better way to do this? It seems like *.tex check will be executed every time I save a file, be it .c or .md file (maybe I'm wrong about this). Could I possibly place this option somewhere so it is executed only when I save .tex files? Something like ftplugin/tex.vim (I couldn't make it working there).

1 Answer 1


Ftplugin is the right way to go you simply didn't use the right file.

When you open a test.tex file and use set ft? the result is plaintex.

So you need to put your code in ftplugin/plaintex.vim

And I would advise to put it in ~/.vim/after/ftplugin/plaintex.vim this way you are sure that you don't override already existing ftplugins.

You might want to read :h ftplugin-name and :h ftplugin-overrule

  • you are linking twice to the same help section Jun 7, 2019 at 11:43
  • Thank you @ChristianBrabandt I fixed it :)
    – statox
    Jun 7, 2019 at 11:45
  • And then your autocommand can use the special <buffer> pattern
    – D. Ben Knoble
    Jun 7, 2019 at 12:40
  • Note also that LaTeX uses filetype=tex
    – D. Ben Knoble
    Jun 7, 2019 at 20:03

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.