Emacs: Hard Wrap Lines

By Xah Lee. Date: . Last updated: .

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.

emacs fill-paragraph
emacs M-x fill-paragraph

Alt+x 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)))

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: Toggle Line Wrap

Reformat Lines for Source Code

Emacs: Reformat Lines for Source Code

Visual Line Wrap (Wrap at Margin)

Emacs: Visual Line Mode, Wrap Lines

Line Wrap Topic

  1. Emacs: Hard Wrap Lines
  2. Emacs: Toggle Line Wrap
  3. Emacs: Reformat Lines for Source Code
  4. Emacs: Visual Line Mode, Wrap Lines
  5. Emacs: Novel Reading Mode
  6. Emacs: toggle-word-wrap
Like my tutorial? Put $5 at patreon

Or Buy Xah Emacs Tutorial

Or buy a nice keyboard: Best Keyboard for Emacs

Ask me question on patreon