Emacs Lisp: Reformat to Sentence Lines 🚀

By Xah Lee. Date: . Last updated: .

This command breaks a long line or text block into multile lines, each line is a sentence, ending in a period.

This is useful for editing a thesis or long prose. Because it lets you clearly see the sentence structure.

put this in your Emacs Init File:

(defun xah-reformat-to-sentence-lines ()
  "Break a long line or text block into multiple lines by ending period.
Work on text selection if there is one, else the current text block.
URL `http://xahlee.info/emacs/emacs/elisp_reformat_to_sentence_lines.html'
Version 2020-12-02 2021-04-14 2021-08-01"
  (interactive)
  (let ($p1 $p2)
    (if (use-region-p)
        (setq $p1 (region-beginning) $p2 (region-end))
      (progn
        (if (re-search-backward "\n[ \t]*\n+" nil "move")
            (progn (re-search-forward "\n[ \t]*\n+")
                   (setq $p1 (point)))
          (setq $p1 (point)))
        (re-search-forward "\n[ \t]*\n" nil "move")
        (setq $p2 (point))))
    (save-restriction
      (narrow-to-region $p1 $p2)
      (progn (goto-char (point-min)) (while (search-forward "\n" nil t) (replace-match " " )))
      (progn (goto-char (point-min)) (while (re-search-forward "  +" nil t) (replace-match " " )))
      (progn (goto-char (point-min)) (while (re-search-forward "\\. +\\([0-9A-Za-z]+\\)" nil t) (replace-match ".\n\\1" )))
      (progn (goto-char (point-min)) (while (search-forward " <a " nil t) (replace-match "\n<a " )))
      (progn (goto-char (point-min)) (while (search-forward "</a>" nil t) (replace-match "</a>\n" )))
      (goto-char (point-max))
      (while (eq (char-before ) 32) (delete-char -1))
      (insert "\n\n"))))

The following are often useful together: