Spearfisher Spearfisher - 1 year ago 100
Javascript Question

Auto-indent javascript code in Vim on save

I am using the awesome

plugin for coding in Go with vim. (https://github.com/fatih/vim-go)

The feature that I love is the auto-indent on save. I'd like to get the same behavior for javascript files. Do you know any way to easy replicate this in js?

Many thanks

Answer Source

There exists a general autoformatting plugin for vim called vim-autoformat which integrates the js-beautifier (the engine behind the online application http://jsbeautifier.org/) and more.

It provides an :Autoformat command, which you can bind to the BufWrite event like so

au BufWrite * :Autoformat

EDIT For if you're only interested in indenting your file (not full-fledged formatting): vim-autoformat falls back to auto indenting your file if js-beautify is not installed.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download