VIM中的Javascript缩进 [英] Javascript indentation in VIM
问题描述
我正在尝试让VIM使用'='和相关命令来缩进Javascript.例如,当我尝试自动缩进以下代码时:
I'm trying to get VIM to indent Javascript with the '=' and related commands. When I try to auto indent the following, for example:
new function($) {
$.fn.setCursorPosition = function(pos) {
if ($(this).setSelectionRange) {
$(this).setSelectionRange(pos, pos);
} else if ($(this).createTextRange) {
var range = $(this).createTextRange();
range.collapse(true);
range.moveEnd('character', pos);
range.moveStart('character', pos);
range.select();
}
结果很荒谬:
new function($) {
$.fn.setCursorPosition = function(pos) {
if ($(this).setSelectionRange) {
$(this).setSelectionRange(pos, pos);
} else if ($(this).createTextRange) {
var range = $(this).createTextRange();
range.collapse(true);
range.moveEnd('character', pos);
range.moveStart('character', pos);
range.select();
}
我已经设置了set syntax=javascript
,并且已经将filetype
设置为:
I've set set syntax=javascript
, and I've set filetype
to:
filetype detection:ON plugin:ON indent:ON
尽管我尝试了所有这种排列方式.我已经尝试了smartindent
,autoindent
和cindent
的每个排列,但是似乎没有什么能给Vim带来预期的缩进的正确效果.我设置了tabstop=4
.
Though I've tried every permutation of this. I've tried every permutation of smartindent
, autoindent
, and cindent
, but nothing seems to have the correct effect of giving Vim the expected indentation. I've set tabstop=4
.
我已经安装了 javascript.vim
和 IndentAnything
,尽管它们似乎没有任何作用.
I've installed javascript.vim
, and IndentAnything
, though they don't seem to have any effect.
对于如何使Vim在JavaScript中正确缩进的任何建议,我将不胜感激.
I'd be very grateful for any suggestions as to how to get Vim indenting properly in JavaScript.
推荐答案
哦,我花了几个小时才弄清楚同样的问题.
Oh man, I just spent a couple of hours figuring out the same problem.
如果您有filetype indent on
(您需要这样做),那么文件的某处可能会设置一些不同的缩进设置.如果使用verbose set <option>?
,则可以看到它的设置位置和设置内容:
If you have filetype indent on
(which you do), then a few different indent settings may be set by a file somewhere. If you use verbose set <option>?
you can see where it's being set and what it's set to:
:verbose set autoindent?
:verbose set cindent?
:verbose set smartindent?
:verbose set indentexpr?
默认情况下,您只希望看到默认缩进文件设置的cindent
:
By default, you'd only expect to see cindent
set by the default indent file:
cindent
Last set from $VIMRUNTIME/indent/javascript.vim
$VIMRUNTIME
是运行:echo $VIMRUNTIME
时获得的路径.
Where $VIMRUNTIME
is the path you get when you run :echo $VIMRUNTIME
.
除非您在vimrc或插件中启用了其他所有选项,否则其他所有选项都不会被设置.
All of the others wouldn't be set unless you enable them (in your vimrc or a plugin).
对我来说,我有一个设置为identexpr
并导致此问题的插件(eclim):
For me, I had a plugin (eclim) that was setting identexpr
and causing this issue:
identexpr=EclimGetJavascriptIndent(V:lnum)
Last set from ~/.vim/bundle/eclim/indent/javascript.vim
这篇关于VIM中的Javascript缩进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!