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

## Automatically Replacing One String with Another

Some errors are better fixed using means other than spell-checking, such as correcting words that you habitually spell incorrectly. I, for example, often write teh when I intend to write the. Likewise, I often write writting instead of writing. A spelling checker could easily tell me about these errors, but given that I make them often, it is a waste of time and an irritation to fix them over and over again.

Tip - Remember, Emacs helps you to be creative and avoid doing trivial tasks over and over again!

The alternative is to set Emacs up to replace one word with another whenever it is typed. Thus, when I type teh and press any nonalphanumeric symbol (for example, a period or a comma), Emacs replaces this with the without asking me. It replaces the word only if it is a standalone word. Thus, Teheran isn't replaced with Theeran.

This feature was designed to expand abbreviations into full words.

Caution - It should go without saying that you should be very careful when defining abbreviations. Defining the as an abbreviation for the hundred elephants is definitely not a good idea, because you would get the text the hundred elephants whenever you type the.

To enable this feature press M-x and type abbrev-mode, which turns it on for the given buffer. If you always want it enabled, you must add it to a hook function. See the section "Controlling Options" in Hour 24, "Installing Emacs Add-Ons."

There are two sets of abbreviations: those local to a given major mode and those for all modes. To add an abbreviation for your current major mode, press C-x a i l (inverse-add-mode-abbrev). To add one for all modes, press C-x a i g (inverse-add-global-abbrev). When typing one of the two, the abbreviation must be located before point. Emacs then asks what this abbreviation should expand to. This can be seen in the following To Do section.

#### Creating an Abbreviation

This task shows you how to assign the abbreviation tdg to The Dotfile Generator . Follow these steps:

  1. Load abbrev-mode if it isn't loaded already (it is loaded if the text Abbrev is in the mode-line). To load it, press M-x and type abbrev-mode.

  2. Type tdg (this is the word that expands to The Dotfile Generator ).

  3. Press C-x a i g (inverse-add-global-abbrev).

  4. Emacs now asks you what word the abbreviation should expand to. Type The Dotfile Generator.

The abbreviation has now been defined as a global abbreviation, which means that it works in any mode. Try typing tdg and a space, and you can see how Emacs replaces this with The Dotfile Generator and the space.

If you, for one reason or another, need to type a word that is an abbreviation, type the word and press C-q and the word delimiter. Thus if you define tdg to be an abbreviation, to insert tdg and a space in the text, type tdg, press C-q, and finally press the spacebar (or any other word delimiter).

### Editing Abbreviations

If you press M-x and type edit-abbrevs, a buffer appears in which you can edit the abbreviations (see Figure 11.8).

Figure 11.8
The buffer in which you can edit your abbreviations.

In this buffer you can add, delete, or edit the abbreviation. When you finish, press C-x C-s.and kill the buffer.

### Loading or Saving an Abbreviation to a File

If you want the abbreviation to exist from one session to the next, you must insert the following line into your .emacs file:

    (read-abbrev-file)

When this line is in the .emacs file, abbreviations are loaded during startup of Emacs; when Emacs exits they are written. If you want the abbreviations to be saved as soon as they are defined, insert the following into your .emacs file:

    (sams-write-abbrev-at-once)

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

© Copyright Macmillan USA. All rights reserved.