【问题标题】:Correctly indent javascript inside script tags on VIM在 VIM 上的脚本标签内正确缩进 javascript
【发布时间】:2012-06-22 01:36:28
【问题描述】:

我需要帮助让 VIM 正确缩进脚本标签内的 javascript。

这就是我想要的样子:

  <script type="text/javascript">
    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-257015-24']);
    _gaq.push(['_trackPageview']);

    (function() {
      var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
      ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
      var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
    })();
  </script>

这是当我让 VIM 缩进时的样子,这很不错,但不是最优的:

  <script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-257015-24']);
_gaq.push(['_trackPageview']);

(function() {
  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
  </script>

我的 VIM 文件大致是这些:http://github.com/lunks/vimfiles

我已经尝试将脚本附加到我想要在我的vimrc 上缩进的标签:

let g:html_indent_inctags = "body,head,tbody,ul,li,p,script"

但是,虽然它成功缩进了script 标记之后的内容,但它不再将其内容缩进为javascript,而只是文本。

我怎样才能让 VIM 在脚本标签内缩进 Javascript,就像我想要的那样?

【问题讨论】:

    标签: javascript vim indentation


    【解决方案1】:

    尝试:set ft=html.javascript 或在 vim.org 上为 HTML/JS 搜索更好的缩进文件。

    【讨论】:

      【解决方案2】:

      这个做到了:https://github.com/vim-scripts/indenthtml.vim

      确保添加到您的 vimrc:

      let g:html_indent_script1 = "inc"
      let g:html_indent_style1 = "inc"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-18
        • 1970-01-01
        • 2010-11-30
        • 2022-01-14
        相关资源
        最近更新 更多