Emacs: Reformat to Long Lines (unfill-region) 💠

By Xah Lee. Date: . Last updated: .

unfill-paragraph, unfill-region

Emacs does not have a “unfill-paragraph” command to do the inverse of “fill”. Here's the solution:

(defun xah-unfill-paragraph ()
  "Replace newline chars in current paragraph by single spaces.
This command does the inverse of `fill-paragraph'.

URL `http://xahlee.info/emacs/emacs/emacs_unfill-paragraph.html'
Version 2016-07-13"
  (interactive)
  (let ((fill-column most-positive-fixnum))
    (fill-paragraph)))
(defun xah-unfill-region (start end)
  "Replace newline chars in region by single spaces.
This command does the inverse of `fill-region'.

URL `http://xahlee.info/emacs/emacs/emacs_unfill-paragraph.html'
Version 2016-07-13"
  (interactive "r")
  (let ((fill-column most-positive-fixnum))
    (fill-region start end)))

toggle “fill” or “unfill”

This command automatically toggle “fill” or “unfill” on the current paragraph. If there is a text selection, then the command automatically works on the region.

(defun xah-fill-or-unfill ()
  "Reformat current block or selection to short/long line.
First call will break into multiple short lines. Repeated call toggles between short and long lines.
This commands calls `fill-region' to do its work. Set `fill-column' for short line length.

URL `http://xahlee.info/emacs/emacs/modernization_fill-paragraph.html'
Created: 2020-11-22
Version: 2025-03-25"
  (interactive)
  ;; This command symbol has a property “'longline-p”, the possible values are t and nil. This property is used to easily determine whether to compact or uncompact, when this command is called again
  (let ( (xisLongline (if (eq last-command this-command) (get this-command 'longline-p) t))
         (deactivate-mark nil)
         xbeg xend )
    (seq-setq (xbeg xend) (if (region-active-p) (list (region-beginning) (region-end)) (list (save-excursion (if (re-search-backward "\n[ \t]*\n" nil 1) (match-end 0) (point))) (save-excursion (if (re-search-forward "\n[ \t]*\n" nil 1) (match-beginning 0) (point))))))
    (if xisLongline
        (fill-region xbeg xend)
      (let ((fill-column 99999 ))
        (fill-region xbeg xend)))
    (put this-command 'longline-p (not xisLongline))))

Note: an early implementation is named “compact-uncompact-block”.

Emacs Lines, Column, Cursor Position

Soft-Wrap Lines

Reformat Lines

Show Line Number, Column Number

Highlight Current Line, Screen Line