Crude capture template

I created a rather crude org-roam-capture template which I had hoped would be able to quickly capture a region of text and turn it into an org-roam note. But it inserts %i into every line.

 '(org-roam-capture-templates
   '(("d" "default" plain #'org-roam-capture--get-point "%?" :file-name "%<%Y%m%d%H%M%S>-${slug}" :head "#+title: ${title}
" :unnarrowed t)
     ("c" "default" plain #'org-roam-capture--get-point "%i" :file-name "%<%Y%m%d%H%M%S>-${slug}" :head "#+title: ${title}
%?" :unnarrowed t)))

Example:

#+title: Lorem-test
Pellentesque tristique imperdiet tortor.
%?Pellentesque tristique imperdiet tortor.
%?Pellentesque tristique imperdiet tortor.
%?

Where am I going wrong?

The second template works fine for me as expected.

(setq org-roam-capture-templates
      '(("d" "default" plain #'org-roam-capture--get-point "%?" :file-name "%<%Y%m%d%H%M%S>-${slug}" :head "#+title: ${title}
" :unnarrowed t)
        ("c" "default" plain #'org-roam-capture--get-point "%i" :file-name "%<%Y%m%d%H%M%S>-${slug}" :head "#+title: ${title}
%?" :unnarrowed t)))

Yes, you are correct. I am not sure why it is working now but it is. Can I ask a follow up question? If one wanted to add this line to the template after #+title: how would one do it?

#+ROAM_TAGS:

One way to do it is to put it directly in the :head section after #+title:.

(setq org-roam-capture-templates
      '(("c" "default" plain #'org-roam-capture--get-point "%i" :file-name "%<%Y%m%d%H%M%S>-${slug}" :head "#+title: ${title}
#+ROAM_TAGS:
%?" :unnarrowed t)))

You can also put it in the template string, the 5th element of the list, where you have the %i wildcard.
For larger templates it may be more convenient to use template files. Check the documentation of org-capture-templates for details. Org-roam capture templates are built on Org capture, so most of the things from there will also work with Org roam.

1 Like

Thank you. I have read carefully the documentation on org-roam templates in the Manual but it is rather sparse. I have been having some difficulty setting up templates in org-roam though I have succeeded with org-capture. For example, if I try to add a link to the location from where I called the capture command, org-roam treats the captured region as a link. See the second example below:

 '(org-roam-capture-templates
   '(("d" "default" plain #'org-roam-capture--get-point "%?" :file-name "%<%Y-%m-%d>-${slug}" :head "#+title: ${title}
" :unnarrowed t)
     ("l" "clipboard" plain #'org-roam-capture--get-point "My comment: %^{My comment}\n Date inserted: %U\n %i\n %a\n" :file-name "%<%Y%m%d%H%M%S>-${slug}" :head "#+title: ${title}
#+ROAM_TAGS: %?
" :unnarrowed t :prepend t)))
1 Like

Do you mean the selected text becomes a part of the link as in file:/path/to/file.org::text to be captured? This is what the %a (annotation) and other link wildcards stand for: a link to the location the capture process was initiated from. If a region was selected, the link would point to that part of the buffer. Org roam’s behaviour is not different from Org capture’s here.

Or do you mean Org roam converts the selected text into a link in the buffer of origin? Then you are probably calling org-roam-insert instead of org-roam-capture or org-roam-find-file.

1 Like

Inclusion of selected text, a search string in terms of Org mode, can be inhibited by setting org-link-context-for-files to nil. Note however, this will affect all links stored with org-store-link, not only the links created by org-capture and org-roam-capture.

A more flexible approach would be to have a wrapper function that would call org-roam-capture with org-link-context-for-files bound to nil dynamically:

(defun my-org-roam-capture ()
  (interactive)
  (let ((org-link-context-for-files))
    (call-interactively #'org-roam-capture)))

N.B. You can read the docstring of org-capture-templates. Org roam capture process is built on top of Org capture, and the template-expanding functionality - the wildcards - should work identically. Only the things specific to Org roam are documented in the manual and internal docstrings, for example, the :head and the :file-name sections of the templates. If something works in org-capture and does not work/works diffferently in org-roam-capture or vice versa, double check your configuration. It could be just that your templates diverged in the course of experimentation.

1 Like

Setting org-link-context-for-files to nil replicated the behavior I was originally expecting. Thank you for this as for the wrapper function.

1 Like