Vim为什么将缩进对齐到括号/冒号?

时间:2020-08-11 17:07:21

标签: vim

我在Vim中遇到了一个不寻常的缩进问题,我想更好地理解。下面的行描述了我输入的内容:

writing some words 
  manually indenting this line 
  this line got automatically indented 
  here's a line with a colon: some more words 
  this line is indented as expected 
  now I introduce (parentheses) and nothing happens 
  indented as expected
  here it comes (parentheses and a colon): let the magic begin
                                           why do parentheses+colon indent me to here?

为什么这样做,如何停止? :)我总体上喜欢cindent,所以我宁愿将其保留为autoindent / smartindent。

我的Vim设置为:

  • 空〜/ .vimrc,除了:set cindent expandtab

  • 已禁用所有插件

  • :verbose set ft? ai? si? cindent? indentkeys? cinkeys? cinoptions? indentexpr? formatoptions?

      filetype=
    noautoindent
    nosmartindent
      cindent
      indentkeys=0{,0},0),0],:,0#,!^F,o,O,e
      cinkeys=0{,0},0),0],:,0#,!^F,o,O,e
      cinoptions=
      indentexpr=
      formatoptions=tcq
            Last set from /usr/share/vim/vim81/debian.vim line 3
    

1 个答案:

答案 0 :(得分:0)

如Matt所言,您可能不希望将cindent用于非C或类似C的语言,因为它具有许多有关C语法的硬编码知识,通常不适用文本,甚至其他编程语言(例如Python或Ruby)。

您可以执行以下操作:

" For general usage.
set autoindent

autocmd FileType c  setl cindent noautoindent

对于非C语言,它将使用autoindent,对于C语言,将使用cindent。如果使用其他语言,则可以为cppjava添加附加节等等。您还可以使用这种类型的autocmd语句来执行诸如调整缩进级别(通过tsstssw)或在制表符和空格之间切换的操作,具体取决于您所使用语言的规范(例如Ruby的2个空格或Go的8个空格标签)。

对于文本,您可能还需要调整formatlistpat以调整列表标题的构成,当启用自动缩进时,列表标题确定是否应缩进下一行以继续列表。这可能是这里发生的事情的一部分。我为该选项使用的值为^[.-*+•]\\+\\s\\+,不应使冒号被考虑。