Vim: Combining autocmd?
Asked Answered
C

3

6

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.

Chapel answered 21/8, 2009 at 17:14 Comment(2)
You should take the ZyX’s solution as answer, since it solves the local buffer problem of Eevee’s.Haemo
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.Chapel
H
9

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.

Hufuf answered 10/3, 2011 at 19:13 Comment(3)
This is brilliant. Now I can re-render asciidoc documents on switching to my browser.Haemo
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.Haemo
Whoa, it’s that easy to prevent it? Okay…Haemo
A
4

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.

Aufmann answered 21/8, 2009 at 17:27 Comment(1)
The colon is not really needed. e.g. au FileType go au VimLeave * call system("killall gocode")Adamsen
M
4

You can use the pattern option of the autocmnd.

autocmd InsertLeave *.tex w
Malia answered 10/3, 2011 at 17:46 Comment(1)
This does not work when the file extension does not match the filetype or doesn't exist. That happens when you're doing things like comparing against backup/temp files, or if the location of the file determines its type, e.g. system configuration files.Bayless

© 2022 - 2024 — McMap. All rights reserved.