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
  • 23,148
  • 6
  • 79
  • 164
Sibi
  • 3,683
  • 2
  • 24
  • 35

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,579
  • 1
  • 15
  • 20
programking
  • 7,194
  • 10
  • 42
  • 63
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,579
  • 1
  • 15
  • 20
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.)

mbork
  • 1,667
  • 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

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,666
  • 3
  • 59
  • 87
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,173
  • 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