Emacs Lisp: Add Period to Line Ends

By Xah Lee. Date: . Last updated: .

This command add a period to each end of line in current text lock. This is a useful when you copy text from discord or twitter to your blog.

(defun xah-add-period-to-line-end ()
  "Add a period to each end of line that does not have one.
Work on current paragraph if there is no selection.

URL `http://xahlee.info/emacs/emacs/emacs_period_to_line_end.html'
Version 2020-11-25 2021-07-04"
  (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")
        (backward-char )
        (setq $p2 (point))))
    (save-restriction
      (narrow-to-region $p1 $p2)
      (goto-char (point-min))
      (while (search-forward "\n" nil "move" )
        (backward-char )
        (let ( (charX (char-before )))
          (if (or (eq charX ?\. ) (eq charX ?! ) (eq charX ?? ) (eq charX ?\n ) (eq charX ?> ))
              nil
            (insert ".")))
        (forward-char )))))

The following are often useful together:

ErgoEmacs mascot-s276x226
Buy Xah Emacs Tutorial

Elisp Examples

Text Transform Under Cursor

Commands Do thing-at-point

Command to Insert Things

Script Examples

Misc