代码之家  ›  专栏  ›  技术社区  ›  Dave Sherohman

缩进注释以匹配vim中的代码

vim
  •  46
  • Dave Sherohman  · 技术社区  · 18 年前

    我在vim中完成了所有的编码,对此我感到非常满意(所以,请不要回答“使用不同的编辑器”),但我一直很恼火,因为smartinent功能根本不想缩进以#开头的注释。例如,我想要

      # Do something
      $x = $x + 1;
      if ($y) {
        # Do something else
        $y = $y + $z;
      }
    

    而不是vim的首选

    # Do something
      $x = $x + 1;
      if ($y) {
    # Do something else
        $y = $y + $z;
      }
    

    我唯一能够防止评论被发送到行首的方法是在点击#之前插入和删除行上的字符(每次都必须记住这是一种麻烦),或者完全关闭smartinent(在打开/关闭大括号时失去自动缩进增加/减少)。

    我如何设置vim来保持注释的缩进,而不是将它们发送到行首?

    4 回复  |  直到 18 年前
        1
  •  46
  •   Alan W. Smith vishes_shell    9 年前

    看起来你在用Perl编码。确保在.vimrc中设置了以下内容:

    filetype plugin indent on
    syntax enable
    

    这些将告诉Vim在打开缓冲区时设置文件类型,并配置缩进和语法突出显示。无需显式设置smartinent,因为Vim附带的Perl语法文件会自动设置它(以及任何其他特定于Perl的自定义)。


    注:有 set smartindent 和 set autoindent 在……里面 ~/.vimrc 可能会阻止溶液发挥作用。如果你有问题,去找他们。

        2
  •  18
  •   Russell Silva    16 年前

    如果您使用的是“smartrindent”缩进选项,则“:help smartrindend”VIM文档中解释了解决问题的方法:

        When typing '#' as the first character in a new line, the indent for
        that line is removed, the '#' is put in the first column.  The indent
        is restored for the next line.  If you don't want this, use this
        mapping: ":inoremap # X^H#", where ^H is entered with CTRL-V CTRL-H.
        When using the ">>" command, lines starting with '#' are not shifted
        right.
    

    我使用“smartinent”,可以确认所描述的修复程序对我有效。它通过将“#”的按键替换为键入“X”,然后按退格键,然后再次键入“#”来欺骗VIM。您可以自己手动尝试,看看它不会触发自动退出。

        3
  •  10
  •   Ben Hoffstein    18 年前

    这个问题可以通过在_vimrc文件中放入以下内容来解决。

    set cindent
    set cinkeys=0{,0},!^F,o,O,e " default is: 0{,0},0),:,0#,!^F,o,O,e
    

    More info ...

        4
  •  7
  •   Paul Tomblin    18 年前

    我认为“smartinent”是为C设计的,所以它认为“#”是预处理器指令的开头,而不是注释。我不知道它的解决方案,除非你键入空格,然后键入退格,然后键入“#”,否则它不会这样做。