You can use the
InsertLeave autocommand event to do what you want. You can add something like that in your
.vimrc to create commands for html and js files:
autocmd! InsertLeave *.html,*.js call JSBeautify
InsertLeave will trigger the event each time you exit insert mode.
*.html,*.js to do that only on html and js files.
call JSBeautify is the call you want to do.
Also you could be interested in the
BufWritePre event which will trigger the command just before writing the buffer to the file. Using the command each time you leave insert mode may be not that comfortable I think.
Relevant help topic:
Edit In the comments OP asks how to use different function for different filetypes. The easiest solution I see is using an
autocmd! InsertLeave *.js call JSBeautify
autocmd! InsertLeave *.html call HTMLBeautify
autocmd! InsertLeave *.css call CSSBeautify
augroup allows to group autocommands.
autocmd! purges the group when you need to re-source your
- the 3 autocommands are pretty similar to the first one excepted they are triggered for only one filetype.
More related topics: