25

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
    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 Oct 25 '14 at 18:56
19

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.

  • 11
    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 Oct 25 '14 at 19:00
  • 11
    It is simply polite to add attribution when you use someone's else information. – nicael Oct 25 '14 at 19:15
  • 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, you must acknowledge your sources. – Gilles Oct 26 '14 at 14:50
  • 1
    Note the link to the source changed. I edited it in the answer. – smonff Jul 20 '17 at 10:24
3

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.

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

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
M-x toggle-fill-unfill

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

(global-set-key (kbd "<f8>") 'toggle-fill-unfill)
  • Can you provide more information about this @easyE ? – smonff Sep 27 '17 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 Oct 3 '17 at 7:39
  • @easyE I don't get the difference between your comment and the content of my answer, sorry. – smonff Oct 3 '17 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 Oct 4 '17 at 10:45
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 !

0

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

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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