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

时间:2016-05-10 21:07:39

标签: 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 item two that 
# is also not indented 
# correctly

如果我在上面的python评论中键入gqap,vim甚至不会识别项目符号。并最终将整个块格式化为paragaph。

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

1 个答案:

答案 0 :(得分:2)

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