1

Let's say that I have a tree of tasks like so:

* TODO Launch website
** TODO Get database running
*** TODO Select MySQL vs. PostgreSQL

Let's say I'm looking at the "Select MySQL vs. PostgreSQL" and I decide I want to file a task under that headline using my Org Capture template. Is there a way to directly tell org-capture to file the new headline under the current headline (i.e., the headline at (point))? I'm aware of the (clock) capture target, but I don't necessarily want to have to clock in at the current point.

1

Go to a new line below the headline *** TODO Select MySQL vs. PostgreSQL Then press C-u 0 before your capture key, i.e.

C-u 0 <your_capture_key>

and you're good to go. The indentation level will be three stars, so you have to indent it manually by M- rightarrow

You can also press C-0 <your_capture_key> and Emacs will interpret it as C-u 0

| improve this answer | |
  • Yes, this works pretty well actually! I might change my answer to be based on this one, since I still do want to automatically adjust the indentation level. – telotortium Aug 9 at 20:57
  • Let me know when you figure out the automatic indentation. I'd be interested, too – Peter Bamm Aug 10 at 16:49
0

I've implemented the following functions to implement this feature:

  • my-org-capture-under-headline: the main command.
  • Some advice around org-capture to provide a new C-u 2 prefix argument
;; Create ‘C-u 2 M-x org-capture’ command to refile org-capture template under
;; headline at point.
(defvar my-org-capture-rfloc nil
  "Holds the RFLOC argument to pass to ‘org-refile’.")
(defun my-org-capture-under-headline (&optional goto keys)
  "Capture a headline using ‘org-capture’, according to the template you \
select, and then immediately refile that headline under the headline at the \
current point.  GOTO and KEYS are passed to ‘org-capture’."
  (interactive "P")
  (unwind-protect
      (progn
        (unless (eq major-mode 'org-mode)
          (user-error "Must be called from an Org-mode buffer"))
        ;; Capture refile target at point. For format see
        ;; ‘org-refile-target-table’.
        (setq my-org-capture-rfloc
              (list
               (org-display-outline-path t t nil t)
               (buffer-file-name (buffer-base-buffer))
               nil
               (org-with-wide-buffer
                (org-back-to-heading t)
                (point-marker))))
        (funcall #'org-capture goto keys)
        (when my-org-capture-rfloc
          (org-capture-goto-last-stored)
          ;; Refile last-captured target under the headline stored earlier.
          (org-refile nil nil my-org-capture-rfloc)
          ;; Ensure point is at the newly-captured and refiled headline.
          (org-refile-goto-last-stored)))
    ;; Ensure ‘my-org-capture-rfloc’ is reset.
    (setq my-org-capture-rfloc nil)))
(defun my-org-capture-under-headline-prefix (_orig-fn &rest _args)
  "Provide an additional “C-u 2” prefix arg to `org-capture'.

When ‘org-capture’ is called with this prefix argument, it will capture a
headline according to the template you select, and then immediately refile that
headline under the headline at the current point."
  (let ((goto (nth 0 _args)))
    (if (equal goto 2)
        (apply #'my-org-capture-under-headline _args)
      (apply _orig-fn _args))))
(advice-add 'org-capture :around #'my-org-capture-under-headline-prefix)
| improve this answer | |

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.