# How to have a sequence of more than two newlines in a capture template?

**URL:** <https://org-roam.discourse.group/t/how-to-have-a-sequence-of-more-than-two-newlines-in-a-capture-template/3555>\
**Category:** Troubleshooting\
**Created:** [August 10, 2024, 6:34pm UTC](https://org-roam.discourse.group/t/how-to-have-a-sequence-of-more-than-two-newlines-in-a-capture-template/3555 "2024-08-10T18:34:43Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![wilya](https://avatars.discourse-cdn.com/v4/letter/w/94ad74/32.png) [@wilya](https://org-roam.discourse.group/u/wilya)\
**Post date:** [August 16, 2024, 2:20pm UTC](https://org-roam.discourse.group/t/how-to-have-a-sequence-of-more-than-two-newlines-in-a-capture-template/3555/2 "2024-08-16T14:20:03Z")

</div>

From what I can understand of the code (with the help of Google AI Gemini), the current behavior of `org-roam-capture` collapses multiple consecutive newlines into a single one, except when they occur at the very end of the template.

The primary function responsible for this behavior is `org-roam-capture--fill-template`:

```auto
(defun org-roam-capture--fill-template (template &optional ensure-newline)
  ;; ... (other parts of the function) ...
  
  (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)

```

### Code analysis

`(replace-regexp-in-string "[\n]*\\'" "" ...)`: This line is the culprit for collapsing multiple newlines. It uses a regular expression to find any sequence of zero or more newlines followed by a single quote and replaces it with an empty string. This effectively removes any extra newlines that might have been present within the template, except for those at the very end.

`(when (and ensure-newline ...))`: This part ensures that if the ensure-newline argument is provided and there’s no trailing whitespace in the original template, a single newline is appended. If the original template had trailing whitespace (spaces, tabs, or newlines), that whitespace is preserved in `template-whitespace-content` and appended back to the final template.

In other words:

- **In-between whitespace** (multiple newlines within the template body): These newlines are collapsed into a single newline due to the replace-regexp-in-string operation.

- **At-the-end whitespace** (newlines at the very end of the template): These newlines are preserved due to the ensure-newline logic, which appends a newline only if there’s no existing trailing whitespace. This specific code was changed after the following [Bug report](https://github.com/org-roam/org-roam/issues/2115). Related: [Multiple blank lines in head of capture template](https://org-roam.discourse.group/t/multiple-blank-lines-in-head-of-capture-template/2488)

---

_[View the full topic](https://org-roam.discourse.group/t/how-to-have-a-sequence-of-more-than-two-newlines-in-a-capture-template/3555)._
