首页 > 解决方案 > 为什么 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 设置是:

标签: vim

解决方案


正如马特所提到的,您可能不想使用cindent不是 C 或类 C 语言的东西,因为它有很多关于 C 语法的硬编码知识,这些知识通常不适用于文本,甚至不适用于其他编程像 Python 或 Ruby 这样的语言。

你可以做的是这样的:

" For general usage.
set autoindent

autocmd FileType c  setl cindent noautoindent

这将autoindent用于不是 C 的东西,而是cindent用于 C。如果您使用其他语言,您可以为cppjava等添加额外的节。您还可以使用这种类型的autocmd语句来执行诸如调整缩进级别之类的事情(通过tsstssw) 或在制表符和空格之间切换,具体取决于您使用的语言规范(例如,Ruby 的 2 个空格或 Go 的 8 个空格制表符)。

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


推荐阅读