Emacs: Toggle Line Wrap

By Xah Lee. Date: . Last updated: .

Emacs has a command fill-paragraphAlt+q】 that does hard-wrap of current paragraph (it inserts a newline char at every ~70 chars.) Emacs also has fill-region, which acts on a text selection. However, there are some usability problems with these commands. This page discuss the problems and fixes.

One frequently asked question is how to “unfill” — do inverse of fill-paragraph. Technically, this means replacing newline char by space. Emacs does not have a built-in command for this. The typical answer to this question, is to set the cut-width to a huge number (For example, set fill-column to 10001000), then Alt+x fill-paragraph, then set fill-column back to previous value. This is inconvenient.

If you need to reformat several paragraphs, Alt+x fill-region. There is no keyboard shortcut.

Auto Toggle Fill / Unfill / Region

In the following, we suggest a command to automatically toggle “fill” or “unfill” on the current paragraph. If there is a text selection, then the command automatically works on the region.

Here is a implementation:

(defun xah-fill-or-unfill ()
  "Reformat current paragraph or region to `fill-column', like `fill-paragraph' or “unfill”.
When there is a text selection, act on the selection, else, act on a text block separated by blank lines.
URL `http://ergoemacs.org/emacs/modernization_fill-paragraph.html'
Version 2017-01-08"
  (interactive)
  ;; This command symbol has a property “'compact-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 ( ($compact-p
          (if (eq last-command this-command)
              (get this-command 'compact-p)
            (> (- (line-end-position) (line-beginning-position)) fill-column)))
         (deactivate-mark nil)
         ($blanks-regex "\n[ \t]*\n")
         $p1 $p2
         )
    (if (use-region-p)
        (progn (setq $p1 (region-beginning))
               (setq $p2 (region-end)))
      (save-excursion
        (if (re-search-backward $blanks-regex nil "NOERROR")
            (progn (re-search-forward $blanks-regex)
                   (setq $p1 (point)))
          (setq $p1 (point)))
        (if (re-search-forward $blanks-regex nil "NOERROR")
            (progn (re-search-backward $blanks-regex)
                   (setq $p2 (point)))
          (setq $p2 (point)))))
    (if $compact-p
        (fill-region $p1 $p2)
      (let ((fill-column most-positive-fixnum ))
        (fill-region $p1 $p2)))
    (put this-command 'compact-p (not $compact-p))))

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

This command is part of ErgoEmacs Keybinding and Emacs: Xah Fly Keys.

Reformat Lines for Source Code

Here's a different implementation that does not use emacs's fill-region commands. This is useful if you want to use it on programing language source code.

Emacs: Reformat Lines for Source Code

Line Wrap Topic

  1. Hard Wrap Lines
  2. Toggle Line Wrap
  3. Reformat Lines for Source Code
  4. Visual Line Mode, Wrap Lines
  5. Novel Reading Mode
  6. toggle-word-wrap

Emacs Text Transform Under Cursor

  1. Toggle Letter Case
  2. Title Case
  3. Upcase Sentences
  4. Cycle Space Hyphen Underscore
  5. Escape Quotes
  6. Quote Lines
  7. Spaces to New Lines
  8. Change Brackets/Quotes
  9. Remove Accent Marks
  10. Convert Straight/Curly Quotes
  11. Convert English/Chinese Punctuations
  12. Color Conversion (RGB, HSL, HSV)
  13. Decimal to Hexadecimal
  14. Replace Greek Letter Names to Unicode
  15. Twitterfy Text
  16. Toggle line wrap
  17. Clean White Space

Emacs Modernization

  1. Simple Changes Emacs Should Adopt
  2. Why Emacs Keys are Painful
  3. Problems of the Scratch Buffer
  4. M-key Notation vs Alt+key Notation
  5. Menu Problem
  6. Mode Line Problem
  7. cua-mode Problems
  8. kill-buffer Induces Buffer Accumulation
  9. Emacs Form Feed ^L
  10. Inconsistency of Search Features
  11. Single Key to Delete Whole Line
  12. Emacs HTML Mode Sucks
  13. Emacs Does Not Support Viewing Images Files In Windows
  14. Emacs Spell Checker Problems
  15. Adopt HTML as Texinfo Replacement
  16. Support HTML Mail
  17. Problems of “man” Command
  18. Emacs Lisp Mode Syntax Coloring Problem
  19. Emacs Ahk Mode Problems
  20. Problems of Emacs's Manual
  21. Problems of Emacs's Manual; Examples
  22. Emacs: Have You Read Emacs Manual?
  23. Elisp: Ban Syntax Table
  24. Emacs: Make elisp-index-search use Current Symbol
  25. Emacs: Usability Problems of Mode Documentation
  26. Emacs GNU Texinfo Problems; Invalid HTML
  27. A Record of Frustration in IT Industry; Disappearing FSF URLs, 2006
  28. Emacs Manual Node Persistency Issues
  29. Emacs: dired-do-query-replace-regex Replace ALL (fixed)
  30. Problems of Emacs Supporting Obsolete Systems
  31. Elisp: Function to Copy/Delete a Dir Recursively (fixed)
  32. Thoughts on Common Lisp Scheme Lisp Based Emacs
  33. Text Editors Popularity and Market Research
  34. Text Editor's Cursor Movement Behavior (emacs, vi, Notepad++)
  35. GNU Emacs Development Inefficiency
  36. Emacs Dev Inefficiency and Emacs Web 2.0?
  37. Letter-Case Commands Usability Problems
  38. Emacs: Select Line, between Quotes, Extend Selection
  39. Emacs: isearch Current Word
  40. Suggestions on Line Wrap Commands
  41. Emacs: Single Key to Delete Whole Line
  42. Emacs Undo and Emacs Cult Problem

If you have a question, put $5 at patreon and message me.
Or Buy Xah Emacs Tutorial
Or buy a nice keyboard: Best Keyboards for Emacs

Emacs

Emacs Lisp

Misc