Word-wrap (filling) in Emacs

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:

No wrap: Lines do not wrap around the screen at all, but continue on and on to the right until you finally hit “return,” which for text, would be at the end of the paragraph. This is acceptable for people writing code, but not acceptable for text.
* Wrap: Lines wrap around the screen, but emacs doesn’t pay attention to words, and will wrap right in the middle of the word, showing a little symbol, probably a backslash, at the right edge of the screen to show the line is being continued below. If you can deal with this, it’s the best way to go for writers. But it’s not ideal.
Fill: Emacs calls it “filling” a paragraph, and it means inserting a newline at a certain distance from the beginning of the line, for example 120 characters, paying attention not to split a word. This gives you nicely formatted paragraphs that look nice, but can get distorted if you add words in the middle of the paragraph afterward, for example. Then you have to reformat the paragraph (which is a simple keystroke). If you’re writing using LATEX (more about that later), this is the best option. But if you eventually want to hand the text to a publisher, they’ll be very unhappy about all the newlines and the hard formatting, so you’ll be forced to find an alternative.

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. ## Word Wrap in EMACS Add the following commands to emacs.rc in each user’s root directory to make Emacs use the cursorpad and do word wrap. You may wish to use 65 as a wrap value or even 78 (not 40). bind-to-key quick-exit ^Z bind-to-key previous-line FNA bind-to-key next-line FNB bind-to-key forward-character FNC bind-to-key backward-character FND 40 set-fill-column add-global-mode “wrap”

Word-wrap in Emacs is called filling.

The Emacs Manual tells you this about it: M-q Fill current paragraph (fill-paragraph). C-x f Set the fill column (set-fill-column). M-x fill-region Fill each paragraph in the region (fill-region). M-x fill-region-as-paragraph Fill the region, considering it as one paragraph. M-s Center a line. But of course it doesn’t tell you how to actually use C-x f. If you just type C-x f you get this error: set-fill-column requires an explicit argument To set the line wrap to 80 for example, type C-u 80 <RET> C-x f <RET>

Continuation Lines

If you add too many characters to one line without breaking it with RET, the line grows to occupy two (or more) lines on the screen. On graphical displays, Emacs indicates line wrapping with small bent arrows in the fringes to the left and right of the window. On text-only terminals, Emacs displays a \ character at the right margin of a screen line if it is not the last in its text line. This \ character says that the following screen line is not really a distinct line in the text, just a continuation of a line too long to fit the screen. Continuation is also called line wrapping. When line wrapping occurs before a character that is wider than one column, some columns at the end of the previous screen line may be “empty.” In this case, Emacs displays additional \ characters in the “empty” columns, just before the \ character that indicates continuation. Sometimes it is nice to have Emacs insert newlines automatically when a line gets too long. Continuation on the screen does not do that. Use Auto Fill mode (see section T.5 Filling Text) if that’s what you want. As an alternative to continuation, Emacs can display long lines by truncation. This means that all the characters that do not fit in the width of the screen or window do not appear at all. They remain in the buffer, temporarily invisible. On terminals, $ in the last column informs you that the line has been truncated on the display. On window systems, a small straight arrow in the fringe to the right of the window indicates a truncated line. Truncation instead of continuation happens whenever horizontal scrolling is in use, and optionally in all side-by-side windows (see section O. Multiple Windows). You can enable or disable truncation for a particular buffer with the command M-x toggle-truncate-lines.

See section J.12 Customization of Display, for additional variables that affect how text is displayed.

Word Wrapping in emacs

To enter word wrap mode from within emacs, enter the following command: Meta-x auto-fill-mode To word wrap a pre-existing paragraph, enter: Meta-q To word wrap and full-justify a pre-existing paragraph, enter: Ctrl-u Meta-q