Multiple blank lines in head of capture template

I’m trying to have my capture templates insert an extra line before the content of the entry and the head. From what I understand after looking at the definition of org-roam-capture--fill-template, when using the template line breaks are essentially ignored. So, effectively, these two bits of code are treated the same

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

Has anyone managed to find a way around this? (I don’t want to insert a bunch of blank lines after capture, so in general I’m OK with the default behavior in most situations, but I’d like the ability to have that extra space after the header (or is is the head?)

1 Like

Use \n to insert a line break, so \n\n would insert a blank line.

@apc This indeed doesn’t work, line breaks \n are ignored at the end of the header. I am having the same problem. I also tried adding a blank line at the start of the template file, but it is ignored as well. Any ideas?
Edit: corrected typo.

Refer to the comments and following lines of code around here.

I believe you’d need to override the function locally (I am not certain which line exactly). I’d edebug and see how the code removes the additional empty lines added in the template.

Have looked at the code comments. It makes sense to strip trailing new lines from the header, but I would suggest not touching anything people define in the template file; i.e. leave that up to the user.

I suggest you report this to the project as an issue.

Done, see here. Thanks!

2 Likes

I know this thread is old, and it seems to be fixed, but I found it while trying to overcome the same problem of inserting a blank line between the ‘head’ and the cursor.

So far, all my attempts have failed, I followed the link to github you provided, and tried to look at the commit which is supposed to have fixed the issue where I can see a reassuring (insert (org-roam-capture--fill-template head 'ensure-newline))) added to the code.

Since inserting \n or \n\n has no effect on my end, I looked for some ensure-newline or fill-template kind of variable but did not find any. I cannot find anything related in the org-roam-capture-template help.

Is there anything in particular to do to prevent blank lines from being stripped off the org-roam-templates ?

See my reply in a new thread: How to have a sequence of more than two newlines in a capture template? - #2 by wilya

TLDR: Only trailing whitespace are preserved.

To answer your question, if you comment out this line:

(defun org-roam-capture--fill-template (template &optional ensure-newline)
  ;; ... (other parts of the function) ...
  
  ;; Comment out or remove the following line to preserve newlines
  ;; (setq template (replace-regexp-in-string "[\n]*\\'" "" (org-capture-fill-template template))) 

  (when (and ensure-newline               
             (string-equal template-whitespace-content ""))
    (setq template-whitespace-content "\n"))
  (setq template (concat template template-whitespace-content))
  template)

it should change the behavior of the function. However, I have no idea if this could have unpleasant second order effects (also called bugs).

You can redefine the org-roam-capture--fill-template function in your Emacs configuration file.

(defun org-roam-capture--fill-template (template &optional ensure-newline)
  ;; ... (copy the original function definition here) ...

  ;; Comment out or remove the newline collapsing line
  ;; (setq template (replace-regexp-in-string "[\n]*\\'" "" (org-capture-fill-template template))) 

  ;; ... (rest of the original function definition) ...
)

This approach effectively overrides the original function definition with your modified version. Keep in mind that when redefining functions from packages, it can lead to conflicts if the package’s implementation changes in the future.

1 Like

I could not get around to it earlier, but thank you for taking the time to provide these detailled additional explanations.