javascript - 我怎样才能让 Vim 得到正确的 JS 缩进?

标签 javascript vim indentation auto-indent

这里是 Emacs 用户,正在试用 Vim。 :)

我非常喜欢快速击键和整体理念,但我在使用稍微高级的东西时遇到了一些麻烦。我的提示之一是缩进(使用 = 命令)。

请参阅此 JS 片段。这就是 Emacs 的 js2-mode 缩进的方式。我喜欢这个:

var MyClass = declare([], {
    constructor: function(params) {
        if(!params) {
            params = {};
        }

现在这就是 Vim 的用途。该死的 hell :

       var MyClass = declare([], {
constructor: function(params) {
if(!params) {
params = {};
}

这个构造之上的所有代码都得到了适当的缩进,但从那里开始就很糟糕了。我的代码中到处都有这种结构。我检查了 :filetype 并打开了这些开关。我尝试过使用和不使用插件。没搞对。 Vim 是 gVim 7.4,几天前下载的。我见过this question并且显示的代码片段已正确缩进,开箱即用。

有人知道我可以在这里尝试什么吗? 感谢您的任何建议!

最佳答案

在你的.vimrc中:

set  nocompatible " vi is decades old

set expandtab     " use soft tabs set shiftwidth=2  # 2 spaces tabs for JS (?) set softtabstop=2

filetype on 
filetype plugin on 
filetype plugin indent on " auto indent for supported languages (JS included)

要重新格式化所有文件,请输入 gg=G

gg -> go to top of file
= -> indent 
G -> until the end of the file

关于javascript - 我怎样才能让 Vim 得到正确的 JS 缩进?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25266743/

相关文章:

javascript - 如何在 angularjs View 页面中求和价格?

javascript - 结合不同变量的两个 if 语句

VIM 将方向键映射到字母 ABCD

vim - 在 Vim 中缩进一串行

emacs - 在 emacs 中设置每个项目的缩进变量

python - `Ctrl` + `Alt` + `L` 不修复警告 PEP 8 : E122 continuation line missing indentation or outdented

javascript - 使用 JS API 安排 Facebook 帖子

Javascript Chrome 扩展程序不加载 https ://apis. google.com/js/platform.js 脚本

vim 替换反斜杠

Ubuntu 16.04 byobu + tmux + vim 背景颜色问题