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

vim:在注释中缩进项目符号/列表(python),vim,auto-indent,Vim,Auto Indent,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

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'
- this is item one that 
is indented correctly
- this is item two that 
is also indented 
correctly
- this is item one that is indented correctly
- this is item two that is also 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
# * 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 * this is item one that is not indented correctly * this 
# is item two that is also not indented  correctly
# 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
# 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
您的工作示例表明,您喜欢vim转换 以下:

:help 'formatlistpat'
:help 'formatoptions'
- this is item one that 
is indented correctly
- this is item two that 
is also indented 
correctly
- this is item one that is indented correctly
- this is item two that is also 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
# * 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 * this is item one that is not indented correctly * this 
# is item two that is also not indented  correctly
# 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
# 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
通过在正常模式下输入gqap或gqip,您可以获得以下信息:

:help 'formatlistpat'
:help 'formatoptions'
- this is item one that 
is indented correctly
- this is item two that 
is also indented 
correctly
- this is item one that is indented correctly
- this is item two that is also 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
# * 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 * this is item one that is not indented correctly * this 
# is item two that is also not indented  correctly
# 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
# 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
但有一个问题是,如果出现以下情况,则无法使用配置缩进vim中的无序列表:

:help 'formatlistpat'
:help 'formatoptions'
- this is item one that 
is indented correctly
- this is item two that 
is also indented 
correctly
- this is item one that is indented correctly
- this is item two that is also 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
# * 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 * this is item one that is not indented correctly * this 
# is item two that is also not indented  correctly
# 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
# 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
在这种情况下,gpaq将不正确地格式化为以下格式:

:help 'formatlistpat'
:help 'formatoptions'
- this is item one that 
is indented correctly
- this is item two that 
is also indented 
correctly
- this is item one that is indented correctly
- this is item two that is also 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
# * 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 * this is item one that is not indented correctly * this 
# is item two that is also not indented  correctly
# 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
# 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 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\\+
:set formatlistpat=^\\s*\\d\\+[.\)]\\s\\+
:set formatlistpat=^\\s*[ivxIVX]\\+[.\)]\\s\\+
以下是:

:help 'formatlistpat'
:help 'formatoptions'
- this is item one that 
is indented correctly
- this is item two that 
is also indented 
correctly
- this is item one that is indented correctly
- this is item two that is also 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
# * 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 * this is item one that is not indented correctly * this 
# is item two that is also not indented  correctly
# 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
# 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*\\w[.\)]\\s\\+
:set formatlistpat=^\\s*\\d\\+[.\)]\\s\\+
:set formatlistpat=^\\s*[ivxIVX]\\+[.\)]\\s\\+
以下是:

:help 'formatlistpat'
:help 'formatoptions'
- this is item one that 
is indented correctly
- this is item two that 
is also indented 
correctly
- this is item one that is indented correctly
- this is item two that is also 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
# * 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 * this is item one that is not indented correctly * this 
# is item two that is also not indented  correctly
# 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
# 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*\\w[.\)]\\s\\+
:set formatlistpat=^\\s*\\d\\+[.\)]\\s\\+
:set formatlistpat=^\\s*[ivxIVX]\\+[.\)]\\s\\+
您可以用两条简单的线将这些放在一起:

:set formatoptions+=n
:set formatlistpat=^\\s*\\w\\+[.\)]\\s\\+\\\\|^\\s*[\\-\\+\\*]\\+\\s\\+