Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong indentation of comments in Emacs

Tags:

emacs

elisp

In many languages, the line comment starts with a single symbol, for example # in Python and R.

I find that in Emacs, when writing such line comments, I have to repeat the comment symbol twice to make the correct indentation.

See the following example:

(setq x-select-enable-clipboard t)
                                        ;using a single comment symbol indents wrongly
;; repeating the comment symbol indents fine
(setq-default c-basic-offset 4)

With a single ; at the beginning of the line cannot get the correct indentation. How to get the correct setting? Thanks!

EDIT:

I found the solution myself. In ESS's document:

Comments are also handled specially by ESS, using an idea borrowed from the Emacs-Lisp indentation style. By default, comments beginning with ‘###’ are aligned to the beginning of the line. Comments beginning with ‘##’ are aligned to the current level of indentation for the block containing the comment. Finally, comments beginning with ‘#’ are aligned to a column on the right (the 40th column by default, but this value is controlled by the variable comment-column,) or just after the expression on the line containing the comment if it extends beyond the indentation column. You turn off the default behavior by adding the line (setq ess-fancy-comments nil) to your .emacs file.

So I put this in my .emacs:

(setq ess-fancy-comments nil) ; this is for ESS

I think for Python mode, it has a similar variable.

like image 936
Yin Zhu Avatar asked Oct 11 '14 07:10

Yin Zhu


People also ask

How to make comments in Emacs?

As mentioned above, the CTRL +; key in Emacs comments out the entire line where the cursor or the region is located. If you want to comment out only the selected region, you can use the ALT +; key. For example, consider the following selected region. To comment out only the highlighted region, use the ALT +; key.

How do I indent multiple lines in Emacs?

26.3. 2 Indenting Several Lines One way to do this is to use the mark; when the mark is active and the region is non-empty, TAB indents every line in the region. Alternatively, the command C-M-\ ( indent-region ) indents every line in the region, whether or not the mark is active (see Indentation Commands).


1 Answers

Your example use Emacs Lisp, in this language the standard convention is that a single ; is indented to the right, whereas two ;; is indented like code would be indented at that point. I strongly recommend that you stick to this convention, otherwise your code would stand out as being different. And three ;;; is indented to the left. Four ;;;; is left indented, and used for major sections. (See https://www.gnu.org/software/emacs/manual/html_node/elisp/Comment-Tips.html)

For Ruby, comments always indent as code, as far as I know.

like image 137
Lindydancer Avatar answered Sep 18 '22 18:09

Lindydancer