保存时自动缩进 Vim 中的 javascript 代码

Auto-indent javascript code in Vim on save

我正在使用很棒的 vim-go 插件在 Go 中使用 vim 进行编码。 (https://github.com/fatih/vim-go)

我喜欢的功能是保存时自动缩进。我想对 javascript 文件获得相同的行为。你知道有什么方法可以在 js 中轻松复制它吗?

非常感谢

vim 有一个通用的自动格式化插件,名为 vim-autoformat which integrates the js-beautifier (the engine behind the online application http://jsbeautifier.org/) 等等。

它提供了一个:Autoformat命令,你可以像这样绑定到BufWrite事件

au BufWrite * :Autoformat

编辑 如果您只对缩进文件感兴趣(而不是完全格式化):vim-autoformat 如果未安装 js-beautify,则自动缩进文件。

您甚至不需要插件即可在 vim 中自动缩进。在 vim 中打开文件后,请立即按这些键:

gg

您的光标应该移动到文件的开头。然后输入:

=G

基本上就是说,"remove trailing spaces from the cursor position to the end of the file"。编码愉快!