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