vim:在注释中缩进 bullet/lists(在 python 中)

vim: indenting bullet/lists within comments (in python)

vim 文本文件中的缩进列表(或项目符号)很好:

- this is item one that 
  is indented correctly
- this is item two that 
  is also indented 
  correctly

我可以在上面的段落中键入 gqap,并且格式和缩进工作正常。

但是,这在 python 评论中不起作用,并且缩进如下:

# - this is item one that 
# is not indented correctly
# - this is item two that 
# is also not indented 
# correctly

如果我在上面的 python 评论中键入 gqap,vim 甚至无法识别要点。并最终将整个块格式化为段落。

那么关于项目符号和缩进,我如何让 vim 在 python 注释中的行为方式与在常规文本文件中的行为方式相同?

这实际上是一个非常困难的问题,尽管 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\+