如果我有一些像这样的长行
# 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.
我想选择行并发出命令,使其换行到文本宽度,同时保留注释符号(标签(以获得以下
# 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
文件,在谷歌上找不到解决方案
您可以使用gq
命令让Vim格式化行。直观地选择行,然后使用gq
,或者使用gq
后跟一个动作(例如gq}
,格式化到下一行(,或者使用带计数的gqq
(3gqq
,格式化当前行和后面的两行(。
Vim要保留注释,需要正确设置两个选项。一个是'formatoptions'
,它需要包括q
(在gq
操作上保留注释(,以及'comments'
选项,它需要包含b:#
,以指示以#
开头的行是注释。
您可以使用为当前缓冲区设置两者
setl fo+=q com+=b:#
在这些设置之后,使用gq
将在自动格式化注释块时将注释字符保持在行的开头。