【问题标题】:vim: indenting bullet/lists within comments (in python)vim:在评论中缩进项目符号/列表(在python中)
【发布时间】:2016-05-10 21:07:39
【问题描述】:

vim 可以在文本文件中缩进列表(或项目符号):

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

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

但是,这在 python cmets 中不起作用,并且会像这样缩进:

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

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

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

【问题讨论】:

    标签: vim auto-indent


    【解决方案1】:

    这实际上是一个相当困难的问题,尽管它得到了 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\\+
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-04
      相关资源
      最近更新 更多