As mentioned in Comment Commands, when the M-j command
adds a comment to a line, it tries to place the comment at the column
specified by the buffer-local variable comment-column. You can
set either the local value or the default value of this buffer-local
variable in the usual way (see Locals). Alternatively, you can
type C-x ; (
comment-set-column) to set the value of
comment-column in the current buffer to the column where point
is currently located. C-u C-x ; sets the comment column to
match the last comment before point in the buffer, and then does a
M-; to align the current line’s comment under the previous one.
The comment commands recognize comments based on the regular
expression that is the value of the variable comment-start-skip.
Make sure this regexp does not match the null string. It may match more
than the comment starting delimiter in the strictest sense of the word;
for example, in C mode the value of the variable is
'\\(//+\\|/\\*+\\)\\s *', which matches extra stars and
spaces after the ‘/*’ itself, and accepts C++ style comments
also. (Note that ‘\\’ is needed in Lisp syntax to include a
‘\’ in the string, which is needed to deny the first star its
special meaning in regexp syntax. See Regexp Backslash.)
When a comment command makes a new comment, it inserts the value of
comment-start as an opening comment delimiter. It also inserts
the value of comment-end after point, as a closing comment
delimiter. For example, in Lisp mode, comment-start is
‘';'’ and comment-end is
'' (the empty string). In
C mode, comment-start is
'/* ' and comment-end is
The variable comment-padding specifies a string that the
commenting commands should insert between the comment delimiter(s) and
the comment text. The default, ‘' '’, specifies a single space.
Alternatively, the value can be a number, which specifies that number
of spaces, or
nil, which means no spaces at all.
The variable comment-multi-line controls how M-j and Auto Fill mode continue comments over multiple lines. See Multi-Line Comments.
The variable comment-indent-function should contain a function that will be called to compute the alignment for a newly inserted comment or for aligning an existing comment. It is set differently by various major modes. The function is called with no arguments, but with point at the beginning of the comment, or at the end of a line if a new comment is to be inserted. It should return the column in which the comment ought to start. For example, in Lisp mode, the indent hook function bases its decision on how many semicolons begin an existing comment, and on the code in the preceding lines.