Search code examples
emacs

Inverse of M-q, an unfill-paragraph-function


Is there an inverse for M-q, some kind of unfill-paragraph-function?

If I have undo data, then it's of course easy. What I am asking for is instead the ability to merge lines in a paragraph into a single long line, right after I have just read the file from disk. This would make it possible to then paste the text into a form (a web form and the like) that is expecting a single linebreak for each paragraph.

In the past I have turned off auto-fill, created a macro to delete an EOL and move to the next line, and applied it repeatedly, but this is getting tiring.


Solution

  • Here's the answer. In short:

    (defun unfill-paragraph ()
      "Replace newline chars in current paragraph by single spaces.
    This command does the reverse of `fill-paragraph'."
      (interactive)
      (let ((fill-column 90002000))
        (fill-paragraph nil)))
     
    (defun unfill-region (start end)
      "Replace newline chars in region by single spaces.
    This command does the reverse of `fill-region'."
      (interactive "r")
      (let ((fill-column 90002000))
        (fill-region start end))) 
    

    Update: I've packaged this up here and it can be installed from Marmalade or Melpa.