next up previous contents
Next: longlines.el Up: The Woodnotes Guide to Previous: Searching and Replacing   Contents

Word wrap (Filling)

Quite frankly, this is emacs' greatest disappointment as a text editor - something so fundamentally important to so many people and something so many other editors do well. Without modification, emacs does not soft wrap the way you'd like it to. Will that change? Perhaps - open source software's greatest strength is that clever people with programming skills can modify projects to suit their own needs. But if this is something you can't overlook, emacs will drive you crazy and you're better off with another application.

Word wrap comes in two forms: soft wrap and hard wrap. Hard wrap means that at the end of every line a "newline" character is inserted. Most plain text email is sent this way. If you have 80 character wide paragraphs and want them to be 120 characters wide, you have to reformat. In other text editors that's a real pain, but in emacs it's easy, and that's one reason few programmers feel compelled to do anything about the word wrap problem. Soft wrap means the program recognizes the width of the window on your screen and reformats the words to fit the window, without inserting any newline characters. If you resize the window, the words adjust automatically. Emacs does not do this.

Emacs presents three options:

Let's look at filling first. The command M-x auto-fill-mode toggles filling on or off. It will insert a newline at a certain position, taking care to pass a word onto the next line if it would be otherwise split. At what character will it do so? Probably around 72 unless you tell it otherwise. Here's how to choose: C-u 80 C-x f sets the width (80 characters, in this example) of your paragraph but does not reformat the paragraph. M-q reformats the paragraph.

So let's say you are typing at the console, which is 120 characters wide, and you are starting a new document. Before you start, hit C-u 120 C-x f to set the margin, and type M-x auto-fill-mode to toggle auto-fill mode on (check the status bar at the bottom of the screen to see if it's on: look for the word "fill" in the mode line). Now start typing. Your paragraphs will be hard wrapped at 120 characters, the width of your screen. Now if you go back to edit your work, the paragraph will be out of whack. Hit M-q to reformat the paragraph. If you later decide you want the paragraph to be 72 characters wide again, you can hit C-u 72 C-x f to set the new margin and M-q to reformat it.

There are two other useful commands available to you if you've selected a region you'd like to format. The command fill-individual paragraphs (remember, as explained in section 6.1 you would access this by typing M-x fill-individual-paragraphs) reformats each paragraph in the region. This is probably what you want if you want to globally change all the paragraphs in your document from 72 to 85 characters wide, for example. The command fill-region-as-paragraph will take all the fragments of text in your region and make them into a single paragraph, removing extraneous blank lines and double spaces, etc. Very handy way to reformat hacked-up text.



Subsections
next up previous contents
Next: longlines.el Up: The Woodnotes Guide to Previous: Searching and Replacing   Contents
Randall Wood 2007-07-04