Search code examples
emacselispindentation

Set comments at the same indentation level as all forms


By default, comments get an indentation level which seems rather alien to me.

(defun example ()
  just
  some
                ; a comment
  words)

How to adjust it so the first semicolon is vertically aligned with the regular Lisp forms?

(defun example ()
  just
  some
  ; a comment
  words)

What I could find out is that the default mechanism works by aligning the comments to a fixed column (queryable via M-x comment-set-column), and that one can modify the comment-indent-function variable (setting it to nil partially fixes my problem).


Solution

  • Emacs indents comments in elisp differently depending on the number of semicolons used. If you use two, you should get the indentation you're after:

    (defun test-single ()
                                            ; A single semicolon
      nil)
    
    (defun test-double ()
      ;; Do two semicolons make a colon ;)
      nil)
    

    In addition, three semicolons ;;; are not re-indented at all. Typically, they are used to mark new major sections in the source file.