Next: Multi-Line Comments, Previous: Comment Conventions, Up: Comments [Index]
Because comments are such an important part of programming, Emacs provides special commands for editing and inserting comments. It can also do spell checking on comments with Flyspell Prog mode (*note Spelling::).
comment-dwim
(‘M-;’)Insert or realign comment on current line. If the region is active, comment or uncomment the region instead. In this case, supplying a positive ARG adds that many comment characters, or if negative, removes that many.
Call the comment command you want (“Do What I Mean”).
If the region is active and ‘transient-mark-mode’ is on, call
comment-region
(unless it only consists of comments, in which case it calls
uncomment-region
).
Else, if the current line is empty, call comment-insert-comment-function
if
it is defined, otherwise insert a comment and indent it. If the line is
blank, the comment is indented to the same position where <TAB> would indent
to; if the line is non-blank, the comment is placed after the last
non-whitespace character on the line, somewhere between comment-column
and
comment-fill-column
if possible. Point is placed after the comment
character.
Else if a prefix ARG is specified, call comment-kill
.
The comment is saved to the kill ring, and you can reinsert it on another
line by moving the end of that line, doing ‘C-y’, and then ‘M-;’ to realin
the comment.
Else, call comment-indent
. If a line already contains the comment-start
string, this command realigns it to the conventional alignment and moves
point after. Comments starting in column 0 are not moved.
If you are typing a comment and wish to continue it to another line, type ‘M-j’
or ‘C-M-j’ (comment-indent-new-line
). This breaks the current line, and
inserts the necessary comment delimiters and indentation to continue the
comment.
comment-line
(‘C-x C-;’)Comments or uncomments complete lines.
Comment or uncomment current line and leave point after it. If region is active, comment lines in active region instead. Unlike ‘comment-dwim’, this always comments whole lines.
With positive prefix, apply to N lines including current one. With negative prefix, apply to -N lines above. Also, further consecutive invocations of this command will inherit the negative argument.
comment-region
(‘C-c C-c’)Add comment delimiters to all the lines in the region, even if the region is inactive.
Use ‘C-c C-c’ in C-like languages.
Comment or uncomment each line in the region.
With just ‘C-u prefix arg’, uncomment each line in region BEG .. END.
Numeric prefix ARG means use ARG comment characters. If ARG is negative, delete that many comment characters instead.
The strings used as comment starts are built from comment-start
and
comment-padding
; the strings used as comment ends are built from
comment-end
and comment-padding
.
For C-like modes, you can configure the exact effect of ‘M-;’ by setting the
variables c-indent-comment-alist
and c-indent-comments-syntactically-p
.
uncomment-region
Uncomment each line in the BEGIN .. END region.
The numeric prefix ARG can specify a number of chars to remove from the comment delimiters.
comment-indent-new-line
(‘C-M-j’, ‘M-j’)If you are typing a comment and wish to continue it to another line, type ‘M-j’ or ‘C-M-j’ (‘comment-indent-new-line’). This breaks the current line, and inserts the necessary comment delimiters and indentation to continue the comment.
Like ‘<RET>’ followed by inserting and aligning a comment.
This command is intended for styles where you write a comment per line, starting a new comment (and terminating it if necessary) on each line. If you want to continue one comment across several lines, use M-x newline-and-indent.
Break line at point and indent, continuing comment if within one. This indents the body of the continued comment under the previous comment line.
The inserted newline is marked hard if variable ‘use-hard-newlines’ is true, unless optional argument SOFT is non-nil.
comment-insert-comment-function
(a variable)Function to insert a comment when a line doesn’t contain one.
comment-kill
Kill the first comment on this line, if any.
With prefix ARG, kill comments on that many lines starting with this one.
comment-indent
Indent this line’s comment to ‘comment-column’, or insert an empty comment.
if non-nil, use the ‘comment-continue’ markers if any.
comment-set-column
(‘C-x ;’)Set the comment column based on point. With no ARG, set the comment column to the current column.
With just minus as arg, kill any comment on this line.
With any other ARG, set comment column to indentation of the previous comment and then align or create a comment on this line at that column.
comment-column
(a variable)Column to indent right-margin comments to. Each mode may establish a different default value for this variable; you can set the value for a particular mode using that mode’s hook.
comment-continue
(a variable)Continuation string to insert for multiline comments. This string will be
added at the beginning of each line except the very first one when commenting
a region with a commenting style that allows comments to span several lines.
It should generally have the same length as comment-start
in order to
preserve indentation.
comment-start
(a variable)String to insert to start a new comment, or nil if no comment syntax.
comment-fill-column
(a variable)Column to use for ‘comment-indent’. If nil, use ‘fill-column’ instead.
comment-style
(a variable)Its value is ‘indent’.
Style to be used for ‘comment-region’.
comment-styles
(a variable)Comment region style definitions.
c-indent-comment-alist
(a variable)c-indent-comments-syntactically-p
(a variable)Next: Multi-Line Comments, Previous: Comment Conventions, Up: Comments [Index]