I currently have a function like so in my .emacs file:

(defun insert-todo () (interactive) (insert "// TODO(map) : ") )

It is bound to my key combination of "C-t". This will work for me if I'm doing Java or JavaScript code as the // is the start of a comment for that particular language. However, I want to dynamically insert the appropriate comment character based on the major mode I'm in.

For example:

Java Mode -> // Python mode -> # Lisp mode -> ;; etc...

So how do I, in my .emacs config file, get the current major mode so I can insert the appropriate comment characters before adding my TODO string?

  • You're reinventing newcomment.el which provides functionality for language-agnostic commentary.
    – wasamasa
    Jun 1, 2018 at 18:42
  • Does that package allow for extending the functionality to include extra information in the comment? So not just deciding on which type of characters are used for commenting?
    – Michael
    Jun 1, 2018 at 18:55
  • It gives you all the comment-related commands, including one to comment out a region. So, insert text, comment out the region and you're done.
    – wasamasa
    Jun 3, 2018 at 14:29
  • So that's not exactly what I want. It's great that I can type text and then comment it out, but the goal is basically to bind a key that auto inserts my TODO(map) as that is what I do searches on in my code before commits.
    – Michael
    Jun 4, 2018 at 16:33
  • Yes, this is done programmatically. It's extremely common to change the location of point, insert text into a buffer and call other functions manipulating buffer text. Rejecting an approach on this basis would mean rejecting lots of packages.
    – wasamasa
    Jun 4, 2018 at 17:02

1 Answer 1


Commonly major-modes are setting comment-start and comment-end. Below an example how to access these values:

(defun give-me-comment-starters-ender-for-a-mode (mode)
  "Returns a cons containing comment-start comment-end of arg MODE"
    (funcall mode)
    (cons comment-start comment-end)))

(defun comment-starter-ender-return-test (&optional arg)
  (let ((erg (give-me-comment-starters-ender-for-a-mode (or arg 'emacs-lisp-mode))))
    (message "%s" erg)))

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.