Vim:重新格式化和重新评论的自动化方式?

Eri*_* Hu 4 vim formatting

我在代码中有一些文档,我想要很好地格式化:

# book_id - integer
# chapter_id - integer (Optional)
# relative_url - Text: the url of the screencast file on S3, relative to the book's url
# view_count - integer
Run Code Online (Sandbox Code Playgroud)

我安装了Tabular.vim,这让我走得很远:

# book_id      - integer
# chapter_id   - integer (Optional)
# relative_url - Text: the url of the screencast file on S3, relative to the book's url
# view_count   - integer
Run Code Online (Sandbox Code Playgroud)

我想要一些自动生成这样的代码的方法.也就是说,包裹到79个字符,如果从前一个注释继续一行,则缩进.我得到的是这个:

# book_id      - integer
# chapter_id   - integer (Optional)
# relative_url - Text: the url of the screencast file on S3, relative to the
# book's url
# view_count   - integer
Run Code Online (Sandbox Code Playgroud)

我正在寻找的是:

# book_id      - integer
# chapter_id   - integer (Optional)
# relative_url - Text: the url of the screencast file on S3, relative to the
#                book's url
# view_count   - integer
Run Code Online (Sandbox Code Playgroud)

是否可以在现有的插件或热键序列中执行此操作?我知道gq,它会将文本重新格式化为在vim中设置的字符宽度,但它不会在注释等内容中添加缩进.

kev*_*kev 5

您可以使用该formatlistpat选项(:set fo+=n需要工作):

删除 #

:%s/^# //
Run Code Online (Sandbox Code Playgroud)

2.设置flp选项

:setl flp=^[^-]*-\\s
Run Code Online (Sandbox Code Playgroud)

3.做格式化

gggqG

前置 #

:%s/^/# /
Run Code Online (Sandbox Code Playgroud)

您还可以使用以下indentexpr选项:

2A.设置inde选项

:setl inde=15
Run Code Online (Sandbox Code Playgroud)

2B.插入空行

:g/^/pu_
Run Code Online (Sandbox Code Playgroud)

5.删除空注释

:g/^# $/d
Run Code Online (Sandbox Code Playgroud)