[英]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.