Word abbreviation mode lets you define abbreviations for special words and phrases. There are many ways to use it. Traditionally, abbreviation mode is used so that you don’t have to type long words or phrases in their entirety. For example, let’s say you are writing a contract that repeatedly references the National Institute of Standards and Technology, and you are not allowed to use an acronym. Rather than typing the full name, you can define the abbreviation nist. Once you have made this definition, Emacs inserts the full name whenever you type the abbreviation nist, followed by a space or punctuation mark. Emacs watches for you to type an abbreviation, then expands it automatically as soon as you press SPACE or a punctuation mark.
Before showing you how to get into word abbreviation mode and define your abbreviation list, we’ll start with an example. Our favorite nontraditional use for word abbreviation mode is to correct misspellings as you type. Almost everyone has a dozen or so words that they habitually type incorrectly because of some worn neural pathways. You can simply tell Emacs that these misspellings are “abbreviations” for the correct versions, and Emacs fixes the misspellings every time you type them; you may not even notice that you typed the word wrong before Emacs fixes it. So assume that you’ve entered abbreviation mode, and that you’ve defined receive as an abbreviation for recieve; now, as you’re typing, you make an innocent mistake.
Type: You will recieve
You type the offending word but haven’t yet pressed SPACE, which will cue Emacs to correct it.
Type: SPACE the materials you requested shortly.
Emacs corrects the word automatically after you press SPACE; you need not stop typing or even be aware that a mistake has been made and corrected.
Besides the convenience of being able to invent abbreviations for phrases that you frequently type, you can see that setting up a short list of abbreviations for common misspellings could reduce the time it takes to proofread files and reduce the number of common typing errors.
When you define abbreviations, never use abbreviations that are words in their own right or Emacs may expand the word when you don’t want it to, since expansion takes place without asking. For example, if you frequently write about the World Association for Replicant Technology, don’t define an abbreviation of wart, or you won’t be able to write about the difficulties of handling toads. (If you use the word wart so infrequently that you think the convenience of the acronym warrants it, you can use ESC x unexpand-abbrev RETURN to have Emacs undo the abbreviation when you really want to type wart.)
You also have to realize that Emacs only knows the abbreviations exactly as you define them. If you define recieve as an abbreviation for receive, you must also define recieves, recieving, and recieved as abbreviations to cover all the forms of the word you might misspell.
Before you go ahead and define some abbreviations, there’s one more basic fact you should know. Emacs classifies abbreviations according to which modes they work in. Global abbreviations work in all modes; local abbreviations work only in the mode in which they were defined. For example, if you want abbreviations to work only in text mode and not in C mode, define them as local while you are in text mode. If you want abbreviations to work in any mode, define them as global. Remember: abbreviations are local to modes, not to files or buffers.
Emacs also provides an inverse method for defining abbreviations. This method is called inverse because you type the abbreviation, and then the definition. Some commands (which we won’t discuss) let you type the definition and then the abbreviation, but they require some tricky key sequences to let Emacs know how many words preceding the cursor are part of the abbreviation. The inverse method is not only easier, it also works whether the definition for the abbreviation is one word or ten words.
Usually, if you go to the trouble of defining a word abbreviation, you will use it in more than one Emacs session. But if you’d like to try out abbreviation mode to see if you want to incorporate it into your startup, use the following procedure.
To define word abbreviations for this session
Enter word abbreviation mode by typing ESC x abbrev-mode RETURN. Abbrev appears on the mode line.
For a global abbreviation, type the abbreviation you want to use and type C-x a i g (which stands for add-inverse-global). (For a local abbreviation, type C-x a i l instead; you guessed it, that’s add-inverse-local.) Emacs then asks you for the expansion.
Type the definition for the abbreviation and press RETURN. Emacs then expands the abbreviation and will do so each time you type it followed by a space or punctuation mark. The abbreviations you’ve defined will work only during this Emacs session.
If you find that you like using word abbreviation mode, you may want to make it part of your startup, as described in the following section.
Once you become hooked on using abbreviation mode, it’s easiest to incorporate it into your .emacs file so that you don’t have to remember to follow the steps listed in the preceding section every time. This procedure also creates a permanent file of your word abbreviations that is loaded every time you start Emacs. You can also delete abbreviations from this file; we’ll discuss how to do so in the next section.
To define word abbreviations and make them part of your startup:
Add these lines to your .emacs file:
(setq-default abbrev-mode t) (read-abbrev-file "~/.abbrev_defs") (setq save-abbrevs t)
Save the .emacs file and reenter Emacs.
Abbrev
appears on the mode line. You also get an error message, saying Emacs can’t load your abbrev file (this message is understandable since you haven’t created the file yet). Ignore this error message; it won’t happen again.Type an abbreviation you want to use and type C-x a - or C-x a i g following the abbreviation. C-x a i g creates a global abbreviation; if you want to create a local abbreviation instead, type C-x a i l. Emacs asks you for the expansion.
Type the definition for the abbreviation and press RETURN. Emacs expands the abbreviation and will do so each time you type it followed by a space or punctuation mark. You can define as many abbreviations as you want to by repeating steps 3 and 4.
Type ESC x write-abbrev-file RETURN to write your abbreviation file. Emacs asks for the filename.
Type ~/.abbrev_defs RETURN. Emacs then writes the file.
You need take only steps 5 and 6 the first time you define abbreviations using this procedure. After this file exists, Emacs loads the abbreviations file automatically.
When you define word abbreviations in subsequent sessions, Emacs asks whether you want to save the abbreviations file. Respond with a y to save the new abbreviations you’ve defined and have them take effect automatically. [21]
If you use word abbreviations frequently, you may define an abbreviation and later change your mind. You can edit the word abbreviation list by typing ESC x edit-abbrevs RETURN. You can see (but not edit) the list by typing ESC x list-abbrevs RETURN.
Once the list is displayed, use C-k (or any other editing commands) to delete the abbreviations you don’t want to use. Because Emacs itself formats this list, don’t try to edit lines or add new lines; deleting is about the only operation that’s safe. Here’s how the abbreviations look when you edit word abbreviations. The file is divided into different sections based on whether the abbreviations are global or local to a particular mode:
"iwthout" |
1 |
"without" |
"prhase" |
1 |
"phrase" |
"teh" |
1 |
"the" |
"fo" |
1 |
"of" |
"eamcs" |
2 |
"Emacs" |
"wrok" |
1 |
"work" |
"aslo" |
1 |
"also" |
"sotred" |
1 |
"stored" |
"inforamtion" |
1 |
"information" |
"esc" |
6 |
"ESC" |
"taht" |
1 |
"that" |
"chatper" |
1 |
"chapter" |
"adn" |
1 |
"and" |
"iwth" |
1 |
"with" |
"chpater" |
1 |
"chapter" |
"tex" |
36 |
"TEX" |
"loaction" |
1 |
"location" |
"wart" |
1 |
"World Association for Replicant Technology” |
The first column lists the abbreviations; in this case, the abbreviations are mostly misspellings. The second column is for internal record keeping; you don’t need to concern yourself with it. The third column provides the definitions of the abbreviations, the word or phrase that Emacs substitutes whenever it sees the abbreviation.
To delete any abbreviation, move to the line on which it is described. Then kill the entire line with C-k and save the file by typing ESC x write-abbrev-file. You can move back to the buffer you were editing before by typing C-x b. (This command is for working with multiple buffers, discussed in Chapter 4.)
You can get rid of word abbreviations completely in one of two ways. First, you can type ESC x kill-all-abbrevs RETURN. This command disables word abbreviations for the current session.
Second, you can delete the file the abbreviations are in. If you made word abbreviations part of your startup, delete the read-abbrev-file line out of your .emacs file.
Usually, you’ll find that the way Emacs capitalizes abbreviations is exactly what you want. If you run into special situations with abbreviations and capitalization, however, it’s often helpful to know what’s going on behind the scenes. Here are the rules:
If the abbreviation’s definition contains any uppercase letters, Emacs always inserts the definition without changing anything. For example, if you define ora as an abbreviation for O’Reilly & Associates, then O’Reilly & Associates will always be capitalized exactly as given.
If the abbreviation’s definition is all lowercase, Emacs capitalizes according to the following rules:
- -
If you type all of the letters of the abbreviation in lowercase, Emacs inserts the definition in lowercase.
- -
If you type any of the letters of the abbreviation in uppercase, Emacs capitalizes the first letter of the first word.
- -
If you type all of the letters of the abbreviation in uppercase, Emacs capitalizes the first letter of every word, unless the variable abbrev-all-caps is set to t; in this case, it capitalizes all letters.
Table 3-8 shows some examples:
Table 4-8. Word Abbreviation Examples
You |
Expands | |||
---|---|---|---|---|
Abbreviation |
Definition |
Type |
To |
Because |
lc |
lamb chop |
lc |
lamb chop |
lc is lowercase, so lamb chop is lowercase. |
lc |
lamb chop |
Lc |
Lamb chop |
There’s one capital in Lc, so Lamb is capitalized. |
lc |
lamb chop |
lC |
Lamb chop |
There’s one capital in lC, so Lamb is capitalized. |
lc |
lamb chop |
LC |
Lamb Chop |
LC is all capitals, so both words are capitalized. |
lc |
Lamb Chop |
lc |
Lamb Chop |
Capitals in the definition are always unchanged. |
lc |
Lamb Chop |
LC |
Lamb Chop |
Capitals in the definition are always unchanged. |
As we said, the rules are a lot to memorize. Fortunately, you don’t need to remember the rules. Emacs usually capitalizes properly on its own, and with a little experimentation, you’ll become comfortable with the way Emacs behaves.
Table 3-9 summarizes word abbreviation commands.
Table 4-9. Word Abbreviation Commands
Keystrokes |
Command Name |
Action |
---|---|---|
(none) |
abbrev-mode |
Enter (or exit) word abbreviation mode. |
C-x a - or C-x a i g |
inverse-add-global-abbrev |
After typing the global abbreviation, type the definition. |
C-x a i l |
inverse-add-mode-abbrev |
After typing the local abbreviation, type the definition. |
(none) |
unexpand-abbrev |
Undo the last word abbreviation. |
(none) |
write-abbrev-file |
Write the word abbreviation file. |
(none) |
edit-abbrevs |
Edit the word abbreviations. |
(none) |
list-abbrevs |
View the word abbreviations. |
(none) |
kill-all-abbrevs |
Kill abbreviations for this session. |
You search for a string you can see on the screen, and Emacs can’t find it. The most probable explanation is that Emacs is taking into account line breaks and punctuation, and you’re not including these in the search string. Use word search, which ignores any line breaks or punctuation, to find the string.
Ispell doesn’t work correctly. There are lots of reasons this may happen. In some cases, ispell-region may work when other commands do not. Mark the buffer as a region by typing C-x h, then type ESC x ispell-region. If this strategy doesn’t work, read on.
You get a message that says, “Problem starting Ispell; use old-style spell instead?" It’s likely that Ispell is not installed. If you type y, Emacs starts the UNIX spell program. Read about the spell program in Section 3.3.2.
You get a message that says, “ispell version 3.1.* is required: try renaming ispell4.el ispell.el." You’re using Emacs 19 with Ispell Version 4. Emacs 19 needs Ispell Version 3. Ask your system administrator to downgrade Ispell to Version 3. (You might suggest he or she try renaming the file ispell4.el to ispell.el first.)
When using spell, you get a message that says, “Searching for program: no such file or directory, spell." Some UNIX systems, particularly Linux distributions, substitute Ispell for the UNIX spell program. In this case, you have to use Ispell or find someone who has a copy of spell to install on your system.
When using spell-buffer, you get an error message that says “spell can’t read .spell ." You need to create a .spell file in the same directory as the file you are spell checking.
You defined abbreviations but Emacs didn’t save them. Unless you make changes to your .emacs file, Emacs doesn’t save them by default. See “Making Word Abbreviations Part of Your Startup” earlier in this chapter. If you’ve made the changes to your .emacs file and it’s still not working, you probably have an earlier version of Emacs 19, which has a minor abbreviation mode bug. You have to explicitly save abbreviations by typing ESC x write-abbrev-file each time you define some abbreviations.
[21] Some early versions of Emacs 19 don’t ask you if you want your abbreviations file saved. If this happens, you will have to save your abbreviations explicitly by typing ESC x write-abbrev-file RETURN each time you define some abbreviations. Since you probably don’t define new abbreviations every day, this requirement may not be a major inconvenience. Anyhow, we think abbreviations are worth the extra effort.
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.