简体   繁体   English

我怎样才能让Vim使JS缩进正确?

[英]How can I have Vim get this JS indenting right?

Emacs user here, giving Vim a go. Emacs用户在这里,让Vim受益匪浅。 :) :)

I like the quick keystrokes and the overall philosphy very much, but I've been hitting some trouble with the slightly more advanced stuff. 我非常喜欢快速键击和整体哲学,但是使用稍微高级的东西却遇到了麻烦。 One of my gripes is indenting (using the = command). 我的麻烦之一是缩进(使用=命令)。

See this JS snippet. 请参阅此JS代码段。 This is how Emacs' js2-mode indents it. 这就是Emacs的js2-mode缩进的方式。 I like this : 我喜欢这个 :

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

Now this is what Vim does with it. 现在这就是Vim所做的。 Friggin' hell : 弗里金地狱:

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

All the code above this construction gets indented decently, but from there on it simply sucks. 上面这种结构的所有代码都会缩进,但是从那里开始简直糟透了。 And I have this kind of construction a bit all over my code. 我在代码中有点这种构造。 I've checked :filetype and flipped those switches on. 我检查了:filetype并打开了那些开关。 I've tried with and without plugins. 我尝试了有无插件。 Not getting it right. 不正确。 Vim is gVim 7.4, downloaded a couple of days ago. Vim是gVim 7.4,已在几天前下载。 I've seen this question and the snippet shown there is indented correctly out of the box. 我已经看到了这个问题,并且此处显示的摘录已正确地开箱即用。

Does anybody have a clue what I could attempt here ? 有人知道我在这里可以尝试什么吗? Thanks for any advice ! 感谢您的任何建议!

In your .vimrc : 在您的.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)

To reformat all the file, type gg=G 要重新格式化所有文件,请输入gg=G

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

I've been a VIM user for > 10 years and I'm using this Javascript syntax plugin. 我已经是VIM用户超过10年了,并且正在使用 Javascript语法插件。 I've tested indentation by 我已经测试了缩进

1: creating a new file and setting the filetype 1:创建一个新文件并设置文件类型

:set filetype=javascript

2: Pasting your (malformatted) code 2:粘贴(格式错误的)代码

3: Hitting gg=G (which formats the complete buffer) 3:命中gg = G(格式化整个缓冲区)

The resulting code looks good for me: 结果代码对我来说看起来不错:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Vis中Javascript / Jquery的Rails.js.erb的良好折叠/缩进? - Good folding/indenting for Rails.js.erb for Javascript/Jquery in Vim? 当我使用Three.js更改gltf格式的纹理时,如何将纹理到达正确的位置? - How can I get texture to the right place when I change texture in gltf format with Three.js? 我如何获取 Output 如果我有多个数字要匹配,仅使用 If else 语句并获取 Output 如果它是正确的:- Java 脚本 - How Can i Get Output If i have multiple number to match by using only with If else statement and To Get Output If Its Right :- Java Script 我怎样才能得到正确的响应承诺? - How can I get the right response promise? JS / JQuery:我无法正确执行循环 - JS/JQuery: I Can't get the loop right 我怎样才能在中间有一个文本并且文本是右对齐的 - how can i have a text in the middle and text that is right align 在一个我有text和img标签的容器中,如何只用js或jquery获得文本 - In a container I have text and img tag, how can I get only the text with js or jquery 如何在变量中获取我发送的消息 discord.js - How can I get in a variable the message i have sent discord.js 如何让 Prettier 在 vi​​m 中处理 js 文件? - How to get Prettier working with js files in vim? "<i>How can I save an MSXML2.DomDocument with indenting?<\/i>如何保存带有缩进的 MSXML2.DomDocument?<\/b> <i>(I think it uses MXXMLWriter)<\/i> (我认为它使用 MXXMLWriter)<\/b>" - How can I save an MSXML2.DomDocument with indenting? (I think it uses MXXMLWriter)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM