35

Emacs has the function fill-paragraph. Is there any function which will do the opposite of that?

I have a paragraph which is already filled and instead I want it in a plain single line?

1
  • 1
    fill-paragraph does not justify the paragraph, unless you use a prefix argument or pass it a non-nil first argument. See (emacs) Fill Commands.
    – Drew
    Commented Oct 25, 2014 at 18:56

7 Answers 7

25

Quoting from Emacs Wiki, by Stefan Monnier:

Unfilling a paragraph joins all the lines in a paragraph into a single line. It is the contrary of FillParagraph.

It works where a line ends with a newline character (”\n”) and paragraphs are separated by blank lines. To make a paragraph end in a single newline then use the function below:

;;; It is the opposite of fill-paragraph    
(defun unfill-paragraph ()
  "Takes a multi-line paragraph and makes it into a single line of text."
  (interactive)
  (let ((fill-column (point-max)))
    (fill-paragraph nil)))

And to bind it to a key:

 ;; Handy key definition
 (define-key global-map "\M-Q" 'unfill-paragraph)

See also UnfillRegion, and UnwrapLine.

8
  • 18
    This is a straight copy from Emacs Wiki page UnfillParagraph, with the author (Stefan Monnier) attribution removed. It is helpful to post the info here, but at least make clear who wrote the code and link to that wiki page.
    – Drew
    Commented Oct 25, 2014 at 19:00
  • 15
    It is simply polite to add attribution when you use someone's else information.
    – nicael
    Commented Oct 25, 2014 at 19:15
  • 2
    Thanks to Malabarba for making the changes. @KingShimkus: nicael explained why this is better.
    – Drew
    Commented Oct 25, 2014 at 22:08
  • 2
    If you post content written by someone else, you must acknowledge your sources. Commented Oct 26, 2014 at 14:50
  • 1
    Note the link to the source changed. I edited it in the answer.
    – smonff
    Commented Jul 20, 2017 at 10:24
9

My method would be placing the cursor in the last line of the paragraph and hitting M-^ several times.

The beauty of this shortcut is that beside joining lines it reduces any amount of indentation spaces into single one.

3
  • AFAIK, you can do M-^ and you need only hold the key down. The function is named delete-indentation.
    – rasmus
    Commented Oct 26, 2014 at 1:28
  • What is the function name for M-S-^, it doesn't seem to work for me ?
    – Sibi
    Commented Oct 26, 2014 at 7:09
  • 1
    Yes the function is delete-indentation and the shortcut is described in Emacs as M-^ - the shift (S) was added by me, because you have to hold it anyway. Sorry for confusion.
    – Rajish
    Commented Oct 26, 2014 at 23:56
8

There is the unfill package for this now.

Provides commands for explicitly unfilling (ie. unwrapping) paragraphs and regions, and also a command that will toggle between filling and unfilling the current paragraph or region.

It is based initially on Xah Lee's examples and later rewritten based on an article by Artur Malabarba

It provide the following:

M-x unfill-region
M-x unfill-paragraph
M-x unfill-toggle

It's convenient to add an handy keybinging of your choice like:

;; Allow to fill or unfill with a single keybinding 
;; depending of the content status
(global-set-key (kbd "<f8>") 'unfill-toggle)
6
  • Can you provide more information about this @easyE ?
    – smonff
    Commented Sep 27, 2017 at 15:32
  • 1
    My mistake: the code based on Xah Lee's examples has not been incorporated as part of Emacs, but [is included as a MELPA package]( melpa.org/#/unfill).
    – easyE
    Commented Oct 3, 2017 at 7:39
  • @easyE I don't get the difference between your comment and the content of my answer, sorry.
    – smonff
    Commented Oct 3, 2017 at 9:41
  • I haven't argued that this is a difference. I was mistaken in thinking the unfill pacakge had been included in Emacs 25, and then I corrected myself.
    – easyE
    Commented Oct 4, 2017 at 10:45
  • 1
    Nice one. This package really should be built-in in Emacs. Commented Feb 1, 2023 at 16:06
2

A poor man's unfill can also be done by first setting the fill-column to some ridiculously high value (I use 9999) and then filling. For instance, C-u 9999 C-x f M-q C-u 70 C-x f. (Also, you might prefer C-9 C-9 C-9 C-9 to C-u 9999.)

1
  • Actually a very good solution without needing a package as it retains some wanted paragraph filling logic Emacs provides.
    – JohnDoe
    Commented May 17, 2023 at 9:31
1

As always, there are several ways to do this, so I'll throw another answer in the ring:

  1. Mark paragraph: M-h

  2. Query replace newline with space in region:

    M-% C-q C-j RET SPC RET !

1

In Evil mode there is the J key binding in normal mode from VIM. It joins the line below and the current line into one line. So by pressing it multiple times, you can undo fill-paragraph.

1
  • After the initial J press, you can also press . to repeat the previous command instead of having to hold down that pesky Shift. ;) Commented Jan 29, 2020 at 18:42
0

Finally found a super easy way:

  1. Select entire paragraph, eg C-x h
  2. Link up lines into one with M-^

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.