3

If I'm writing a comment

;; this is a comment
;; this is its second line

And I hit enter, I get a new line like this:

;; this is a comment
;; this is its second line

Instead of getting a new line with comments continued, like this:

;; this is a comment
;; this is its second line
;;

How do I get the latter behavior? I'm editing clojure if that matters

  • 1
    How is it supposed to know that you want to continue the comment? How is it going to know that you have finished the comment and you now want to write some code? – NickD Jul 11 at 11:38
  • 2
    It can't know. But it can be more convenient to manually break out of the comment (once) than to manually continue the comment (for every line of it). – phils Jul 11 at 12:09
2

The standard key for this behaviour is M-j. It is bound to indent-new-comment-line by default but, depending on the buffer, may be bound to some other mode-specific analog. These commands take care of indentation, and also comment continuation when you're inside one.

In programming modes I make RET do whatever M-j does:

(defun my-coding-config ()
  "Custom behaviours for most programming modes."
  ;; Make RET maintain indentation and comments.
  (local-set-key (kbd "RET") (key-binding (kbd "M-j")))
  (local-set-key (kbd "<S-return>") 'newline))

(mapc
 (lambda (language-mode-hook)
   (add-hook language-mode-hook 'my-coding-config))
 '(prog-mode-hook
   ;; plus anything not derived from prog-mode:
   inferior-emacs-lisp-mode-hook
   css-mode-hook
   python-mode-hook))

(add-hook 'js-mode-hook 'my-js-mode-hook)
(defun my-js-mode-hook ()
  "Custom behaviours for `js-mode'."
  ;; Fix M-j behaviour in block comments in js-mode
  (setq-local comment-multi-line t)
  (local-set-key [remap indent-new-comment-line] 'c-indent-new-comment-line))
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.