automatic C // comments

11 views
Skip to first unread message

rwmit...@gmail.com

unread,
Dec 7, 2022, 1:36:34 PM12/7/22
to vim_use
Vim automatically continues comments to the next line.  I think this is coming from c.vim.  When using // comments, it'll automatically create the second line as:

some-code // my comment
                     // <ready to add next comment>

I found this article:


While it explains what adding f:// back in does, my testing doesn't seem to support my understanding and the article doesn't explain other possible options besides 'f'.  What it does seem to do is disable all auto // comments.

I _think_ what I'd like is if a line begins (other than whitespace) with a  // comment, then continue creating comments on the subsequent lines, but if a comment is preceded by code, do not auto-indent with // on the next line.

Is that possible?  Am I making sense outside my own head?

rwmit...@gmail.com

unread,
Dec 7, 2022, 1:59:11 PM12/7/22
to vim_use
Reading runtime/changes.doc, I see using <Ctrl>U immediately after an indented comment line is created, undoes the auto comment and puts the cursor in the correct column for the next line.  This is likely the correct solution for me.

This is the relevant lines from the above document:

              *fo-r*
r Automatically insert the current comment leader after hitting
  <Enter> in Insert mode.
              *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.

The last 3 lines looks like exactly what I was initially asking for, but I don't understand how to make that happen.

Gary Johnson

unread,
Dec 7, 2022, 3:20:12 PM12/7/22
to vim_use
On 2022-12-07, rwmit...@gmail.com wrote:
> Reading runtime/changes.doc, I see using <Ctrl>U immediately after an indented
> comment line is created, undoes the auto comment and puts the cursor in the
> correct column for the next line. This is likely the correct solution for me.
>
> This is the relevant lines from the above document:
>
> *fo-r*
> r Automatically insert the current comment leader after hitting
> <Enter> in Insert mode.
> *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.
>
> The last 3 lines looks like exactly what I was initially asking for, but I
> don't understand how to make that happen.

The ex command to make that happen is

set fo+=/

That will add the option "letter" / to 'formatoptions'. See

:help set+=

You can put that command in your vimrc to have it apply to all
comments or in a file named ~/.vim/after/ftplugin/c.vim to have it
apply only to C files. See

:help after-directory
:help ftplugin-overrule
:help ftplugin

Regards,
Gary

rwmit...@gmail.com

unread,
Dec 7, 2022, 7:37:20 PM12/7/22
to vim_use

Perfect!  Thanks!!
Reply all
Reply to author
Forward
0 new messages