Reformatting Paragraphs

If you’re in auto-fill mode, Emacs tries to make your lines as neat as possible by inserting line breaks so that each line is roughly the same length. Of course, this helps only when you’re writing the first draft. As you edit, you’ll make some lines longer and some shorter. Soon, you’ll no longer have a neatly formatted file. Depending on the editing you’ve done, some lines may be short and some may be so long that they no longer fit on your display.

Emacs won’t reformat your text of its own accord. If you want to restore your file to its original beauty, you need to give a fill command. The simplest way to reformat your text is to give the fill-paragraph command by typing ESC q. Emacs reformats the paragraph, then positions the cursor at the end of the paragraph.

There’s one important pitfall here. In text mode, a paragraph is any text that is indented or has a blank line before and after it. If you have a file with no blank lines, Emacs thinks it is all one long paragraph. Typing ESC q by mistake takes all the text, ignoring line breaks, and makes it one long paragraph. This outcome is a particular problem if you use the nroff, troff, or TEX text-formatting systems. Luckily, pressing C-x u (for undo) will magically put things back the way they were. If you use troff or TEX or if (for any other reason) you regularly create files with no blank lines, here are some suggestions:

  • For nroff or troff, use nroff mode, described in Chapter 9. You can reformat ...

Get Learning GNU Emacs, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.