我正在尝试让VIM使用'='和相关命令来缩进Javascript。例如,当我尝试自动缩进以下代码时:
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
设置为:filetype detection:ON plugin:ON indent:ON
尽管我已经尝试了所有这种排列方式。我已经尝试过
smartindent
,autoindent
和cindent
的每个排列,但是似乎没有什么能给Vim带来预期的缩进的正确效果。我已经设置了tabstop=4
。我已经安装了
javascript.vim
和 IndentAnything
,尽管它们似乎没有任何作用。对于如何在JavaScript中正确使Vim缩进的任何建议,我将不胜感激。
最佳答案
噢,老兄,我花了几个小时才弄清楚同样的问题。
如果您具有filetype indent on
(您可以这样做),那么文件的某处可能会设置一些不同的缩进设置。如果您使用verbose set <option>?
,则可以看到它的设置位置和设置:
:verbose set autoindent?
:verbose set cindent?
:verbose set smartindent?
:verbose set indentexpr?
默认情况下,您只希望看到默认缩进文件设置的
cindent
:cindent
Last set from $VIMRUNTIME/indent/javascript.vim
其中
$VIMRUNTIME
是运行:echo $VIMRUNTIME
时获得的路径。除非您启用了其他所有选项(在vimrc或插件中),否则将不会设置所有其他选项。
对我来说,我有一个插件(eclim),它正在设置
identexpr
并导致此问题:identexpr=EclimGetJavascriptIndent(V:lnum)
Last set from ~/.vim/bundle/eclim/indent/javascript.vim