# Capture to an existing random file

**URL:** <https://org-roam.discourse.group/t/capture-to-an-existing-random-file/3075>\
**Category:** How To\
**Created:** [May 18, 2023, 10:17pm UTC](https://org-roam.discourse.group/t/capture-to-an-existing-random-file/3075 "2023-05-18T22:17:21Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![bitozoid](https://avatars.discourse-cdn.com/v4/letter/b/3e96dc/32.png) [@bitozoid](https://org-roam.discourse.group/u/bitozoid)\
**Post date:** [May 18, 2023, 10:17pm UTC](https://org-roam.discourse.group/t/capture-to-an-existing-random-file/3075/1 "2023-05-18T22:17:21Z")

</div>

Hi!

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:

```auto
* 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.

```auto
("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:

```auto
("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](https://emacs.stackexchange.com/a/57812)).

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?

Regards

---

<div class="post-metadata">

**Author:** ![nobiot](https://yyz2.discourse-cdn.com/free1/user_avatar/org-roam.discourse.group/nobiot/32/159_2.png) [@nobiot](https://org-roam.discourse.group/u/nobiot)\
**Post date:** [May 19, 2023, 3:53pm UTC](https://org-roam.discourse.group/t/capture-to-an-existing-random-file/3075/2 "2023-05-19T15:53:01Z")

</div>

> [@bitozoid](#):
>
> C: add a template to an existing random file.

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).

---

<div class="post-metadata">

**Author:** ![nobiot](https://yyz2.discourse-cdn.com/free1/user_avatar/org-roam.discourse.group/nobiot/32/159_2.png) [@nobiot](https://org-roam.discourse.group/u/nobiot)\
**Post date:** [May 19, 2023, 4:37pm UTC](https://org-roam.discourse.group/t/capture-to-an-existing-random-file/3075/3 "2023-05-19T16:37:47Z")

</div>

> [@bitozoid](#):
>
> ```auto
> ("r" "Read it later" entry "* %?"
> :target (file+olp nil ("Read it later"))
> :unnarrowed t)
> 
> ```

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

> [@bitozoid](#):
>
> `"%<%Y%m%d%H%M%S>-${slug}.org"`

---

<div class="post-metadata">

**Author:** ![bitozoid](https://avatars.discourse-cdn.com/v4/letter/b/3e96dc/32.png) [@bitozoid](https://org-roam.discourse.group/u/bitozoid)\
**Post date:** [May 19, 2023, 5:27pm UTC](https://org-roam.discourse.group/t/capture-to-an-existing-random-file/3075/4 "2023-05-19T17:27:03Z")

</div>

> [@nobiot](#):
>
> Do you really mean a “random file”?

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

> [@nobiot](#):
>
> 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

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.

---

<div class="post-metadata">

**Author:** ![bitozoid](https://avatars.discourse-cdn.com/v4/letter/b/3e96dc/32.png) [@bitozoid](https://org-roam.discourse.group/u/bitozoid)\
**Post date:** [May 19, 2023, 5:39pm UTC](https://org-roam.discourse.group/t/capture-to-an-existing-random-file/3075/5 "2023-05-19T17:39:25Z")

</div>

> [@nobiot](#):
>
> > [@bitozoid](#):
> >
> > `"%<%Y%m%d%H%M%S>-${slug}.org"`

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

---

<div class="post-metadata">

**Author:** ![nobiot](https://yyz2.discourse-cdn.com/free1/user_avatar/org-roam.discourse.group/nobiot/32/159_2.png) [@nobiot](https://org-roam.discourse.group/u/nobiot)\
**Post date:** [May 19, 2023, 6:01pm UTC](https://org-roam.discourse.group/t/capture-to-an-existing-random-file/3075/6 "2023-05-19T18:01:35Z")

</div>

> [@bitozoid](#):
>
> 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:

> [@bitozoid](#):
>
> By random I mean a “specific file that I choose”

A common [dictionary definition](https://www.merriam-webster.com/dictionary/random#:~:text=%3A%20lacking%20a%20definite%20plan%2C%20purpose,with%20definite%20probability%20of%20occurrence) 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

---

<div class="post-metadata">

**Author:** ![bitozoid](https://avatars.discourse-cdn.com/v4/letter/b/3e96dc/32.png) [@bitozoid](https://org-roam.discourse.group/u/bitozoid)\
**Post date:** [May 20, 2023, 8:53pm UTC](https://org-roam.discourse.group/t/capture-to-an-existing-random-file/3075/7 "2023-05-20T20:53:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bitozoid](https://avatars.discourse-cdn.com/v4/letter/b/3e96dc/32.png) [@bitozoid](https://org-roam.discourse.group/u/bitozoid)\
**Post date:** [May 24, 2023, 6:51pm UTC](https://org-roam.discourse.group/t/capture-to-an-existing-random-file/3075/8 "2023-05-24T18:51:31Z")

</div>

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):

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

```

with

```auto
(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.

---

<div class="post-metadata">

**Author:** ![nobiot](https://yyz2.discourse-cdn.com/free1/user_avatar/org-roam.discourse.group/nobiot/32/159_2.png) [@nobiot](https://org-roam.discourse.group/u/nobiot)\
**Post date:** [May 24, 2023, 8:24pm UTC](https://org-roam.discourse.group/t/capture-to-an-existing-random-file/3075/9 "2023-05-24T20:24:01Z")

</div>

> [@bitozoid](#):
>
> @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.
