5

Edit this C++ excerpt:

// line 1
int xxxxxxxx = 0; // line 2
int yyyyyyyy = 0;

When the cursor is in line 1 and you hit o to insert a line below it, Vim conveniently starts the new line with another // comment.

When the cursor is in line 2 and you do the same, the new line begins with a comment aligned with line 2's trailing comment:

// line 1
int xxxxxxxx = 0; // line 2
                  // 
int yyyyyyyy = 0;

This is unhelpful if you prefer block (multiline) comments to not be deeply indented. A trailing comment is more likely to be two or three words sufficient unto itself, than the unfinished start of a long paragraph.

Can inserting below line 2 begin without an automatically added comment mark (like inserting below line 3), while preserving line 1's behavior?

In C++, set comments? reports comments=sO:* -,mO:* ,exO:*/,s1:/*,mb:*,ex:*/,:///,://, and set formatoptions? reports croql. This is for vim 9.0 patches 1-1424, on both Ubuntu 22.04 and macOS 13.4.1.

(The offending line-2 behavior happens with // also in C, but not with # in bash, php, ruby, etc; and not with // in php).

4
  • 1
    what is your formatoptions? (this never happens to me)
    – Maxim Kim
    Commented Dec 13, 2023 at 0:09
  • 4
    There is / you can add to formatoptions that should do the thing for you. Check :h fo-table for details.
    – Maxim Kim
    Commented Dec 13, 2023 at 0:15
  • @MaximKim I'd accept that as an answer. set formatoptions=croql/ worked on both mac and ubuntu. (Put (something like) that in my vimrc, or elsewhere?) Commented Dec 13, 2023 at 17:53
  • 1
    Put it in your vimrc to have it everywhere or in ~/.vim/after/ftplugin/cpp.vim if you want this option only for C++ files (likewise c.vim for C).
    – Friedrich
    Commented Dec 13, 2023 at 18:36

2 Answers 2

4

Add / to formatoptions

set formatoptions+=/

as described in :h fo-table:

                            *fo-o*
o   Automatically insert the current comment leader after hitting 'o' or
    'O' in Normal mode.  In case comment is unwanted in a specific place
    use CTRL-U to quickly delete it. |i_CTRL-U|
                            *fo-/*
/   When 'o' is included: do not insert the comment leader for a //
    comment after a statement, only when // is at the start of the line.
2

When cindent is on, the comment formatting options are somehow overridden by the cindent and formatoptions settings.

:help format-comments

Enabling 'cindent' will override the alignment flags in many cases.
Reindenting using a different method like |gq| or |=| will not consult
alignment flags either. The same behaviour can be defined in those other
formatting options. One consideration is that 'cindent' has additional options
for context based indenting of comments but cannot replicate many three piece
indent alignments.  However, 'indentexpr' has the ability to work better with
three piece comments.

One solution to this problem is to turn off cindent:

set nocindent

And search for an alternative way to indent c and c++ code...

This indent script could be an example of solution for that problem.

Or even better, as suggested by @MaximKim:

set formatoptions+=/
5
  • 1
    I can't reproduce cindent to have an impact on this issue. Maybe I'm missing something?
    – Friedrich
    Commented Dec 13, 2023 at 12:38
  • @Friedrich, I can reproduce the issue with both Vim 9.0 and Neovim 0.9.4. But with both if I set cindent to false (:set nocindent) the "problem" is gone (actually no comment continuation is done). Commented Dec 13, 2023 at 12:44
  • 1
    I have Vim 9.0 (patches 1-1968). I start with vim --clean foo.cpp and comment continuation is done after both :set cindent and :set nocindent. Very strange :-/
    – Friedrich
    Commented Dec 13, 2023 at 13:01
  • 1
    Also in the case of a line like int a = 3; // foo? Commented Dec 13, 2023 at 13:25
  • 1
    Yes, exactly. That's why I'm puzzled.
    – Friedrich
    Commented Dec 13, 2023 at 13:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.