Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim: Combining autocmd?

Tags:

vim

autocmd

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.

like image 263
Paul Biggar Avatar asked Aug 21 '09 17:08

Paul Biggar


People also ask

What is Autocmd Vim?

Introduction *autocmd-intro* You can specify commands to be executed automatically when reading or writing a file, when entering or leaving a buffer or window, and when exiting Vim. For example, you can create an autocommand to set the 'cindent' option for files matching *.c.

What is Augroup?

AU Group is the world's leading broker. exclusively specialised in trade receivables.


2 Answers

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.

like image 156
ZyX Avatar answered Sep 25 '22 15:09

ZyX


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.

like image 21
Eevee Avatar answered Sep 22 '22 15:09

Eevee