When using Emacs (version 24.1 if that makes any difference), I would like to have a way to remove all the automatic line breaking inserted by the auto-fill minor mode.

Disabling auto-fill-mode is enough to avoid inserting new automatic line breaks, but does nothing about cancelling the existing ones.

I found a way to do what I want, but I wonder if it's the best way to go about it. I'm describing it here with the hope that it'll help explain my issue better:

  • C-u 1000 C-x f: Set the current-fill-column to an arbitrary large value.
  • C-x h: Select all text in buffer
  • M-q: Re-arrange line breaks according to the new current-fill-column value. If the value is large enough, this will emulate the behavior I'm looking for.
  • M-x auto-fill-mode: Disable Auto Fill mode.

I wonder if this is a good way to go about it or whether there's a better way.


You could do a search-and-replace:

(Prompt: Query replace: ) C-q C-j Enter
(Prompt: Query replace  with: )Enter

Emacs will now start replacing every line break with nothing. If you want to get rid of all of them, press ! . If you want to verify every deletion, keep pressing y or n as appropriate.

  • not exactly what I'm looking for. I don't want to replace every line break, only the ones inserted automatically by auto-fill-mode. Going through each line break and deciding y or n does not seem like a good answer either. But thanks for showing me M-% I didn't know that :) – rahmu Aug 31 '12 at 9:56
  • When they're not inserted by auto-fill-mode, I'm guessing there are two breaks in sequence? If so, start by replacing two breaks with a character sequence that's not in your document, e.g. "BRBR". Then replace all character breaks with nothing, and then replace all BRBR with two character breaks again. (Yes, I've done this on occasion...) – Jenny D Aug 31 '12 at 10:00

The line breaks inserted by auto-fill are not different from line breaks inserted by other methods (typed, copy-pasted, from search-and-replace, …). From what you describe, it looks like what you're trying to do is replace single line breaks by spaces, and leave multiple consecutive line breaks alone. You can do that with a regexp replacement, but it isn't much simpler that what you've been doing. Call M-x replace-regexp (or M-%) to replace \(.\)^J\(.\) by \1 \2 (where ^J is a literal newline character, which you can insert by typing C-q C-j. This will miss a few newlines if you have lines containing a single character, run the command a second time to replace these.

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.