0

Based on specific mode, by using a keybinding I want to add a TODO string that is preceded by an unique comment character. For example, (org-mode is exception since ** does not stands for comment and TODO shouldn't followed by :)

  • python-mode: => # TODO:
  • for script-mode: => # TODO:
  • for latex mode: => % TODO:
  • for org-mode: => ** TODO

Basically comment character of the current mode on the buffer followed by TODO: .

1

This function should cover the first cases (comment delimiter followed by " TODO: ")

(defun insert-todo ()
  (interactive "*")
  (insert comment-start " TODO: "))

This one covers the org-mode case:

(defun org-insert-todo ()
  (interactive "*")
  (insert "** TODO "))

You could bind them to a key combination with something like this

(define-key prog-mode-map (kbd "C-c t") #'insert-todo)
(define-key org-mode-map (kbd "C-c t") #'org-insert-todo)

In case you're wondering, the * in the interactive form serves to signal an error when the user invokes the function in a read-only buffer (see the manual).

4
  • 1
    org-mode-map is correct.
    – NickD
    Feb 8 at 15:01
  • Thanks, I've deleted the heads-up in the answer Feb 8 at 15:13
  • is it possible to have C-c C-t instead as keybinding? I tried it but didn't work
    – alper
    Feb 8 at 15:34
  • @alper It's (define-key prog-mode-map (kbd "C-c C-t") #'insert-todo) but note that C-c <letter> keybindings are reserved for the user while C-c C-t might conflict with the major mode's bindings (ref.). That could be why it's not working for you. Feb 8 at 15:40

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.