1 (defun remove-single-newlines ()
2 ;; Desc: Replace all single newlines with spaces.
4 "Remove single newlines, joining lines that are separated by a single newline, while preserving paragraphs separated by multiple newlines."
6 (let ((start (point-min)) (end (point-max)))
9 (while (re-search-forward "\\([^\n]\\)\n\\([^\n]\\)" end t)
10 ;; Check if the newline is followed by another newline (indicating a paragraph break)
11 (unless (looking-at-p "\n")
12 (replace-match "\\1 \\2" nil nil))))))
14 (defun remove-single-newlines-region (start end)
15 ;; Desc: Replace all single newlines in a selected region with spaces.
17 "Remove single newlines in the selected region, joining lines separated by a single newline, while preserving paragraphs separated by multiple newlines."
18 (interactive "r") ; "r" means this function should use the region start and end as arguments when called interactively
21 (while (re-search-forward "\\([^\n]\\)\n\\([^\n]\\)" end t)
22 ;; Check if the newline is followed by another newline (indicating a paragraph break)
23 (unless (looking-at-p "\n")
24 (replace-match "\\1 \\2" nil nil)))))