I am trying to create a function, which captures a note and then copies the same note to another file (In my case for a google calendar synchronization). The problem ist that after giving the capture parameters the capture buffer closes automatically unlike the regular behaviour, where I have the option to finalize or refile the capture: enter image description here

How should I edit the following function to remain in capture buffer?

(defun org-capture-copy-to-gcal ()
  (find-file "D:/Google Drive/Documents/gcal.org")

And is there a way to customize the capture refile shortcut to another shorter key?


The problem is how to refile the same note to a second target from org-caputure, which I'm not sure is possible with the current code, but here's something you could try without having to manually refile the note:

("g" "gcal" plain (file+function "~/file-a.org" org-refile-subtree-prepare)
 "* %?")

(defun org-refile-subtree-prepare ()
  (goto-char (point-min))
  (add-hook 'org-capture-after-finalize-hook 'org-refile-subtree))

(defun org-refile-subtree ()
    (let ((marker org-capture-last-stored-marker))
      (switch-to-buffer (marker-buffer marker))
      (goto-char (marker-position marker))
          (find-file-noselect "~/file-b.org") ; <= gcal.org
        (goto-char (point-max))
        (remove-hook 'org-capture-after-finalize-hook

(advice-add 'org-capture-kill :before (lambda ()
                                        (remove-hook 'org-capture-after-finalize-hook

What this does is it jumps to the last stored marker, copies the subtree, pastes it in the second target, and removes the hook when it finishes.

Now to run your gcal code from a function without going through the interactive template:

(defun org-capture-copy-to-gcal ()
  (org-capture nil "g"))

And if you don't want the gcal template showing up when you call org-caputure interactively, change the key (in this case g) to gcal or some other string.

| improve this answer | |
  • Autorefiling is actually possible with the code above. What I am really missing is to have an option to edit the capture buffer manually, wehn using defun() environment, it just closes immediately after the input queues unlike the capture buffer, which you call with the capture command. – Rareform Apr 8 '19 at 12:24
  • Could you maybe explain what you mean by "closing immediately after the input"? You may want to include a simple template that triggers the problem you're seeing. Your code won't copy the contents of the entry to the second file if that's what you're trying to do. I edited the answer. See if that works for you. – jagrg Apr 8 '19 at 18:09

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.