可视化 select 并在 VIM 中分行至文本宽度

Visual select and break multiple lines in VIM up to textwidth

如果我有几个像这样的长行

# I am Quater. Read my words, and be my friend.
# Father commands me to record the truth of history, so that readers will learn from those who went before.
# Therefore, I give each of my seven sons one of these self-engraving, history-recording klay walls.

我想 select 行并发出命令并使其换行至文本宽度,同时保留注释符号(井号标签)以获得以下内容

# I am Quater. Read my words, and be my friend.
# Father commands me to record the truth of 
# history, so that readers will learn from 
# those who went before. Therefore, I give each 
# of my seven sons one of these self-engraving, 
# history-recording klay walls.

我知道有一种方法可以做到这一点,因为我以前在 VIM 做过,但是我删除了我的 .vimrc 文件,我在 [=21= 的任何地方都找不到解决方案]

您可以 Vim 使用 gq 命令格式化行。 select 视觉上的行然后使用 gq,或使用 gq 后跟一个动作(例如 gq} 格式化直到下一行),或使用 gqq 和计数(3gqq 格式化当前行及其后两行)。

为了Vim保留评论,需要正确设置两个选项。一个是 'formatoptions',它需要包括 q(以保留对 gq 操作的注释),以及 'comments' 选项,它需要包括 b:# 以表示以 # 开头的行是注释。

您可以使用以下方法为当前缓冲区设置两者:

setl fo+=q com+=b:#

在这些设置之后,使用 gq 将在自动格式化评论块时将评论字符保留在行的开头。