I often see or leave comments in code projects like:

//TODO more efficient hash on keys than BASE64?

Ideally, I would like to be able to highlight a comment as such and org-capture it to an org file like : code.org. The code.org file would contain an org-mode link back to the original file that it was captured in. Then I would be able to treat code.org as an agenda file.

1 Answer 1


A capture template can contain arbitrary elisp code using the %(...) syntax. We just need a function that takes a string (which will be the region you are capturing) and strips the comment characters. The only fussy thing is learning what the comment start character is. This function runs in the capture buffer, not the original one so we have to jump back and lookup comment-start.

(defun capture-comment-line (&optional line)
  (let ((c
            (switch-to-buffer (plist-get org-capture-plist :original-buffer))
    (while (string-prefix-p c line)
      (setq line (string-remove-prefix c line)))
    (comment-string-strip line t t)

Then we make a simple capture template:

(add-to-list 'org-capture-templates
       '("C" "TODO comment" entry
         (file+headline "code.org" "Tasks")
         "* %(capture-comment-line \"%i\")\n  %a"
  • 1
    I haven't tried to implement this yet. Looking at it though it doesn't seem to create an org-mode link in the code.org entry back to the comment
    – kevzettler
    Commented Apr 23, 2017 at 16:42
  • 2
    The %a expands to a link back to wherever you called org capture from
    – erikstokes
    Commented Apr 23, 2017 at 16:43

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.