34

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?

Malabarba
  • 22,878
  • 6
  • 78
  • 163
Sibi
  • 3,603
  • 2
  • 22
  • 35
  • 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`](http://www.gnu.org/software/emacs/manual/html_node/emacs/Fill-Commands.html). – Drew Oct 25 '14 at 18:56

6 Answers6

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.

smonff
  • 1,575
  • 1
  • 15
  • 20
programking
  • 7,064
  • 9
  • 41
  • 62
  • 16
    This is a straight copy from [Emacs Wiki page UnfillParagraph](http://www.emacswiki.org/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 Oct 25 '14 at 19:00
  • @Drew, yep. Is that a problem? If you want I will delete my answer. – programking Oct 25 '14 at 19:00
  • 14
    It is simply polite to add attribution when you use someone's else information. – nicael Oct 25 '14 at 19:15
  • Thank you @nicael, I will be sure to do that in the future. – programking Oct 25 '14 at 19:26
  • 2
    Thanks to Malabarba for making the changes. @KingShimkus: nicael explained why this is better. – Drew Oct 25 '14 at 22:08
  • 2
    If you [post content written by someone else](http://emacs.stackexchange.com/help/referencing), you must acknowledge your sources. – Gilles 'SO- stop being evil' Oct 26 '14 at 14:50
  • 1
    Note the [link to the source](https://www.emacswiki.org/emacs/UnfillParagraph) changed. I edited it in the answer. – smonff Jul 20 '17 at 10:24
  • I've used this command happily for many years, but just now noticed a problem with it: if the first line in the region to be converted to a single line starts with a hyphen, then, if the first non-whitespace character of any of the following lines is a hyphen, then the latter hyphens disappear. – Teemu Leisti Dec 11 '18 at 13:41
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.

Rajish
  • 191
  • 4
  • AFAIK, you can do `M-^` and you need only hold the key down. The function is named `delete-indentation`. – rasmus Oct 26 '14 at 01:28
  • What is the function name for `M-S-^`, it doesn't seem to work for me ? – Sibi Oct 26 '14 at 07: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 Oct 26 '14 at 23:56
7

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)
Glorfindel
  • 234
  • 1
  • 5
  • 13
smonff
  • 1,575
  • 1
  • 15
  • 20
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 !

itsjeyd
  • 14,586
  • 3
  • 58
  • 87
1

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.)

mbork
  • 1,647
  • 1
  • 13
  • 23
  • Actually a very good solution without needing a package as it retains some wanted paragraph filling logic Emacs provides. – JohnDoe May 17 '23 at 09:31
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.

Cubic
  • 103
  • 4
Lenar Hoyt
  • 1,163
  • 7
  • 25
  • After the initial `J` press, you can also press `.` to repeat the previous command instead of having to hold down that pesky Shift. ;) – Lorem Ipsum Jan 29 '20 at 18:42