vim:在注释中缩进项目符号/列表(在Python中)

Pon*_*ars 1 vim auto-indent

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

- this is item one that 
  is indented correctly
- this is item two that 
  is also indented 
  correctly
Run Code Online (Sandbox Code Playgroud)

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

然而,这在 python 注释中不起作用,并且会像这样缩进:

# - this is item one that 
# is not indented correctly
# - this is item two that 
# is also not indented 
# correctly
Run Code Online (Sandbox Code Playgroud)

如果我gqap在上面的 python 注释中输入,vim 甚至无法识别项目符号点。并最终将整个块格式化为段落。

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

jar*_*raj 5

这实际上是一个相当困难的问题,尽管 formatlistpat 很好地支持它,但没有明显记录。只需将 formatlistpat 和 formatoptions 识别为我想要在 vimrc 中使用的设置,我就能够支持多种样式的列表。

您可以通过以下内容获得完整的概述:

:help 'formatlistpat'
:help 'formatoptions'
Run Code Online (Sandbox Code Playgroud)

您的工作示例表明您喜欢 vim 转换以下内容的方式:

- this is item one that 
is indented correctly
- this is item two that 
is also indented 
correctly
Run Code Online (Sandbox Code Playgroud)

通过在正常模式下输入 gqap 或 gqip,您将获得以下信息:

- this is item one that is indented correctly
- this is item two that is also indented correctly
Run Code Online (Sandbox Code Playgroud)

但是存在一个问题,如果出现类似以下内容,则无法使用您的配置在 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
Run Code Online (Sandbox Code Playgroud)

在这种情况下,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
Run Code Online (Sandbox Code Playgroud)

您可以使用以下命令立即更正此缩进问题:

:set formatoptions+=n
:set formatlistpat=^\\s*[\\-\\+\\*]\\+\\s\\+
Run Code Online (Sandbox Code Playgroud)

这将以您想要的方式重新格式化您的评论:

# - 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
Run Code Online (Sandbox Code Playgroud)

您还想支持其他一些列表:

# 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
Run Code Online (Sandbox Code Playgroud)

可以正确格式化:

:set formatlistpat=^\\s*\\w[.\)]\\s\\+
Run Code Online (Sandbox Code Playgroud)

下列:

# 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
Run Code Online (Sandbox Code Playgroud)

可以正确格式化:

:set formatlistpat=^\\s*\\d\\+[.\)]\\s\\+
Run Code Online (Sandbox Code Playgroud)

下列:

# 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
Run Code Online (Sandbox Code Playgroud)

可以正确格式化:

:set formatlistpat=^\\s*[ivxIVX]\\+[.\)]\\s\\+
Run Code Online (Sandbox Code Playgroud)

您可以用两行简单的代码将它们组合在一起:

:set formatoptions+=n
:set formatlistpat=^\\s*\\w\\+[.\)]\\s\\+\\\\|^\\s*[\\-\\+\\*]\\+\\s\\+
Run Code Online (Sandbox Code Playgroud)