# How to insert a function to the file name in org-roam-capture-templates

**URL:** <https://org-roam.discourse.group/t/how-to-insert-a-function-to-the-file-name-in-org-roam-capture-templates/2108>\
**Category:** Troubleshooting\
**Created:** [October 8, 2021, 3:29am UTC](https://org-roam.discourse.group/t/how-to-insert-a-function-to-the-file-name-in-org-roam-capture-templates/2108 "2021-10-08T03:29:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![FloppyEggplant](https://avatars.discourse-cdn.com/v4/letter/f/f05b48/32.png) [@FloppyEggplant](https://org-roam.discourse.group/u/FloppyEggplant)\
**Post date:** [October 8, 2021, 3:29am UTC](https://org-roam.discourse.group/t/how-to-insert-a-function-to-the-file-name-in-org-roam-capture-templates/2108/1 "2021-10-08T03:29:33Z")

</div>

How can I change the org-capture templates to dynamically format the file name?

The following example doesn’t work because it will only call the function once - when I evaluate the variable. I need to make it call a function every time I create the template.

The function simply returns the current buffer’s file-name and appends `_anki.org` to the name.

```lisp
(setq org-roam-capture-templates
      `(("a" "anki-cards" plain "%?"
         :if-new (file+head ,(format "%s_anki.org"
                                     (file-name-nondirectory
                                      (file-name-sans-extension
                                       (org-roam-node-file (org-roam-node-at-point)))))
                            "#+title: ${title}\nDescription: ")
         :unnarrowed t)))

```

---

<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 8, 2021, 5:08am UTC](https://org-roam.discourse.group/t/how-to-insert-a-function-to-the-file-name-in-org-roam-capture-templates/2108/2 "2021-10-08T05:08:03Z")

</div>

Try %(sexp) per documentation: “%(function)\_anki.org”, or something along those lines.

Not sure if the at-point func gives you what you intend to get, though (the function may be called within a temp buffer).

On a side note, I suggest to use :target, instead of :if-new, which is being deprecated.

---

<div class="post-metadata">

**Author:** ![FloppyEggplant](https://avatars.discourse-cdn.com/v4/letter/f/f05b48/32.png) [@FloppyEggplant](https://org-roam.discourse.group/u/FloppyEggplant)\
**Post date:** [October 8, 2021, 3:14pm UTC](https://org-roam.discourse.group/t/how-to-insert-a-function-to-the-file-name-in-org-roam-capture-templates/2108/3 "2021-10-08T15:14:06Z")

</div>

Is it possible to pass a variable or to get the buffer from where I called org-capture? As you said, the at-point function returns nil. Is it possible to at least get a link to the current buffer?

---

<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 8, 2021, 7:08pm UTC](https://org-roam.discourse.group/t/how-to-insert-a-function-to-the-file-name-in-org-roam-capture-templates/2108/4 "2021-10-08T19:08:56Z")

</div>

Does this mean that you can now call a function and confirm that the at-point function didn’t return the node’s filename correctly as you expect it would?

An easy way would be to define a global variable and set it just before calling the capture command. I’d wrap it with a custom function. You could consider advising, too.

---

<div class="post-metadata">

**Author:** ![FloppyEggplant](https://avatars.discourse-cdn.com/v4/letter/f/f05b48/32.png) [@FloppyEggplant](https://org-roam.discourse.group/u/FloppyEggplant)\
**Post date:** [October 8, 2021, 7:43pm UTC](https://org-roam.discourse.group/t/how-to-insert-a-function-to-the-file-name-in-org-roam-capture-templates/2108/5 "2021-10-08T19:43:24Z")

</div>

I’ve tried and this is the template works:

```lisp
("a" "anki-cards" plain "%?"
  :target (file+head "%(my-org-roam-anki-card-filename \"${at-point}\")_anki.org"
  "#+title: Anki Cards") :unnarrowed t)

```

This is the function (it is more of an hack as I have to pass a string with the node information and regex the id):

```lisp
(defun my-org-roam-anki-card-filename (node)
  (let* ((file (file-name-nondirectory
                (file-name-sans-extension
                 (replace-regexp-in-string
                  ".*org-roam-node \\(.*?\\)\\.org\\b.*"
                  "\\1"
                  node)))))
    (format "%s" file)))

```

Now I want to pass the title to the template, or at least a link to the original file but the expansion `%a` doesn’t work.

Edit: Forget it. I misread what `%a` does. To have a link to file I just need to insert `[[%f]]`
