Capture to an existing random file


I mainly see three use cases for capture:

  • A: add a template to an existing predefined file,
  • B: add a template to a new file,
  • C: add a template to an existing random file.

I would say that org-capture / org-capture-templates fit great for A, and org-roam-capture / org-roam-capture-templates fit great for B, but I would expect org-roam-capture / org-roam-capture-templates to also fit for case C.

To be precise, I’d like to add some common sections to my roam files and I’d like to be able to capture directly to those sections. For example, if I find an interesting article Z about X but I would like to read it when dealing with X, I’d like to start a capture where I can select the node X, and then write some info about Z, so that it gets stored as:

* All about X
** X1
** X2
* Read it Later
** Y
** Z
My captured note here.

With M-x org-roam-capture, I am asked for a node, and then for a template. But IMHO the documentation is not clear about how this case is managed. I can do some testing, but I wonder whether this case is considered, and how it is dealt with for the different :target options.

For example, if I use the default template in an existing node it allows me to capture ‘plain’ at the start of the file just after the properties.

("d" "default" plain "%?"
   :target (file+head "%<%Y%m%d%H%M%S>-${slug}.org" "#+title: ${title}\n")
   :unnarrowed t)

This one seems to fit my case, using a nil path, not to be used to create new files:

("r" "Read it later" entry "* %?"
   :target (file+olp nil ("Read it later"))
   :unnarrowed t)

However I’m allowed to use it with a new file and the error management does not look good.

Besides, it would make more sense to me first to select the template, and then to choose whether a new node or an existing node would be used. I figure out that at the moment org-roam is calling to org-capture, so that’s not possible right now. This is something that would make org-protocol much more powerful. As a workaround for org-protocol, I can just open the node in Emacs and call org-capture using then variable buffer-file-name as file path in the template (org-capture: prompt for a target file - Emacs Stack Exchange).

To sum up,

  • are the org-roam-capture-templates designed to be used in random files?
  • in such case, do you think the documentation (both manual and variable doc) is clear enough about it?
  • is there a nice way to define a ‘null path’ template that is not designed/allowed to be used for a new file?
  • is there a nice way to use org-protocol to fill up an existing roam file with a ‘null path’ template?


There are many components to your question; just to take a step back, so that we understand you correctly.

Do you really mean a “random file”? It seems to me what you are really going for is a “specific file that I choose”, not letting the computer to choosing one “randomly” for you (you’d never know which one is to be chosen).

If you have a file name like this, as an example, it seems to be working in the way I interpret what you wish to achieve

By random I mean a “specific file that I choose” (random as in Random Access Memory).

Is there a way to restrict the creation of a file with a template that has been designed just for existing files (what I call a “null path” template)? Using nil I cannot create a new file, but the result is not very nice.

I can create a new file with a template that uses this file name. I wonder whether I could avoid this behaviour.

I can avoid this by calling org-roam-captue and choose an existing file.

Just as a side note on this:

A common dictionary definition of “random” is opposite of “your choosing”. If you choose a file, that is not random.

  1. a lacking a definite plan, purpose, or pattern
  2. relating to, having, or being elements or events with definite probability of occurrence

Thanks a lot for your feedback.

I’ve noticed the evolution from :filename/:head, :if-new and now :target property. The definition of the :target property, which looks similar to the target parameter in org-capture-templates, looks to me a bit messy for the case of a specific file that I choose, maybe cause org-capture-templates seems to be designed for a predefined target.

Not a query, but a suggestion for future enhancements.

1 Like

I think I got it working nicely, and it may work with org-protocol.

In org-capture-templates (maybe in org-roam-capture-templates as well, haven’t tried yet):

 "Read it later"
 (file+headline org-roam-node-file-read "Read it later")
 "* %?"


(defun org-roam-node-file-read ()
  "Get path for node"
   (org-roam-node-file (org-roam-node-read)))

@nobiot, would you consider adding this function to org-roam? It looks short, simple, and very useful for this case.

I am a user just like you. All I can suggest is you create a PR or an issue in the GitHub repo as a feature request and see what the project would say.