Emacs: Reformat Line Wrap
Emacs has a command
fill-paragraph 【Alt+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
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 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://xahlee.info/emacs/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
- Simple Changes Emacs Should Adopt
- Why Emacs Keys are Painful
- Ban Scratch Buffer
- M-x vs Alt+x Notation
- Menu Idiocy
- Mode Line Problem
- cua-mode Problem
- Inconsistency of Search
- grep in emacs Pain
- Problems of describe-mode
- Problems of Emacs Manual
- Emacs Manual Sucks by Examples
- kill-buffer Problem
- Emacs Spell Checker Pain
- Form Feed ^L
- Single Key Delete Whole Line
- Emacs HTML Mode Sucks
- Emacs No View Image on Windows
- HTML should replace Texinfo
- Support HTML Mail
- Problems of “man”
- Emacs Lisp Mode Syntax Coloring Problem
- Emacs AHK Mode Problems
- Ban Syntax Table
- Make elisp-index-search use Current Symbol
- Texinfo Invalid HTML
- Disappearing FSF URLs, 2006
- Emacs Manual Node Persistency Issues
- Emacs: dired-do-query-replace-regex Replace ALL (fixed)
- Problems of Emacs Supporting Obsolete Systems
- Elisp: Function to Copy/Delete a Dir Recursively (fixed)
- CommonLisp/SchemeLisp Emacs
- Text Editors Popularity
- Cursor Movement Behavior Survey
- Letter-Case Commands Problem
- Select Line/Block/Quote, Extend
- isearch Current Word
- Reformat Line Wrap