【问题标题】:vim indentation: multiple languages in filevim缩进:文件中的多种语言
【发布时间】:2014-06-24 19:42:28
【问题描述】:

假设我有以下文件:temp.html 正确缩进,它看起来像这样:

<html>
    <head>
    </head>
    <body>
        <script type="text/javascript>
            function blahblahblah(...) {
                doSomething();
            }
            function something else() {
                etc...;
            }
        </script>
    </body>
</html>

但是,当我输入 gg=G 时,我会得到如下信息:

<html>
    <head>
    </head>
    <body>
        <script type="text/javascript>
            function blahblahblah(...) {
            doSomething();
            }
            function something else() {
            etc...;
            }
        </script>
    </body>
</html>

我的 javascript 缩进搞砸了。我的 .vimrc 中有 filetype plugin indent on,它似乎适用于我的 HTML。但是,我的代码的 javascript 部分没有正确缩进。如何使用 vim 正确缩进包含多种语言代码的文件?

【问题讨论】:

    标签: vim


    【解决方案1】:

    缩进混合文件类型在 Vim 中永远不会真正起作用,除非您使用更适合该任务的(通常是特定于语言的)外部程序。

    我推荐js-beautify,它可以很好地在 HTML 中格式化 JavaScript。

    这是我用来格式化纯 JS 的命令(放入 ~/.vim/after/ftplugin/javascript.vim):

    command! -buffer -range=% Format let b:winview = winsaveview() |
      \ execute <line1> . "," . <line2> . "!js-beautify -f - -j -B -s " . &shiftwidth |
      \ call winrestview(b:winview)
    

    这里是我使用可选嵌入的 JS 格式化 HTML 的命令(放入 ~/.vim/after/ftplugin/html.vim):

    command! -buffer -range=% Format let b:winview = winsaveview() |
      \ execute <line1> . "," . <line2> . "!html-beautify -f - -I -s " . &shiftwidth |
      \ call winrestview(b:winview)
    

    这些命令的工作方式相同:

    :Format      " formats the whole buffer
    :5,23Format  " formats lines 5 to 23
    :'<,'>Format " formats the visually selected lines
    

    【讨论】:

      猜你喜欢
      • 2014-02-19
      • 2018-10-29
      • 1970-01-01
      • 2011-12-01
      • 2011-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多