Vim: Combining autocmd?

Tag: vim , autocmd Author: wq1812 Date: 2009-08-05

I need to do the logical-and of two autocmd events in vim. Basically, the command has to run on an InsertLeave when the FileType is tex. It seems like this should work (in a .vimrc):

autocmd FileType tex :autocmd InsertLeave :w

But it doesn't. The nested option doesn't seem to help either, even though the manual indicates it should.

Its easy to do a logical-OR:

autocmd BufEnter,BufLeave ...

it mustn't be too hard to do a logical-AND.

You should take the ZyX’s solution as answer, since it solves the local buffer problem of Eevee’s.
Done, but I'm taking your word for it. I seem to remember the local buffers problem, but I haven't written latex in years, and switched to emacs 2 years ago.

Best Answer

I have a correction to @Eevee answer: to make autocommand work for one buffer only, you should use

augroup TexAutoWrite
    autocmd FileType tex :autocmd! TexAutoWrite InsertLeave <buffer> :update
augroup END

, see `:h autocmd-buflocal.

comments:

This is brilliant. Now I can re-render asciidoc documents on switching to my browser.
Ouch. Problem: The second autocommand added up every time you switch to a tex file. After a short time vim starts to save lots of times in a row after you leave insert mode.
@Profpatsch Corrected.
Whoa, it’s that easy to prevent it? Okay…

Other Answer1

InsertLeave still needs a parameter.

This works for me:

autocmd FileType tex :autocmd InsertLeave * :w

Note that this behavior will remain if you later edit a non-tex file in the same buffer. I'm not sure if there's a simple way to remove it when editing anything but a certain type of file.

comments:

Indeed, works like a charm.

Other Answer2

You can use the pattern option of the autocmnd.

autocmd InsertLeave *.tex w