#
[bpt/emacs.git] / man / abbrevs.texi
1 @c This is part of the Emacs manual.
2 @c Copyright (C) 1985, 86, 87, 93, 94, 95, 1997 Free Software Foundation, Inc.
3 @c See file emacs.texi for copying conditions.
4 @node Abbrevs, Picture, Building, Top
5 @chapter Abbrevs
6 @cindex abbrevs
7 @cindex expansion (of abbrevs)
8
9 A defined @dfn{abbrev} is a word which @dfn{expands}, if you insert
10 it, into some different text. Abbrevs are defined by the user to expand
11 in specific ways. For example, you might define @samp{foo} as an abbrev
12 expanding to @samp{find outer otter}. Then you would be able to insert
13 @samp{find outer otter } into the buffer by typing @kbd{f o o
14 @key{SPC}}.
15
16 A second kind of abbreviation facility is called @dfn{dynamic abbrev
17 expansion}. You use dynamic abbrev expansion with an explicit command
18 to expand the letters in the buffer before point by looking for other
19 words in the buffer that start with those letters. @xref{Dynamic
20 Abbrevs}.
21
22 @menu
23 * Abbrev Concepts:: Fundamentals of defined abbrevs.
24 * Defining Abbrevs:: Defining an abbrev, so it will expand when typed.
25 * Expanding Abbrevs:: Controlling expansion: prefixes, canceling expansion.
26 * Editing Abbrevs:: Viewing or editing the entire list of defined abbrevs.
27 * Saving Abbrevs:: Saving the entire list of abbrevs for another session.
28 * Dynamic Abbrevs:: Abbreviations for words already in the buffer.
29 * Dabbrev Customization:: What is a word, for dynamic abbrevs. Case handling.
30 @end menu
31
32 @node Abbrev Concepts
33 @section Abbrev Concepts
34
35 An @dfn{abbrev} is a word which has been defined to @dfn{expand} into
36 a specified @dfn{expansion}. When you insert a word-separator character
37 following the abbrev, that expands the abbrev---replacing the abbrev
38 with its expansion. For example, if @samp{foo} is defined as an abbrev
39 expanding to @samp{find outer otter}, then you can insert @samp{find
40 outer otter.} into the buffer by typing @kbd{f o o .}.
41
42 @findex abbrev-mode
43 @vindex abbrev-mode
44 @cindex Abbrev mode
45 @cindex mode, Abbrev
46 Abbrevs expand only when Abbrev mode (a minor mode) is enabled.
47 Disabling Abbrev mode does not cause abbrev definitions to be forgotten,
48 but they do not expand until Abbrev mode is enabled again. The command
49 @kbd{M-x abbrev-mode} toggles Abbrev mode; with a numeric argument, it
50 turns Abbrev mode on if the argument is positive, off otherwise.
51 @xref{Minor Modes}. @code{abbrev-mode} is also a variable; Abbrev mode is
52 on when the variable is non-@code{nil}. The variable @code{abbrev-mode}
53 automatically becomes local to the current buffer when it is set.
54
55 Abbrev definitions can be @dfn{mode-specific}---active only in one major
56 mode. Abbrevs can also have @dfn{global} definitions that are active in
57 all major modes. The same abbrev can have a global definition and various
58 mode-specific definitions for different major modes. A mode-specific
59 definition for the current major mode overrides a global definition.
60
61 Abbrevs can be defined interactively during the editing session. Lists
62 of abbrev definitions can also be saved in files and reloaded in later
63 sessions. Some users keep extensive lists of abbrevs that they load in
64 every session.
65
66 @node Defining Abbrevs
67 @section Defining Abbrevs
68
69 @table @kbd
70 @item C-x a g
71 Define an abbrev, using one or more words before point as its expansion
72 (@code{add-global-abbrev}).
73 @item C-x a l
74 Similar, but define an abbrev specific to the current major mode
75 (@code{add-mode-abbrev}).
76 @item C-x a i g
77 Define a word in the buffer as an abbrev (@code{inverse-add-global-abbrev}).
78 @item C-x a i l
79 Define a word in the buffer as a mode-specific abbrev
80 (@code{inverse-add-mode-abbrev}).
81 @item M-x kill-all-abbrevs
82 This command discards all abbrev definitions currently in effect,
83 leaving a blank slate.
84 @end table
85
86 @kindex C-x a g
87 @findex add-global-abbrev
88 The usual way to define an abbrev is to enter the text you want the
89 abbrev to expand to, position point after it, and type @kbd{C-x a g}
90 (@code{add-global-abbrev}). This reads the abbrev itself using the
91 minibuffer, and then defines it as an abbrev for one or more words before
92 point. Use a numeric argument to say how many words before point should be
93 taken as the expansion. For example, to define the abbrev @samp{foo} as
94 mentioned above, insert the text @samp{find outer otter} and then type
95 @kbd{C-u 3 C-x a g f o o @key{RET}}.
96
97 An argument of zero to @kbd{C-x a g} means to use the contents of the
98 region as the expansion of the abbrev being defined.
99
100 @kindex C-x a l
101 @findex add-mode-abbrev
102 The command @kbd{C-x a l} (@code{add-mode-abbrev}) is similar, but
103 defines a mode-specific abbrev. Mode-specific abbrevs are active only in a
104 particular major mode. @kbd{C-x a l} defines an abbrev for the major mode
105 in effect at the time @kbd{C-x a l} is typed. The arguments work the same
106 as for @kbd{C-x a g}.
107
108 @kindex C-x a i g
109 @findex inverse-add-global-abbrev
110 @kindex C-x a i l
111 @findex inverse-add-mode-abbrev
112 If the text already in the buffer is the abbrev, rather than its
113 expansion, use command @kbd{C-x a i g}
114 (@code{inverse-add-global-abbrev}) instead of @kbd{C-x a g}, or use
115 @kbd{C-x a i l} (@code{inverse-add-mode-abbrev}) instead of @kbd{C-x a
116 l}. These commands are called ``inverse'' because they invert the
117 meaning of the two text strings they use (one from the buffer and one
118 read with the minibuffer).
119
120 To change the definition of an abbrev, just define a new definition.
121 When the abbrev has a prior definition, the abbrev definition commands
122 ask for confirmation for replacing it.
123
124 To remove an abbrev definition, give a negative argument to the abbrev
125 definition command: @kbd{C-u - C-x a g} or @kbd{C-u - C-x a l}. The
126 former removes a global definition, while the latter removes a
127 mode-specific definition.
128
129 @findex kill-all-abbrevs
130 @kbd{M-x kill-all-abbrevs} removes all the abbrev definitions there
131 are, both global and local.
132
133 @node Expanding Abbrevs
134 @section Controlling Abbrev Expansion
135
136 An abbrev expands whenever it is present in the buffer just before
137 point and you type a self-inserting whitespace or punctuation character
138 (@key{SPC}, comma, etc.@:). More precisely, any character that is not a
139 word constituent expands an abbrev, and any word-constituent character
140 can be part of an abbrev. The most common way to use an abbrev is to
141 insert it and then insert a punctuation character to expand it.
142
143 @vindex abbrev-all-caps
144 Abbrev expansion preserves case; thus, @samp{foo} expands into @samp{find
145 outer otter}; @samp{Foo} into @samp{Find outer otter}, and @samp{FOO} into
146 @samp{FIND OUTER OTTER} or @samp{Find Outer Otter} according to the
147 variable @code{abbrev-all-caps} (a non-@code{nil} value chooses the first
148 of the two expansions).
149
150 These commands are used to control abbrev expansion:
151
152 @table @kbd
153 @item M-'
154 Separate a prefix from a following abbrev to be expanded
155 (@code{abbrev-prefix-mark}).
156 @item C-x a e
157 @findex expand-abbrev
158 Expand the abbrev before point (@code{expand-abbrev}).
159 This is effective even when Abbrev mode is not enabled.
160 @item M-x expand-region-abbrevs
161 Expand some or all abbrevs found in the region.
162 @end table
163
164 @kindex M-'
165 @findex abbrev-prefix-mark
166 You may wish to expand an abbrev with a prefix attached; for example,
167 if @samp{cnst} expands into @samp{construction}, you might want to use
168 it to enter @samp{reconstruction}. It does not work to type
169 @kbd{recnst}, because that is not necessarily a defined abbrev. What
170 you can do is use the command @kbd{M-'} (@code{abbrev-prefix-mark}) in
171 between the prefix @samp{re} and the abbrev @samp{cnst}. First, insert
172 @samp{re}. Then type @kbd{M-'}; this inserts a hyphen in the buffer to
173 indicate that it has done its work. Then insert the abbrev @samp{cnst};
174 the buffer now contains @samp{re-cnst}. Now insert a non-word character
175 to expand the abbrev @samp{cnst} into @samp{construction}. This
176 expansion step also deletes the hyphen that indicated @kbd{M-'} had been
177 used. The result is the desired @samp{reconstruction}.
178
179 If you actually want the text of the abbrev in the buffer, rather than
180 its expansion, you can accomplish this by inserting the following
181 punctuation with @kbd{C-q}. Thus, @kbd{foo C-q ,} leaves @samp{foo,} in
182 the buffer.
183
184 @findex unexpand-abbrev
185 If you expand an abbrev by mistake, you can undo the expansion and
186 bring back the abbrev itself by typing @kbd{C-_} to undo (@pxref{Undo}).
187 This also undoes the insertion of the non-word character that expanded
188 the abbrev. If the result you want is the terminating non-word
189 character plus the unexpanded abbrev, you must reinsert the terminating
190 character, quoting it with @kbd{C-q}. You can also use the command
191 @kbd{M-x unexpand-abbrev} to cancel the last expansion without
192 deleting the terminating character.
193
194 @findex expand-region-abbrevs
195 @kbd{M-x expand-region-abbrevs} searches through the region for defined
196 abbrevs, and for each one found offers to replace it with its expansion.
197 This command is useful if you have typed in text using abbrevs but forgot
198 to turn on Abbrev mode first. It may also be useful together with a
199 special set of abbrev definitions for making several global replacements at
200 once. This command is effective even if Abbrev mode is not enabled.
201
202 Expanding an abbrev runs the hook @code{pre-abbrev-expand-hook}
203 (@pxref{Hooks}).
204
205 @need 1500
206 @node Editing Abbrevs
207 @section Examining and Editing Abbrevs
208
209 @table @kbd
210 @item M-x list-abbrevs
211 Display a list of all abbrev definitions.
212 @item M-x edit-abbrevs
213 Edit a list of abbrevs; you can add, alter or remove definitions.
214 @end table
215
216 @findex list-abbrevs
217 The output from @kbd{M-x list-abbrevs} looks like this:
218
219 @example
220 (lisp-mode-abbrev-table)
221 "dk" 0 "define-key"
222 (global-abbrev-table)
223 "dfn" 0 "definition"
224 @end example
225
226 @noindent
227 (Some blank lines of no semantic significance, and some other abbrev
228 tables, have been omitted.)
229
230 A line containing a name in parentheses is the header for abbrevs in a
231 particular abbrev table; @code{global-abbrev-table} contains all the global
232 abbrevs, and the other abbrev tables that are named after major modes
233 contain the mode-specific abbrevs.
234
235 Within each abbrev table, each nonblank line defines one abbrev. The
236 word at the beginning of the line is the abbrev. The number that
237 follows is the number of times the abbrev has been expanded. Emacs
238 keeps track of this to help you see which abbrevs you actually use, so
239 that you can eliminate those that you don't use often. The string at
240 the end of the line is the expansion.
241
242 @findex edit-abbrevs
243 @kindex C-c C-c @r{(Edit Abbrevs)}
244 @kbd{M-x edit-abbrevs} allows you to add, change or kill abbrev
245 definitions by editing a list of them in an Emacs buffer. The list has
246 the same format described above. The buffer of abbrevs is called
247 @samp{*Abbrevs*}, and is in Edit-Abbrevs mode. Type @kbd{C-c C-c} in
248 this buffer to install the abbrev definitions as specified in the
249 buffer---and delete any abbrev definitions not listed.
250
251 The command @code{edit-abbrevs} is actually the same as
252 @code{list-abbrevs} except that it selects the buffer @samp{*Abbrevs*}
253 whereas @code{list-abbrevs} merely displays it in another window.
254
255 @node Saving Abbrevs
256 @section Saving Abbrevs
257
258 These commands allow you to keep abbrev definitions between editing
259 sessions.
260
261 @table @kbd
262 @item M-x write-abbrev-file @key{RET} @var{file} @key{RET}
263 Write a file @var{file} describing all defined abbrevs.
264 @item M-x read-abbrev-file @key{RET} @var{file} @key{RET}
265 Read the file @var{file} and define abbrevs as specified therein.
266 @item M-x quietly-read-abbrev-file @key{RET} @var{file} @key{RET}
267 Similar but do not display a message about what is going on.
268 @item M-x define-abbrevs
269 Define abbrevs from definitions in current buffer.
270 @item M-x insert-abbrevs
271 Insert all abbrevs and their expansions into current buffer.
272 @end table
273
274 @findex write-abbrev-file
275 @kbd{M-x write-abbrev-file} reads a file name using the minibuffer and
276 then writes a description of all current abbrev definitions into that
277 file. This is used to save abbrev definitions for use in a later
278 session. The text stored in the file is a series of Lisp expressions
279 that, when executed, define the same abbrevs that you currently have.
280
281 @findex read-abbrev-file
282 @findex quietly-read-abbrev-file
283 @vindex abbrev-file-name
284 @kbd{M-x read-abbrev-file} reads a file name using the minibuffer and
285 then reads the file, defining abbrevs according to the contents of the
286 file. @kbd{M-x quietly-read-abbrev-file} is the same except that it
287 does not display a message in the echo area saying that it is doing its
288 work; it is actually useful primarily in the @file{.emacs} file. If an
289 empty argument is given to either of these functions, they use the file
290 name specified in the variable @code{abbrev-file-name}, which is by
291 default @code{"~/.abbrev_defs"}.
292
293 @vindex save-abbrevs
294 Emacs will offer to save abbrevs automatically if you have changed any of
295 them, whenever it offers to save all files (for @kbd{C-x s} or @kbd{C-x
296 C-c}). This feature can be inhibited by setting the variable
297 @code{save-abbrevs} to @code{nil}.
298
299 @findex insert-abbrevs
300 @findex define-abbrevs
301 The commands @kbd{M-x insert-abbrevs} and @kbd{M-x define-abbrevs} are
302 similar to the previous commands but work on text in an Emacs buffer.
303 @kbd{M-x insert-abbrevs} inserts text into the current buffer before point,
304 describing all current abbrev definitions; @kbd{M-x define-abbrevs} parses
305 the entire current buffer and defines abbrevs accordingly.@refill
306
307 @node Dynamic Abbrevs
308 @section Dynamic Abbrev Expansion
309
310 The abbrev facility described above operates automatically as you insert
311 text, but all abbrevs must be defined explicitly. By contrast,
312 @dfn{dynamic abbrevs} allow the meanings of abbrevs to be determined
313 automatically from the contents of the buffer, but dynamic abbrev expansion
314 happens only when you request it explicitly.
315
316 @kindex M-/
317 @kindex C-M-/
318 @findex dabbrev-expand
319 @findex dabbrev-completion
320 @table @kbd
321 @item M-/
322 Expand the word in the buffer before point as a @dfn{dynamic abbrev},
323 by searching in the buffer for words starting with that abbreviation
324 (@code{dabbrev-expand}).
325
326 @item C-M-/
327 Complete the word before point as a dynamic abbrev
328 (@code{dabbrev-completion}).
329 @end table
330
331 @vindex dabbrev-limit
332 For example, if the buffer contains @samp{does this follow } and you
333 type @kbd{f o M-/}, the effect is to insert @samp{follow} because that
334 is the last word in the buffer that starts with @samp{fo}. A numeric
335 argument to @kbd{M-/} says to take the second, third, etc.@: distinct
336 expansion found looking backward from point. Repeating @kbd{M-/}
337 searches for an alternative expansion by looking farther back. After
338 scanning all the text before point, it searches the text after point.
339 The variable @code{dabbrev-limit}, if non-@code{nil}, specifies how far
340 in the buffer to search for an expansion.
341
342 @vindex dabbrev-check-all-buffers
343 After scanning the current buffer, @kbd{M-/} normally searches other
344 buffers, unless you have set @code{dabbrev-check-all-buffers} to
345 @code{nil}.
346
347 A negative argument to @kbd{M-/}, as in @kbd{C-u - M-/}, says to
348 search first for expansions after point, and second for expansions
349 before point. If you repeat the @kbd{M-/} to look for another
350 expansion, do not specify an argument. This tries all the expansions
351 after point and then the expansions before point.
352
353 After you have expanded a dynamic abbrev, you can copy additional
354 words that follow the expansion in its original context. Simply type
355 @kbd{@key{SPC} M-/} for each word you want to copy. The spacing and
356 punctuation between words is copied along with the words.
357
358 The command @kbd{C-M-/} (@code{dabbrev-completion}) performs
359 completion of a dynamic abbreviation. Instead of trying the possible
360 expansions one by one, it finds all of them, then inserts the text that
361 they have in common. If they have nothing in common, @kbd{C-M-/}
362 displays a list of completions, from which you can select a choice in
363 the usual manner. @xref{Completion}.
364
365 Dynamic abbrev expansion is completely independent of Abbrev mode; the
366 expansion of a word with @kbd{M-/} is completely independent of whether
367 it has a definition as an ordinary abbrev.
368
369 @node Dabbrev Customization
370 @section Customizing Dynamic Abbreviation
371
372 Normally, dynamic abbrev expansion ignores case when searching for
373 expansions. That is, the expansion need not agree in case with the word
374 you are expanding.
375
376 @vindex dabbrev-case-fold-search
377 This feature is controlled by the variable
378 @code{dabbrev-case-fold-search}. If it is @code{t}, case is ignored in
379 this search; if @code{nil}, the word and the expansion must match in
380 case. If the value of @code{dabbrev-case-fold-search} is
381 @code{case-fold-search}, which is true by default, then the variable
382 @code{case-fold-search} controls whether to ignore case while searching
383 for expansions.
384
385 @vindex dabbrev-case-replace
386 Normally, dynamic abbrev expansion preserves the case pattern @emph{of
387 the abbrev you have typed}, by converting the expansion to that case
388 pattern.
389
390 @vindex dabbrev-case-fold-search
391 The variable @code{dabbrev-case-replace} controls whether to preserve
392 the case pattern of the abbrev. If it is @code{t}, the abbrev's case
393 pattern is preserved in most cases; if @code{nil}, the expansion is
394 always copied verbatim. If the value of @code{dabbrev-case-replace} is
395 @code{case-replace}, which is true by default, then the variable
396 @code{case-replace} controls whether to copy the expansion verbatim.
397
398 However, if the expansion contains a complex mixed case pattern, and
399 the abbrev matches this pattern as far as it goes, then the expansion is
400 always copied verbatim, regardless of those variables. Thus, for
401 example, if the buffer contains @code{variableWithSillyCasePattern}, and
402 you type @kbd{v a M-/}, it copies the expansion verbatim including its
403 case pattern.
404
405 @vindex dabbrev-abbrev-char-regexp
406 The variable @code{dabbrev-abbrev-char-regexp}, if non-@code{nil},
407 controls which characters are considered part of a word, for dynamic expansion
408 purposes. The regular expression must match just one character, never
409 two or more. The same regular expression also determines which
410 characters are part of an expansion. The value @code{nil} has a special
411 meaning: abbreviations are made of word characters, but expansions are
412 made of word and symbol characters.
413
414 @vindex dabbrev-abbrev-skip-leading-regexp
415 In shell scripts and makefiles, a variable name is sometimes prefixed
416 with @samp{$} and sometimes not. Major modes for this kind of text can
417 customize dynamic abbreviation to handle optional prefixes by setting
418 the variable @code{dabbrev-abbrev-skip-leading-regexp}. Its value
419 should be a regular expression that matches the optional prefix that
420 dynamic abbreviation should ignore.