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