VIM 中的 Javascript 缩进

发布于 2024-09-09 04:01:08 字数 1534 浏览 6 评论 0原文

我正在尝试让 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

尽管我已经尝试了这一切的排列。我已经尝试了 smartindentautoindentcindent 的每种排列,但似乎没有任何一种方法能够给 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

﹂绝世的画 2024-09-16 04:01:08

天哪,我刚刚花了几个小时解决同样的问题。

如果您启用了文件类型缩进(您确实这么做了),那么某个文件可能会在某处设置一些不同的缩进设置。如果您使用 verbose set

: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

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 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?

By default, you'd only expect to see cindent set by the default indent file:

cindent
Last set from $VIMRUNTIME/indent/javascript.vim

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:

identexpr=EclimGetJavascriptIndent(V:lnum)
Last set from ~/.vim/bundle/eclim/indent/javascript.vim
绮烟 2024-09-16 04:01:08

我前段时间遇到了同样的问题,解决方案是插件“vim-javascript”。它是 vim 的 JavaScript 包,提供语法和缩进插件。

https://github.com/pangloss/vim-javascript

安装非常简单。

如果您使用 pathogen,请使用以下步骤:

  cd ~/.vim/bundle
  git clone https://github.com/pangloss/vim-javascript.git

如果您使用 vundle 使用以下步骤:

将以下行添加到您的 vimrc 文件中:

Plugin "pangloss/vim-javascript"

并安装它:

:so ~/.vimrc
:PluginInstall

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:

  cd ~/.vim/bundle
  git clone https://github.com/pangloss/vim-javascript.git

If you are using vundle use the follow steps:

Add the follow line to your vimrc file:

Plugin "pangloss/vim-javascript"

And install it:

:so ~/.vimrc
:PluginInstall
影子的影子 2024-09-16 04:01:08

添加两个右大括号并使用 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.

原谅我要高飞 2024-09-16 04:01:08

我讨厌说“这对我有用”这样无益的话,但确实如此。即使我的 .vimrc 中没有任何内容并且所有插件都关闭,我也得到了正确的缩进。

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();
        }

您是否尝试过使用 --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.

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();
        }

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.

旧城烟雨 2024-09-16 04:01:08

对我来说它有效(这不是很有帮助的陈述,我知道;-))。
我认为文件类型未正确检测。

说什么

 :set filetype

?它应该报告“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

 :set filetype

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.

戏蝶舞 2024-09-16 04:01:08

前几天,我在使用 MacVim 7.2 和 Lua 文件时遇到了问题,该文件无法正确缩进 - 即使在使用 set syntaxset filetypefiletype indent 后也是如此上,它没有正确缩进文件。

我发现添加:

filetype plugin indent on

到我的 .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 and filetype indent on, it wasn't indenting the file correctly.

I discovered adding:

filetype plugin indent on

to my .gvimrc file solved the issue, at least for me. YMMV.

空城旧梦 2024-09-16 04:01:08

如果你想缩进而不使用任何插件。

  • 按 ESC 键切换到正常。
  • 输入gg=G

作用是:gg将光标移动到文件顶部,=会触发自动缩进,G 会将光标移动到文件末尾。

If you want to indent without using any plugin.

  • Switch to Normal by hitting ESC key.
  • Type 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.

糖粟与秋泊 2024-09-16 04:01:08

对我来说,同一行上“{”字符之前存在多个“(”字符似乎改变了“=”使用的缩进算法。删除其中一个“(”似乎可以修复所有部分的“=”文件的其他范围区域 '{...}' 除外,它的开头行上也有多个 '('。

我在 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文