2

Starting from the existing customization associated with key j, is there a way to view and modify the target file? In other words,

  • by default, captured notes would go to */journal/inbox.org,
  • optionally, input a different path, say */journal/pc.org.

Could you suggest ways to do that?

~/.emacs:

 '(org-capture-templates
   (quote
    (
     ("j" "Journal" entry
      (file+olp+datetree "~/Documents/write/notes/journal/inbox.org")
      "* %?
Entered on %U
  %i
  %a")
     )
    )
   )

Manual:

2

One way is to write a custom function and use it as target: (function function-finding-location). But this would get complicated.


Another way is to use a variable as file and then use a wrapper around org-capture to set that variable to the desired file first:

(defvar my-org-capture-filename nil
  "File name for org capture template.")

(setq org-capture-templates
      '(("j" "journal" entry (file+olp+datetree my-org-capture-filename)
         "* %?
Entered on %U
  %i
  %a")))

(defun my-org-capture ()
  "Read file name to capture to."
  (interactive)
  (setq my-org-capture-filename
        (read-file-name "Capture to: " "~/Documents/write/notes/journal"
                        nil t "inbox.org"))
  (call-interactively #'org-capture))

Then use M-x my-org-capture


You could create a template for each file and use ji, jp etc. as key to capture. To make the group work add ("j" "journal") in the template:

(setq org-capture-templates
      '(("j" "journal")
        ("ji" "inbox.org" entry
         (file+olp+datetree "~/Documents/write/notes/journal/inbox.org")
         "* %?
Entered on %U
  %i
  %a")
        ("jp" "pc.org" entry
         (file+olp+datetree "~/Documents/write/notes/journal/pc.org")
         "* %?
Entered on %U
  %i
  %a")))
  • I think there are several problems with the second solution: the read-file-name call should have the full pathname for "inbox.org" (and if that pathname starts with ~, it should probably be wrapped in an expand-file-name). I still can't get it to work ("Invalid capture target specification") but even if I got it to work, having to use a different function for this template (or having all templates go through this function and answering the question every time) would be a killer for me. I much prefer the third solution: it's closer to the spirit of how I think org-capture should be used. – NickD Dec 2 '19 at 2:55
  • The "invalid capture target specification" errors I was getting were my fault: I had switched to an older version to test something and forgot to switch back. – NickD Dec 2 '19 at 2:56
  • @NickD expand-file-name is already done inside read-file-name respectively read-file-name-default. Adapted the function call to use initial instead of default-filename to not have to use the full path. – Hubisan Dec 2 '19 at 10:54
  • First sol works like a charm. Got rid of %i %a, since linking to the file:header from where the capture was called not always warranted. If it is, I use M-x org-store-link. – Erwann Dec 2 '19 at 19:18

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.