When I'm writing a comment and press M-j to continue the comment on the next line (using single-line comments), if I press it multiple times say because I want to leave the next comment line "empty" for separation, the cursor ends up appearing right after the comment delimiter. Instead, I always want the cursor to be separated from the comment delimiter by one space. I'd like this to be the case in every mode, globally if possible.

As an example, if I have this:

;; comment|

If I press M-j I get this:

;; comment
;; |

It's correctly separated from the comment delimiter by one space.

If without doing anything else I press M-j again, I get this:

;; comment

So from then on there is no space added between the comment delimiter and the cursor. I want there to always be a space of separation.

By comparison, in c++-mode I always get the space.

I'd like to know if there's a way to get this in other modes (or even all other modes) before I resort to hacking it in by wrapping the function myself.

EDIT: I ended up advising comment-indent-new-line to add one space if there aren't any spaces following the comment-start:

(define-advice comment-indent-new-line
    (:after (&optional soft) at-least-one-space)
  "Ensure that at least one space is added after the comment-start."
  (let ((start (regexp-quote comment-start)))
    (when (and (nth 4 (syntax-ppss))
               (looking-back start (+ (point) (length start)))
               (not (looking-back " "  (+ (point) 1))))
      (insert " "))))

1 Answer 1


I also ran into this issue. It is caused because in emacs-lisp-mode, comment-start is ";" whereas in c++-mode, it is "// " (note the space). The obvious fix would be to add a space to comment-start in emacs-lisp-mode. Unfortunately, this will break many lisp functions which assume that comment-start does not contain a space.

The best solution I've found is to use this advice. You should probably change it to use nadvice.el instead of the old advice system.

(defadvice comment-indent-new-line (after just-one-space-after-comment activate)
  "Make sure that `comment-indent-new-line' does not insert extra spaces."
  (when (and (looking-back comment-start)
             (not (looking-back " ")))
  • Thanks! I appreciate your response. I wanted to see if there was a way that I could set some variable perhaps or something to get this behavior before I went off to advising a function. I wish I could see what was responsible for why it works as I want in c++-mode. Jun 4, 2016 at 7:49
  • @JorgeIsraelPeña See my edit. Unfortunately, if you change the variable in question, it will cause other undesireable behavior.
    – Qudit
    Jun 4, 2016 at 7:56
  • Thanks, that makes a lot of sense. I hesitate to use this advice though because it seems like it'd always force a single space. If I have the comment-text indented and I press M-j, it continues the comment with the same indentation, which I like. It seems like this would force there to be a single space? I ended up creating my own advice which adds one space if there aren't any spaces after the comment-start, which I think is more flexible. Jun 5, 2016 at 21:46
  • Indentation inside comments works for me with that advice. Note that the test (not (looking-back " ")) prevents it from being applied if there are already spaces after the comment.
    – Qudit
    Jun 6, 2016 at 2:36
  • Ah you're absolutely right, not sure how I missed that. Jun 6, 2016 at 2:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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