HomeMathComputingArtsWordsLiteratureMusictwitter facebook webfeed

Emacs unfill-paragraph, unfill-region, compact-uncompact-block

Advertise Here For Profit

Xah Lee, 2010-05-12

Emacs has a fill-paragraph command. The command add line breaks in the current paragraph, so that each line is no more than about 70 chars. However, there's no unfill-paragraph — a command that does the reverse. Here's the solution.

unfill-paragraph, unfill-region

(defun unfill-paragraph ()
  "Replace newline chars in current paragraph by single spaces.
This command does the reverse of `fill-paragraph'."
  (interactive)
  (let ((fill-column 90002000))
    (fill-paragraph nil)))

(defun unfill-region (start end)
  "Replace newline chars in region by single spaces.
This command does the reverse of `fill-region'."
  (interactive "r")
  (let ((fill-column 90002000))
    (fill-region start end)))

You can define a keyboard shortcut for them.

The above code works by setting the variable “fill-column” to a large value then call fill-paragraph to do the work.

compact-uncompact-block

A even better command is compact-uncompact-block. It automatically “fill” or “unfill” depending on whether the current paragraph is already “filled”. Also, it automatically works on the current text selection if there's one, so you don't need to choose between “*-paragraph” or “-region” versions.

For code, see: Suggestions on Emacs's Line-Cutting Commands.

blog comments powered by Disqus