Search code examples
vimjs-beautify

Automatically call HTMLBeaufity when exiting Insert mode in Vim


I'd like to automatically format html and js code when I exit insert mode. Currently I have ctrl f mapped to format the current file in my vimrc:

map <c-f> :call JSBeautify()<cr>

Is there a way I can trigger this command each time I exit insert mode? Thanks


Solution

  • Try to put this

    augroup AuJsBeautify
       au!
       au  InsertLeave * call JsBeautify()
    augroup END
    

    in your .vimrc.

    To know more about autocommands, read :h 40.3 and :h autocommand.


    Or if you prefer mapping, you can just map it on your Esc

    inoremap <Esc> <Esc>:call JsBeautify()<cr>