Org Mode: Prompt for a heading and then refile it to point

| org

I sometimes want the inverse of org-refile when I create a subtree and think of things that should probably go into it. This function prompts for a heading that matches org-refile-targets and then moves it to the current location.

(defun my-org-refile-to-point (refloc)
  "Prompt for a heading and refile it to point."
  (interactive (list (org-refile-get-location "Heading: ")))
  (let* ((file (nth 1 refloc))
         (pos (nth 3 refloc)))
    (save-excursion
      (with-current-buffer (find-file-noselect file 'noward)
        (save-excursion
          (save-restriction
            (widen)
            (goto-char pos)
            (org-copy-subtree 1 t))))
      (org-paste-subtree nil nil nil t))))
View org source for this post
You can comment with Disqus or you can e-mail me at sacha@sachachua.com.