4

The commands fill-region (and fill-paragraph) breaks lines in a region (and paragraph) to a predefined width. How does one achieve the opposite effect?

Given a buffer, say markdown or org, where paragraphs are separated by two newlines, how does one unfill (remove non-paragraph related line breaks) regions and paragraphs?

There are some hacks to do this, however wanted to know what is the canonical way:

  1. Set the variable fill-column to a high value and run fill-paragraph.
  2. Use a custom macro.

4 Answers 4

8

Try M-x unfill-region and M-x unfill-paragraph. Their documentation says that they are the respective inverses of those two commands.

Edit:

Forgot that this is not built in. You want package unfill from MELPA, or at https://github.com/purcell/unfill

2
  • 1
    These functions aren't part of Emacs. Do you know where you got them?
    – Tyler
    Commented Jan 4, 2022 at 22:03
  • 1
    You're right! Sorry about that. Been using them so long I forgot they were extras. I'll edit the answer. Commented Jan 5, 2022 at 23:14
4

spacemacs has a nice design regarding this in better defaults layer. from it's manual:

Fill or unfill paragraph: Pressing M-q for the first time fills current paragraph and pressing M-q for the second time unfills it.

It achieves this by consulting the last-command variable.

Personly I just use evil's join(J) command, or Emacs's delete-indentation(M-^).

2

I've been using this from the Emacs wiki:

(defun my-unfill-paragraph (&optional region)
  "Make multi-line paragraph into a single line of text.

REGION unfills the region.  See URL
`https://www.emacswiki.org/emacs/UnfillParagraph'"
  (interactive (progn (barf-if-buffer-read-only) '(t)))
  (let ((fill-column (point-max))
        ;; This would override `fill-column' if it's an integer.
        (emacs-lisp-docstring-fill-column t))
    (fill-paragraph nil region)))

It works by setting the upper limit on the fill-column variable to the maximum point in the buffer. The fill-column variable controls the column beyond which automatic line-wrapping should happen. It also sets emacs-lisp-docstring-fill-column to t which means that the fill column for lisp docstrings should use the same fill value as that given by fill-column. In essence, this removes line-wrapping. Finally, it calls the fill command for the region or current paragraph.

1

I can't think of a quick built-in solution that works in all cases. For a single paragraph, you can:

  1. Mark the paragraph (mark-paragraph, M-h).
  2. Remove the beginning and ending line break from the region (right C-x C-x left).
  3. Replace line breaks by spaces (C-M-% C-q C-j RET SPC RET !).

This is unwieldy enough that setting fill-column to a very high value and running fill-region tends to be more convenient. Doing it for multiple paragraphs is even more unwieldy because there isn't a regular expression that matches line breaks that aren't paragraph breaks.

If you don't want to modify fill-column, you can:

  1. Create an indirect buffer: C-x 4 c
  2. Set fill-column in the indirect buffer: M-x set-variable RET fill-column RET 99999999 RET
  3. (Select what you want and) M-q
  4. Close the indirect buffer (C-x k RET C-x 0).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.