Emacs: Hard Wrap Lines, fill-paragraph, unfill-paragraph

By Xah Lee. Date: . Last updated: .
emacs fill-paragraph
emacs M-x fill-paragraph

Alt+x fill-paragraphAlt+q】 to break a long line into multiple lines.

Alt+x fill-region to break lines in a text selection.

Alt+x set-fill-columnCtrl+x f】 to set the max characters per line used by “fill” commands.

You can call ruler-mode to turn on ruler while trying this.

Note: the fill commands break lines by inserting newline character at different places.

If you just want a visual margin, see:

auto-fill-mode

If you want to have lines cut as you type, Alt+x auto-fill-mode.

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://ergoemacs.org/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://ergoemacs.org/emacs/emacs_unfill-paragraph.html'
Version 2016-07-13"
  (interactive "r")
  (let ((fill-column most-positive-fixnum))
    (fill-region start end)))

You can define a keyboard shortcut for them. 〔►see Emacs: How to Define Keys

Auto Toggle Fill / Unfill / Region

Here's a even better command. It toggles fill or unfill, and automatically work on selection if there's one.

Emacs: fill-region, unfill-region Toggle

Reformat Lines for Source Code

Emacs: Reformat Lines for Source Code

Visual Line Wrap (Wrap at Margin)

Emacs: Visual Line Mode, Wrap Lines at Word Boundaries

Liket it? Put $5 at patreon. Or Buy Xah Emacs Tutorial. Thanks.