# Using org-capture template expansion "%(EXP)" in org-roam

**URL:** <https://org-roam.discourse.group/t/using-org-capture-template-expansion-exp-in-org-roam/3246>\
**Category:** Troubleshooting\
**Created:** [October 3, 2023, 4:46pm UTC](https://org-roam.discourse.group/t/using-org-capture-template-expansion-exp-in-org-roam/3246 "2023-10-03T16:46:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tolisin](https://avatars.discourse-cdn.com/v4/letter/t/ed655f/32.png) [@Tolisin](https://org-roam.discourse.group/u/Tolisin)\
**Post date:** [October 3, 2023, 4:46pm UTC](https://org-roam.discourse.group/t/using-org-capture-template-expansion-exp-in-org-roam/3246/1 "2023-10-03T16:46:34Z")

</div>

Hey there,

has anyone used the org-capture template expansion “%(EXP)” ([Template expansion (The Org Manual)](https://orgmode.org/manual/Template-expansion.html)) and made it work during node creation in org-roam? Here’s a minimal example of what I am trying to achieve:

```auto
  (use-package org-roam
    :custom
    (org-roam-directory "~/org/roam/")
    (org-roam-capture-templates
     '(("d" "default" plain
        "%?"
        :if-new (file+head "${slug}.org" "#+title: ${title}\n#+date: %U\n#+filetags: %(org-roam-node-insert)")
        :unnarrowed t)))
    :bind (("C-c n l" . org-roam-buffer-toggle)
           ("C-c n f" . org-roam-node-find)
           ("C-c n i" . org-roam-node-insert))
    :config
    (org-roam-db-autosync-mode t))

```

**How it should work:** Creating a new node via “org-roam-node-insert” or “org-roam-node-find” will call “org-roam-node-insert” inside the capture buffer. A node can be selected and will be inserted after “#+filetags:”.

**What actually happens:** “org-roam-node-insert” is called and a node can be selected. However, the node is not inserted.

Is this a limitation of %(EXP) or org-roam?

---

<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:** [October 3, 2023, 4:53pm UTC](https://org-roam.discourse.group/t/using-org-capture-template-expansion-exp-in-org-roam/3246/2 "2023-10-03T16:53:45Z")

</div>

I think this answers your question: [Capture template from file but only for new entries - #9 by nobiot](https://org-roam.discourse.group/t/capture-template-from-file-but-only-for-new-entries/3114/9)

---

<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:** [October 3, 2023, 6:24pm UTC](https://org-roam.discourse.group/t/using-org-capture-template-expansion-exp-in-org-roam/3246/3 "2023-10-03T18:24:29Z")

</div>

> [@Tolisin](#):
>
> **What actually happens:** “org-roam-node-insert” is called and a node can be selected. However, the node is not inserted.

Okay, my previous response was probably unhelpful given this result. I think this result means that you need to create a wrapper function to return a string you want to be inserted and that `org-roam-node-insert` does not return a string. I think it returns nil (and inserts the node as a side effect). Does this make sense to you?

---

<div class="post-metadata">

**Author:** ![Tolisin](https://avatars.discourse-cdn.com/v4/letter/t/ed655f/32.png) [@Tolisin](https://org-roam.discourse.group/u/Tolisin)\
**Post date:** [October 3, 2023, 6:53pm UTC](https://org-roam.discourse.group/t/using-org-capture-template-expansion-exp-in-org-roam/3246/4 "2023-10-03T18:53:35Z")

</div>

This makes absolutely sense. I was assuming that org-roam-node-insert returns a string. I am not sure, if I am proficient enough to write a wrapper. Maybe at some point. Calling org-roam-node-insert manually is a viable option for the time being 🙂

---

<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:** [October 3, 2023, 7:31pm UTC](https://org-roam.discourse.group/t/using-org-capture-template-expansion-exp-in-org-roam/3246/5 "2023-10-03T19:31:12Z")

</div>

It should be something like this.

```auto
(with-temp-buffer
  (org-roam-node-insert)
  (buffer-string))

```

---

<div class="post-metadata">

**Author:** ![Tolisin](https://avatars.discourse-cdn.com/v4/letter/t/ed655f/32.png) [@Tolisin](https://org-roam.discourse.group/u/Tolisin)\
**Post date:** [October 4, 2023, 8:17am UTC](https://org-roam.discourse.group/t/using-org-capture-template-expansion-exp-in-org-roam/3246/6 "2023-10-04T08:17:32Z")

</div>

This worked marvelously! Thank you very much!

```auto
  (defun insert-node-as-str ()
    "Call org-roam-node-insert and return a string."
    (with-temp-buffer 
     (org-roam-node-insert)
     (buffer-string)))

```

Just add “%(insert-node-as-str)” to the capture template.

---

<div class="post-metadata">

**Author:** ![padraic](https://avatars.discourse-cdn.com/v4/letter/p/b19c9b/32.png) [@padraic](https://org-roam.discourse.group/u/padraic)\
**Post date:** [October 16, 2023, 8:55pm UTC](https://org-roam.discourse.group/t/using-org-capture-template-expansion-exp-in-org-roam/3246/7 "2023-10-16T20:55:47Z")

</div>

Just adding my two cents, I had to add `(interactive)` for this to work

```auto
(defun insert-node-as-str ()
  "Call org-roam-node-insert and return a string."
  (interactive)
  (with-temp-buffer 
    (org-roam-node-insert)
    (buffer-string)))

```
