vim 中的格式注释以 #'(井号-破折号)开头

Format comments in vim starting with #' (hash-dash)

使用 vim,我使用 gw 或 gwap 自动格式化 python 或 R 中的长注释,并且 vim 在行首自动插入#符号需要。在 python 和 R 中,自动文档生成的注释都以 #' 而不是 # 开头,并且 vim 不知道如何处理这些。

如何教 vim 带 #' 的行也是注释,以及如何将行格式化为块以在每行的开头添加 #' 符号?

最小示例:

#' Some random text. Please
#' format me 
#' nicely by
#' typing gwap anywhere in this paragraph.

应该变成这样:

#' Some random text. Please format me 
#' nicely by typing gwap anywhere in
#' this paragraph.

您需要扩展 comments option:

'comments' 'com'        string  (default
                                "s1:/*,mb:*,ex:*/,://,b:#,:%,:XCOMM,n:>,fb:-")
                        local to buffer
                        {not in Vi}
                        {not available when compiled without the |+comments|
                        feature}
        A comma separated list of strings that can start a comment line.  See
        |format-comments|.  See |option-backslash| about using backslashes to
        insert a space.

格式化使用此选项来识别评论领袖。每个以逗号分隔的条目都采用 {flags}:{string} 形式,其中标志控制使用何种类型的注释格式。

b:#' 添加到该列表以使文本格式插入匹配的评论前导,b 代表 需要空白,因此只有 #'后跟一些空格,然后是文本,被视为评论领导者:

:set comments+=b:#'

有关详细信息,请参阅 :help format-comments

演示:

考虑到 filetype plugin can override the comments option again; you can add the same setting as an override (with a :setlocal command) 在各自的 ~/.vim/ftplugin/<type>.vim 文件中(如果尚不存在则创建它)。

所以对于一个 Python 缓冲区,如果有一个 ~/.vim/ftplugin/python.vim 文件,它将是 运行 在通用 $VIMRUNTIME/ftplugin/python.vim 文件被执行之后让你设置额外的此文件类型的配置,包括额外的注释配置。