简体   繁体   English

在vim中使用代码和注释格式化行

[英]Format line with code and comments in vim

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: 正如预期的那样,第40个字符的换行符:

    // abc abc abc abc abc abc abc
    // abc

Also, if a line longer than 40 characters is copied and pasted as 此外,如果复制并粘贴长度超过40个字符的行

    // abc abc abc abc abc abc abc abc

and command gqq is typed it ends up with the same result as above. 并输入命令gqq ,结果与上面结果相同。

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 如果输入命令gqq ,则会产生

    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: 我预计插入行超过40个字符或发出gqq都会破坏注释并在下一行中插入“//” - 类似于:

    void funAbc(void) { // abc abc
        // abc abc abc abc abc abc

I've tried setting 'smartindent' but it didn't solved the issue. 我试过设置'smartindent'但它没有解决问题。

Is there any option/trick that can change the behavior of these comments starting after code? 是否有任何选项/技巧可以改变代码后开始的这些注释的行为?

I don't think it is. 我认为不是。 The comment formatting with 'comments' and 'formatoptions' only recognises when the comment string is at start-of-line. 带有'comments'和'formatoptions'的注释格式只能识别注释字符串何时处于行首。 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. 出于这个原因,我不认为在Vim中发货是可能的。 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. 您可能能够找到一个插件来执行此操作,但在Vim网站上粗略搜索并未发现任何有用的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM