这实际上是一个相当困难的问题,尽管它得到了 formatlistpat 的很好支持,但没有明显的文档记录。只需将 formatlistpat 和 formatoptions 标识为我想在我的 vimrc 中使用的设置,我就能够支持多种样式的列表。
您可以通过以下方式获得完整的概述:
:help 'formatlistpat'
:help 'formatoptions'
您的工作示例表明您喜欢 vim 转换
以下:
- this is item one that
is indented correctly
- this is item two that
is also indented
correctly
通过在正常模式下输入 gqap 或 gqip,您将获得以下信息:
- this is item one that is indented correctly
- this is item two that is also indented correctly
但是有一个问题是,如果您看到类似以下内容,您无法在 vim 中使用您的配置缩进无序列表:
# - this is item one that
# is not indented correctly
# - this is item two that
# is also not indented
# correctly
# + this is item one that
# is not indented correctly
# + this is item two that
# is also not indented
# correctly
# * this is item one that
# is not indented correctly
# * this is item two that
# is also not indented
# correctly
在这种情况下,gpaq 将错误地格式化为以下内容:
# - this is item one that is not indented correctly - this is item two that
# is also not indented correctly + this is item one that is not
# indented correctly + this is item two that is also not indented
# correctly * this is item one that is not indented correctly * this
# is item two that is also not indented correctly
您可以立即使用以下方法更正此缩进问题:
:set formatoptions+=n
:set formatlistpat=^\\s*[\\-\\+\\*]\\+\\s\\+
这将按照您想要的方式重新格式化您的评论:
# - this is item one that is not indented correctly
# - this is item two that is also not indented correctly
# + this is item one that is not indented correctly
# + this is item two that is also not indented correctly
# * this is item one that is not indented correctly
# * this is item two that is also not indented correctly
您也不想支持其他列表:
# a) this is item one that is not
# indented correctly
# b) this is item two that is also not
# indented correctly
# C. this is item three that is also not
# indented correctly
可以正确格式化:
:set formatlistpat=^\\s*\\w[.\)]\\s\\+
以下内容:
# 1 this is item one that
# is not indented correctly
# 2) this is item two that
# is also not indented
# correctly
# 33. this is item three that
# is also not indented
# correctly
可以正确格式化:
:set formatlistpat=^\\s*\\d\\+[.\)]\\s\\+
以下内容:
# i. this is item one that
# is not indented correctly
# ii. this is item two that
# is also not indented
# correctly
# iv) this is item three that
# is also not indented
# correctly
可以正确格式化:
:set formatlistpat=^\\s*[ivxIVX]\\+[.\)]\\s\\+
你可以用两条简单的语句把它们放在一起:
:set formatoptions+=n
:set formatlistpat=^\\s*\\w\\+[.\)]\\s\\+\\\\|^\\s*[\\-\\+\\*]\\+\\s\\+