Q: how do I get yasnippet to insert the comment sign, when I press return to start new line?

For example, my snippet looks like this

         ;; $1 .
         ;; / END 

When the cursor is on the $1, and I'm typing some text, I would like to press return. It will start a new line, but the ;; is missing on the new line.

I found nothing on this specific issue. I could also use elisp forms to create a new mapping on the new line, like (concat comment-start), but it's not clear to me how to expand them appropriately.

See the comment below.

  • Are you talking about after expanding the snippet, or while writing the snippet? Does hitting M-j (indent-new-comment-line) give you behavior that you want?
    – nanny
    May 21, 2015 at 13:18
  • Nanny, your comment is appreciated.. but no, when I'm calling (indent-new-comment-line) inside the snippet, it will begin a new line, but without comment tag.
    – ReneFroger
    May 21, 2015 at 13:27

1 Answer 1


Before I created a function to solve this, I looked into the Lisp documentation about comment strings and Yasnippet bindings. What about this one:

(defun foo-bar ()
  (insert (concat comment-start) " ")

(eval-after-load 'yasnippet
     (define-key yas-keymap (kbd "<return>") 'foo-bar)))  
  • I prefer double comment tag ;; instead ;, I got only one. But your solution works anyway.
    – ReneFroger
    May 21, 2015 at 18:51

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.