To define word abbreviations and make them part of your startup:
1. Add these lines to your .emacs file:
(setq-default abbrev-mode t)
(read-abbrev-file "~/.abbrev_defs")
(setq save-abbrevs t)
2. Save the .emacs file and reenter Emacs. Abbrev
appears on the mode line. You may get an error message saying Emacs can't load your abbrev file (understandable if you haven't created the file yet). Ignore this error message; it won't happen again.
3. Type an abbreviation and type C-x a i g or C-x a - following the abbreviation. These commands create a global abbreviation; if you want to create a local abbreviation instead, type C-x a i l. Emacs asks you for the expansion.
4. Type the definition for the abbreviation and press Enter. 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.
5. Type C-x C-c to exit Emacs. Emacs asks if you want to save the abbreviations in .abbrev_defs.
6. Type y to save your abbreviations.
After you define some abbreviations and save them, Emacs loads the abbreviations file automatically. When you define word abbreviations in subsequent sessions, Emacs asks again 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.
3.4.2.3 Deleting a word abbreviation
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 M-x edit-abbrevs Enter. You can see (but not edit) the list by typing M-x list-abbrevs Enter.
After 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:
(text-mode-abbrev-table)
(lisp-mode-abbrev-table)
(fundamental-mode-abbrev-table)
(global-abbrev-table)
"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"
"loaction" 1 "location"
"recieve" 1 "receive"
"wart" 1 "World Association for Replicant Technology"
The file is divided into sections by mode. We defined global abbreviations in this case; any abbreviations Flyspell (described earlier in this chapter) creates are local abbreviations and would be listed under the mode in which they were defined.
In this buffer, the first column lists the abbreviations (in this case, 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, delete the line for that abbreviation and save the file by typing M-x write-abbrev-file. You can move back to the buffer you were editing before by typing C-x b (a command for working with multiple buffers, discussed in Chapter 4).
3.4.2.4 Disabling word abbreviations
You can get rid of word abbreviations completely in one of two ways. First, you can type M-x kill-all-abbrevs Enter. 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 from your .emacs file.
3.4.2.5 Abbreviations and capitalization
Usually, Emacs capitalizes abbreviations exactly the way you want. If you run into special situations with abbreviations and capitalization, however, you may wantl 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 Media, O'Reilly will always be capitalized exactly as shown.
• 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-7 shows some examples.
Table 3-7. Word abbreviation capitalization
Abbreviation | Definition | You type: | Expands 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. |