在 vim 中使用代码和注释格式化行
使用以下设置
set textwidth=40
set fo? -> formatoptions=croql)
set comments? -> comments=sO:* -,mO:* ,exO:*/,s1:/*,mb:*,ex:*/,://
set cindent
set cinoptions ->cinoptions=
如果我进入插入模式并开始键入以下行
// abc abc abc abc abc abc abc abc
,则按预期在第 40 个字符处换行:
// abc abc abc abc abc abc abc
// abc
此外,如果将长度超过 40 个字符的行复制并粘贴为
// abc abc abc abc abc abc abc abc
命令 gqq
输入它最终得到与上面相同的结果。
但我注意到,如果我进入插入模式并键入下面的行,
void funAbc(void) { // abc abc abc abc abc abc abc abc
则不会断行。如果输入命令 gqq
,
void funAbc(void) { // abc abc
abc abc abc abc abc abc
我预计插入长度超过 40 个字符的行或发出 gqq
都会破坏注释并在下一个插入“//”行 - 与此类似的内容:
void funAbc(void) { // abc abc
// abc abc abc abc abc abc
我尝试设置 'smartindent'
但它没有解决问题。
是否有任何选项/技巧可以更改代码之后开始的这些注释的行为?
Using the following settings
set textwidth=40
set fo? -> formatoptions=croql)
set comments? -> comments=sO:* -,mO:* ,exO:*/,s1:/*,mb:*,ex:*/,://
set cindent
set cinoptions ->cinoptions=
If I enter insert mode and start typing the following line
// abc abc abc abc abc abc abc abc
the line breaks at the 40th character, as expected:
// abc abc abc abc abc abc abc
// abc
Also, if a line longer than 40 characters is copied and pasted as
// abc abc abc abc abc abc abc abc
and command gqq
is typed it ends up with the same result as above.
But I've noticed that if I enter insert mode and type the line below
void funAbc(void) { // abc abc abc abc abc abc abc abc
it doesn't break the line. If command gqq
is entered it results in
void funAbc(void) { // abc abc
abc abc abc abc abc abc
I've expected that both inserting line longer than 40 characters or issuing gqq
would break the comments and insert "//" in the next line - something similar to this:
void funAbc(void) { // abc abc
// abc abc abc abc abc abc
I've tried setting 'smartindent'
but it didn't solved the issue.
Is there any option/trick that can change the behavior of these comments starting after code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不认为是这样。使用“comments”和“formatoptions”进行注释格式化仅识别注释字符串何时位于行首。从行中间开始的注释只能被语法荧光笔识别,不能被格式化程序识别。因此,我认为在 Vim 中这是不可能的。您也许可以找到一个插件来完成此操作,但在 Vim 网站上粗略搜索并没有找到任何有用的东西。
I don't think it is. The comment formatting with 'comments' and 'formatoptions' only recognises when the comment string is at start-of-line. The comment starting in the middle of the line is only recognised by the syntax-highlighter, not the formatter. For this reason I don't think it's possible in Vim as shipped. You might be able to find a plugin to do it, but a cursory search on the Vim website didn't turn up anything useful.