VIM 中的 Javascript 缩进
我正在尝试让 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 的建议。
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();
}
The result is the rather absurd:
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();
}
I've set set syntax=javascript
, and I've set filetype
to:
filetype detection:ON plugin:ON indent:ON
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
.
I've installed javascript.vim
, and IndentAnything
, though they don't seem to have any effect.
I'd be very grateful for any suggestions as to how to get Vim indenting properly in JavaScript.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
天哪,我刚刚花了几个小时解决同样的问题。
如果您启用了
文件类型缩进
(您确实这么做了),那么某个文件可能会在某处设置一些不同的缩进设置。如果您使用 verbose set默认情况下,您只希望看到
cindent
设置为默认缩进文件:其中
$VIMRUNTIME
是运行:echo $VIMRUNTIME
时获得的路径。除非您启用它们(在您的 vimrc 或插件中),否则所有其他内容都不会被设置。
对我来说,我有一个插件(eclim)正在设置
identexpr
并导致此问题:Oh man, I just spent a couple of hours figuring out the same problem.
If you have
filetype indent on
(which you do), then a few different indent settings may be set by a file somewhere. If you useverbose set <option>?
you can see where it's being set and what it's set to:By default, you'd only expect to see
cindent
set by the default indent file:Where
$VIMRUNTIME
is the path you get when you run:echo $VIMRUNTIME
.All of the others wouldn't be set unless you enable them (in your vimrc or a plugin).
For me, I had a plugin (eclim) that was setting
identexpr
and causing this issue:我前段时间遇到了同样的问题,解决方案是插件“vim-javascript”。它是 vim 的 JavaScript 包,提供语法和缩进插件。
https://github.com/pangloss/vim-javascript
安装非常简单。
如果您使用 pathogen,请使用以下步骤:
如果您使用 vundle 使用以下步骤:
将以下行添加到您的 vimrc 文件中:
并安装它:
I had the same problem some time ago and the solution was the plugin "vim-javascript". It is JavaScript bundle for vim providing syntax and indent plugins.
https://github.com/pangloss/vim-javascript
The installation is very simple.
If you are using pathogen, use the follow steps:
If you are using vundle use the follow steps:
Add the follow line to your vimrc file:
And install it:
添加两个右大括号并使用
vi{
选择整个块,在没有插件的 gvim 7.2 中为我提供了正确的自动缩进。您可能想通过在命令行上使用--noplugins
标志启动 vim 来查看是否有错误的插件弄乱了它。然后再试一次。Adding the two closing braces and selecting the entire block with
vi{
provided proper automatic indentation for me in gvim 7.2 with no plugins. You may want to see if an errant plugin is messing it up by starting vim with the--noplugins
flag on the command line. and try again.我讨厌说“这对我有用”这样无益的话,但确实如此。即使我的 .vimrc 中没有任何内容并且所有插件都关闭,我也得到了正确的缩进。
您是否尝试过使用
--noplugins
开关加载 vim 并暂时移动/重命名您的 .vimrc 以查看它是否仍然不起作用?我怀疑您的 .vimrc 或其他插件中的另一个设置可能会导致冲突。I hate to say something unhelpful like "It works for me", but it does. Even with nothing in my .vimrc and all plugins off, I get the correct indentation.
Have you tried loading vim with the
--noplugins
switch and temporarily moving/renaming your .vimrc to see if it still doesn't work? I suspect another setting in your .vimrc or another plugin may be causing the conflict.对我来说它有效(这不是很有帮助的陈述,我知道;-))。
我认为文件类型未正确检测。
说什么
?它应该报告“javascript”。
[编辑]
提示:请注意,有一个名为“filetype”的选项和一个名为:filetype 的命令。
要获取选项 do :help 'filetype' 的帮助,请执行命令 do :help :filetype。
For me it works (not very helpful statement, I know ;-) ).
I suppose that the filetype is not detected correctly.
What does
say? It should report "javascript".
[EDIT]
Hint: Please note that there is an option called 'filetype' and a command called :filetype.
To get help for the option do :help 'filetype' for the command do :help :filetype.
前几天,我在使用 MacVim 7.2 和 Lua 文件时遇到了问题,该文件无法正确缩进 - 即使在使用
set syntax
、set filetype
和filetype indent 后也是如此上
,它没有正确缩进文件。我发现添加:
到我的 .gvimrc 文件解决了这个问题,至少对我来说是这样。 YMMV。
I was having problems the other day with MacVim 7.2 and a Lua file that wouldn't indent correctly -- even after using
set syntax
,set filetype
andfiletype indent on
, it wasn't indenting the file correctly.I discovered adding:
to my
.gvimrc
file solved the issue, at least for me. YMMV.如果你想缩进而不使用任何插件。
gg=G
作用是:
gg
将光标移动到文件顶部,=
会触发自动缩进,G
会将光标移动到文件末尾。If you want to indent without using any plugin.
gg=G
What is does is:
gg
moves the cursor to the top of the file,=
will trigger auto-indentation,G
will move the cursor to the end of the file.对我来说,同一行上“{”字符之前存在多个“(”字符似乎改变了“=”使用的缩进算法。删除其中一个“(”似乎可以修复所有部分的“=”文件的其他范围区域 '{...}' 除外,它的开头行上也有多个 '('。
我在 lubuntu 中使用 vim 7.4
For me the presence of more than one '(' character before the '{' character on the same line seemed to change the indentation algorithm used by '='. Removing one of the '(' seemed to fix '=' for all parts of the file except other scope regions '{...}' which also had multiple '(' on the opening line.
I'm using vim 7.4 in lubuntu