4.2 Comment Commands

When the commands in this section add comment delimiters, they use either line comments or block comments depending on the setting of the comment style (see Minor Modes).

C-c C-c (comment-region)

This command comments out the lines that start in the region. With a negative argument, it does the opposite - it deletes the comment delimiters from these lines. See Multi-Line Comments in GNU Emacs Manual, for fuller details. comment-region isn’t actually part of CC Mode - it is given a CC Mode binding for convenience.

M-; (comment-dwim or indent-for-comment 4)

Insert a comment at the end of the current line, if none is there already. Then reindent the comment according to comment-column (see Options for Comments in GNU Emacs Manual) and the variables below. Finally, position the point after the comment starter. C-u M-; kills any comment on the current line, together with any whitespace before it. This is a standard Emacs command, but CC Mode enhances it a bit with two variables:

User Option: c-indent-comment-alist

This style variable allows you to vary the column that M-; puts the comment at, depending on what sort of code is on the line, and possibly the indentation of any similar comment on the preceding line. It is an association list that maps different types of lines to actions describing how they should be handled. If a certain line type isn’t present on the list then the line is indented to the column specified by comment-column.

See the documentation string for a full description of this variable (use C-h v c-indent-comment-alist).

User Option: c-indent-comments-syntactically-p

Normally, when this style variable is nil, M-; will indent comment-only lines according to c-indent-comment-alist, just as it does with lines where other code precede the comments. However, if you want it to act just like TAB for comment-only lines you can get that by setting c-indent-comments-syntactically-p to non-nil.

If c-indent-comments-syntactically-p is non-nil then c-indent-comment-alist won’t be consulted at all for comment-only lines.


Footnotes

(4)

The name of this command varies between (X)Emacs versions.