Sams Teach Yourself Emacs in 24 Hours |  | ContentsIndex
Hour 12: Visible Editing Utilities |  | Previous HourNext Hour
Sections in this Hour: |  |  |  |

## Filling

When writing ordinary text you often want Emacs to break the lines for you when a word goes beyond a given column. The text should not be broken in the middle of the word, but instead in between two words. In Hour 4, "Basic Editing," you already saw that auto-fill-mode could do that for you. What you might have discovered, however, is that when you break a line, Emacs does not automatically align the text for you. This is a feature, not a bug! Emacs does nothing you haven't asked it to do. To make Emacs fill a paragraph, press M-q (fill-paragraph).

A paragraph is defined using the variable paragraph-start, which is a regular expression defining the start of a paragraph and therefore also the end. In many major modes a paragraph is simply defined to start after a blank line, but in other major modes this might be more complex so a section heading might, for example, start a paragraph too.

If you do not want to fill a whole paragraph, you can instead use the function called fill-region. Unfortunately this is not bound to any key by default, so you might want to add this function to M-q (when a region is active), which you do by inserting the following into your .emacs file:

    (global-set-key [(meta q)] 'sams-fill)

M-q then works as always when the region is inactive, but when the region is active, M-q instead invokes fill-region rather than fill-paragraph.

Caution - This works only if you use transient-mark-mode. The reason that it does not work when you are not using transient-mark mode is that the region always is active then.

### Filling Parameters

Two extra parameters exist for configuring filling, namely the column at which the lines should break, and the prefix added to the beginning of every line.

There exist two different ways you can set the column where lines are broken:

- Press Escape, type the number, and press C-x f (set-fill-column).

- Go to the column where you want lines to break and press C-u C-x f.

Setting the column where lines are broken affects line breaking, both when inserting ordinary text (with auto-fill-mode enabled) and when invoking one of the fill commands.

When the lines are broken, Emacs can insert some initial text. This might, for example, be the comment character for the given major mode. To tell Emacs what this prefix should be, go to the end of it, and press C-x . (set-fill-prefix; control-x-period). This can be seen in Figure 12.8.

Figure 12.8
Setting the fill prefix.

### Adaptive Filling

In many situations, it isn't necessary to explicitly set the fill prefix, because Emacs has a feature called adaptive filling . Using adaptive filling, Emacs itself tries to guess which fill prefix you want.

Note - Adaptive filling is one of the new features in Emacs 20, so this section does not apply to Emacs 19 users.

The rules for guessing the prefix depend on the major mode, and they are kind of complex. The following can be taken as a rule of thumb (although it's not the whole truth):

- If the line starts with the comment string for the major mode, the comment string is used as a fill prefix when breaking the line.

- If the line starts with punctuation characters, the subsequent lines are filled with white spaces of the length of the punctuation characters.

Note - For the whole truth on adaptive filling, refer to the section "Adaptive Filling" in the reference manual available from the info pages.

If you dislike adaptive filling, you can disable it by inserting the following text into your .emacs file:

    (setq adaptive-fill-mode nil)

Sams Teach Yourself Emacs in 24 Hours |  | ContentsIndex
Hour 12: Visible Editing Utilities |  | Previous HourNext Hour
Sections in this Hour: |  |  |  |

© Copyright Macmillan USA. All rights reserved.