Auto indent javascript code in Vim on save

I am using an awesome vim-go

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

A feature I love is automatic indent on save. I would like to get the same behavior for javascript files. Do you know any way to easily reproduce this in js?

Many thanks

+3


source to share


2 answers


There is a generic auto-formatting module for vim called vim-autoformat that bundles js-beautifier (engine behind http://jsbeautifier.org/ online app ), etc.

It provides a command :Autoformat

that you can bind to an event BufWrite

, for example:



au BufWrite * :Autoformat

      

EDIT If you are interested in indenting your file (not full formatting): vim-autoformat falls back to auto indenting your file if js-beautify is not installed.

+1


source


You don't even need a vim auto indent plugin. Once you open the file in vim press these keys:

gg

      

The cursor should go to the beginning of the file. And then enter this:



=G

      

which basically says "remove trailing spaces from cursor position to end of file". Happy coding!

+3


source







All Articles