1 @c This is part of the Emacs manual.
2 @c Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995, 1997, 2001, 2002, 2003,
3 @c 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 @c See file emacs.texi for copying conditions.
8 @cindex expansion (of abbrevs)
10 A defined @dfn{abbrev} is a word which @dfn{expands}, if you insert
11 it, into some different text. Abbrevs are defined by the user to expand
12 in specific ways. For example, you might define @samp{foo} as an abbrev
13 expanding to @samp{find outer otter}. Then you could insert
14 @samp{find outer otter } into the buffer by typing @kbd{f o o
17 A second kind of abbreviation facility is called @dfn{dynamic abbrev
18 expansion}. You use dynamic abbrev expansion with an explicit command
19 to expand the letters in the buffer before point by looking for other
20 words in the buffer that start with those letters. @xref{Dynamic
23 ``Hippie'' expansion generalizes abbreviation expansion.
24 @xref{Hippie Expand, , Hippie Expansion, autotype, Features for
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.
38 @section Abbrev Concepts
40 An @dfn{abbrev} is a word which has been defined to @dfn{expand} into
41 a specified @dfn{expansion}. When you insert a word-separator character
42 following the abbrev, that expands the abbrev---replacing the abbrev
43 with its expansion. For example, if @samp{foo} is defined as an abbrev
44 expanding to @samp{find outer otter}, then you can insert @samp{find
45 outer otter.} into the buffer by typing @kbd{f o o .}.
51 Abbrevs expand only when Abbrev mode (a minor mode) is enabled.
52 Disabling Abbrev mode does not cause abbrev definitions to be forgotten,
53 but 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
55 turns Abbrev mode on if the argument is positive, off otherwise.
56 @xref{Minor Modes}. @code{abbrev-mode} is also a variable; Abbrev mode is
57 on when the variable is non-@code{nil}. The variable @code{abbrev-mode}
58 automatically becomes local to the current buffer when it is set.
60 Abbrevs can have @dfn{mode-specific} definitions, active only in one major
61 mode. Abbrevs can also have @dfn{global} definitions that are active in
62 all major modes. The same abbrev can have a global definition and various
63 mode-specific definitions for different major modes. A mode-specific
64 definition for the current major mode overrides a global definition.
66 You can define abbrevs interactively during the editing session. You
67 can also save lists of abbrev definitions in files for use in later
68 sessions. Some users keep extensive lists of abbrevs that they load
71 @node Defining Abbrevs
72 @section Defining Abbrevs
76 Define an abbrev, using one or more words before point as its expansion
77 (@code{add-global-abbrev}).
79 Similar, but define an abbrev specific to the current major mode
80 (@code{add-mode-abbrev}).
82 Define a word in the buffer as an abbrev (@code{inverse-add-global-abbrev}).
84 Define a word in the buffer as a mode-specific abbrev
85 (@code{inverse-add-mode-abbrev}).
86 @item M-x define-global-abbrev @key{RET} @var{abbrev} @key{RET} @var{exp} @key{RET}
87 Define @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}
89 Define @var{abbrev} as a mode-specific abbrev expanding into @var{exp}.
90 @item M-x kill-all-abbrevs
91 Discard all abbrev definitions, leaving a blank slate.
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}}.
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.
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}.
117 @findex inverse-add-global-abbrev
119 @findex inverse-add-mode-abbrev
120 If the abbrev text itself is already in the buffer, you can use the
121 commands @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
123 abbrev by specify the expansion in the minibuffer. These commands are
124 called ``inverse'' because they invert the meaning of the two text
125 strings they use (one from the buffer and one read with the
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.
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.
139 @findex kill-all-abbrevs
140 To remove an abbrev definition, give a negative argument to the
141 abbrev definition command: @kbd{C-u - C-x a g} or @kbd{C-u - C-x a l}.
142 The former removes a global definition, while the latter removes a
143 mode-specific definition. @kbd{M-x kill-all-abbrevs} removes all
144 abbrev definitions, both global and local.
146 @node Expanding Abbrevs
147 @section Controlling Abbrev Expansion
149 When Abbrev mode is enabled, an abbrev expands whenever it is
150 present in the buffer just before point and you type a self-inserting
151 whitespace or punctuation character (@key{SPC}, comma, etc.@:). More
152 precisely, any character that is not a word constituent expands an
153 abbrev, and any word-constituent character can be part of an abbrev.
154 The most common way to use an abbrev is to insert it and then insert a
155 punctuation or whitespace character to expand it.
157 @vindex abbrev-all-caps
158 Abbrev expansion preserves case; thus, @samp{foo} expands into @samp{find
159 outer otter}; @samp{Foo} into @samp{Find outer otter}, and @samp{FOO} into
160 @samp{FIND OUTER OTTER} or @samp{Find Outer Otter} according to the
161 variable @code{abbrev-all-caps} (setting it non-@code{nil} specifies
162 @samp{FIND OUTER OTTER}).
164 These commands are used to control abbrev expansion:
168 Separate a prefix from a following abbrev to be expanded
169 (@code{abbrev-prefix-mark}).
171 @findex expand-abbrev
172 Expand the abbrev before point (@code{expand-abbrev}).
173 This is effective even when Abbrev mode is not enabled.
174 @item M-x expand-region-abbrevs
175 Expand some or all abbrevs found in the region.
179 @findex abbrev-prefix-mark
180 You may wish to expand an abbrev and attach a prefix to the expansion;
181 for example, if @samp{cnst} expands into @samp{construction}, you might want
182 to use it to enter @samp{reconstruction}. It does not work to type
183 @kbd{recnst}, because that is not necessarily a defined abbrev. What
184 you can do is use the command @kbd{M-'} (@code{abbrev-prefix-mark}) in
185 between the prefix @samp{re} and the abbrev @samp{cnst}. First, insert
186 @samp{re}. Then type @kbd{M-'}; this inserts a hyphen in the buffer to
187 indicate that it has done its work. Then insert the abbrev @samp{cnst};
188 the buffer now contains @samp{re-cnst}. Now insert a non-word character
189 to expand the abbrev @samp{cnst} into @samp{construction}. This
190 expansion step also deletes the hyphen that indicated @kbd{M-'} had been
191 used. The result is the desired @samp{reconstruction}.
193 If you actually want the text of the abbrev in the buffer, rather than
194 its expansion, you can accomplish this by inserting the following
195 punctuation with @kbd{C-q}. Thus, @kbd{foo C-q ,} leaves @samp{foo,} in
196 the buffer, not expanding it.
198 @findex unexpand-abbrev
199 If you expand an abbrev by mistake, you can undo the expansion and
200 bring back the abbrev itself by typing @kbd{C-_} to undo (@pxref{Undo}).
201 This also undoes the insertion of the non-word character that expanded
202 the abbrev. If the result you want is the terminating non-word
203 character plus the unexpanded abbrev, you must reinsert the terminating
204 character, quoting it with @kbd{C-q}. You can also use the command
205 @kbd{M-x unexpand-abbrev} to cancel the last expansion without
206 deleting the terminating character.
208 @findex expand-region-abbrevs
209 @kbd{M-x expand-region-abbrevs} searches through the region for defined
210 abbrevs, and for each one found offers to replace it with its expansion.
211 This command is useful if you have typed in text using abbrevs but forgot
212 to turn on Abbrev mode first. It may also be useful together with a
213 special set of abbrev definitions for making several global replacements at
214 once. This command is effective even if Abbrev mode is not enabled.
216 Expanding any abbrev first runs the hook @code{pre-abbrev-expand-hook}
219 @node Editing Abbrevs
220 @section Examining and Editing Abbrevs
223 @item M-x list-abbrevs
224 Display a list of all abbrev definitions. With a numeric argument, list
226 @item M-x edit-abbrevs
227 Edit a list of abbrevs; you can add, alter or remove definitions.
231 The output from @kbd{M-x list-abbrevs} looks like this:
234 @var{various other tables@dots{}}
235 (lisp-mode-abbrev-table)
237 (global-abbrev-table)
242 (Some blank lines of no semantic significance, and some other abbrev
243 tables, have been omitted.)
245 A line containing a name in parentheses is the header for abbrevs in a
246 particular abbrev table; @code{global-abbrev-table} contains all the global
247 abbrevs, and the other abbrev tables that are named after major modes
248 contain the mode-specific abbrevs.
250 Within each abbrev table, each nonblank line defines one abbrev. The
251 word at the beginning of the line is the abbrev. The number that
252 follows is the number of times the abbrev has been expanded. Emacs
253 keeps track of this to help you see which abbrevs you actually use, so
254 that you can eliminate those that you don't use often. The string at
255 the end of the line is the expansion.
257 Some abbrevs are marked with @samp{(sys)}. These ``system'' abbrevs
258 (@pxref{Abbrevs,,, elisp, The Emacs Lisp Reference Manual}) are
259 pre-defined by various modes, and are not saved to your abbrev file.
260 To disable a ``system'' abbrev, define an abbrev of the same name that
261 expands to itself, and save it to your abbrev file.
264 @kindex C-c C-c @r{(Edit Abbrevs)}
265 @kbd{M-x edit-abbrevs} allows you to add, change or kill abbrev
266 definitions by editing a list of them in an Emacs buffer. The list has
267 the same format described above. The buffer of abbrevs is called
268 @samp{*Abbrevs*}, and is in Edit-Abbrevs mode. Type @kbd{C-c C-c} in
269 this buffer to install the abbrev definitions as specified in the
270 buffer---and delete any abbrev definitions not listed.
272 The command @code{edit-abbrevs} is actually the same as
273 @code{list-abbrevs} except that it selects the buffer @samp{*Abbrevs*}
274 whereas @code{list-abbrevs} merely displays it in another window.
277 @section Saving Abbrevs
279 These commands allow you to keep abbrev definitions between editing
283 @item M-x write-abbrev-file @key{RET} @var{file} @key{RET}
284 Write a file @var{file} describing all defined abbrevs.
285 @item M-x read-abbrev-file @key{RET} @var{file} @key{RET}
286 Read the file @var{file} and define abbrevs as specified therein.
287 @item M-x quietly-read-abbrev-file @key{RET} @var{file} @key{RET}
288 Similar but do not display a message about what is going on.
289 @item M-x define-abbrevs
290 Define abbrevs from definitions in current buffer.
291 @item M-x insert-abbrevs
292 Insert all abbrevs and their expansions into current buffer.
295 @findex write-abbrev-file
296 @kbd{M-x write-abbrev-file} reads a file name using the minibuffer and
297 then writes a description of all current abbrev definitions into that
298 file. This is used to save abbrev definitions for use in a later
299 session. The text stored in the file is a series of Lisp expressions
300 that, when executed, define the same abbrevs that you currently have.
302 @findex read-abbrev-file
303 @findex quietly-read-abbrev-file
304 @vindex abbrev-file-name
306 @kbd{M-x read-abbrev-file} reads a file name using the minibuffer
307 and then reads the file, defining abbrevs according to the contents of
308 the file. The function @code{quietly-read-abbrev-file} is similar
309 except that it does not display a message in the echo area; you cannot
310 invoke it interactively, and it is used primarily in the @file{.emacs}
311 file. If either of these functions is called with @code{nil} as the
312 argument, it uses the file name specified in the variable
313 @code{abbrev-file-name}, which is by default @code{"~/.abbrev_defs"}.
314 That file is your standard abbrev definition file, and Emacs loads
315 abbrevs from it automatically when it starts up. (As an exception,
316 Emacs does not load the abbrev file when it is started in batch mode.
317 @xref{Initial Options}, for a description of batch mode.)
320 Emacs will offer to save abbrevs automatically if you have changed
321 any of them, whenever it offers to save all files (for @kbd{C-x s} or
322 @kbd{C-x C-c}). It saves them in the file specified by
323 @code{abbrev-file-name}. This feature can be inhibited by setting the
324 variable @code{save-abbrevs} to @code{nil}.
326 @findex insert-abbrevs
327 @findex define-abbrevs
328 The commands @kbd{M-x insert-abbrevs} and @kbd{M-x define-abbrevs} are
329 similar to the previous commands but work on text in an Emacs buffer.
330 @kbd{M-x insert-abbrevs} inserts text into the current buffer after point,
331 describing all current abbrev definitions; @kbd{M-x define-abbrevs} parses
332 the entire current buffer and defines abbrevs accordingly.
334 @node Dynamic Abbrevs
335 @section Dynamic Abbrev Expansion
337 The abbrev facility described above operates automatically as you
338 insert text, but all abbrevs must be defined explicitly. By contrast,
339 @dfn{dynamic abbrevs} allow the meanings of abbreviations to be
340 determined automatically from the contents of the buffer, but dynamic
341 abbrev expansion happens only when you request it explicitly.
345 @findex dabbrev-expand
346 @findex dabbrev-completion
349 Expand the word in the buffer before point as a @dfn{dynamic abbrev},
350 by searching in the buffer for words starting with that abbreviation
351 (@code{dabbrev-expand}).
354 Complete the word before point as a dynamic abbrev
355 (@code{dabbrev-completion}).
358 @vindex dabbrev-limit
359 For example, if the buffer contains @samp{does this follow } and you
360 type @kbd{f o M-/}, the effect is to insert @samp{follow} because that
361 is the last word in the buffer that starts with @samp{fo}. A numeric
362 argument to @kbd{M-/} says to take the second, third, etc.@: distinct
363 expansion found looking backward from point. Repeating @kbd{M-/}
364 searches for an alternative expansion by looking farther back. After
365 scanning all the text before point, it searches the text after point.
366 The variable @code{dabbrev-limit}, if non-@code{nil}, specifies how far
367 away in the buffer to search for an expansion.
369 @vindex dabbrev-check-all-buffers
370 After scanning the current buffer, @kbd{M-/} normally searches other
371 buffers, unless you have set @code{dabbrev-check-all-buffers} to
374 @vindex dabbrev-ignored-buffer-regexps
375 For finer control over which buffers to scan, customize the variable
376 @code{dabbrev-ignored-buffer-regexps}. Its value is a list of regular
377 expressions. If a buffer's name matches any of these regular
378 expressions, dynamic abbrev expansion skips that buffer.
380 A negative argument to @kbd{M-/}, as in @kbd{C-u - M-/}, says to
381 search first for expansions after point, then other buffers, and
382 consider expansions before point only as a last resort. If you repeat
383 the @kbd{M-/} to look for another expansion, do not specify an
384 argument. Repeating @kbd{M-/} cycles through all the expansions after
385 point and then the expansions before point.
387 After you have expanded a dynamic abbrev, you can copy additional
388 words that follow the expansion in its original context. Simply type
389 @kbd{@key{SPC} M-/} for each additional word you want to copy. The
390 spacing and punctuation between words is copied along with the words.
392 The command @kbd{C-M-/} (@code{dabbrev-completion}) performs
393 completion of a dynamic abbrev. Instead of trying the possible
394 expansions one by one, it finds all of them, then inserts the text
395 that they have in common. If they have nothing in common, @kbd{C-M-/}
396 displays a list of completions, from which you can select a choice in
397 the usual manner. @xref{Completion}.
399 Dynamic abbrev expansion is completely independent of Abbrev mode; the
400 expansion of a word with @kbd{M-/} is completely independent of whether
401 it has a definition as an ordinary abbrev.
403 @node Dabbrev Customization
404 @section Customizing Dynamic Abbreviation
406 Normally, dynamic abbrev expansion ignores case when searching for
407 expansions. That is, the expansion need not agree in case with the word
410 @vindex dabbrev-case-fold-search
411 This feature is controlled by the variable
412 @code{dabbrev-case-fold-search}. If it is @code{t}, case is ignored in
413 this search; if it is @code{nil}, the word and the expansion must match
414 in case. If the value of @code{dabbrev-case-fold-search} is
415 @code{case-fold-search}, which is true by default, then the variable
416 @code{case-fold-search} controls whether to ignore case while searching
419 @vindex dabbrev-case-replace
420 Normally, dynamic abbrev expansion preserves the case pattern
421 @emph{of the dynamic abbrev you are expanding}, by converting the
422 expansion to that case pattern.
424 @vindex dabbrev-case-fold-search
425 The variable @code{dabbrev-case-replace} controls whether to
426 preserve the case pattern of the dynamic abbrev. If it is @code{t},
427 the dynamic abbrev's case pattern is preserved in most cases; if it is
428 @code{nil}, the expansion is always copied verbatim. If the value of
429 @code{dabbrev-case-replace} is @code{case-replace}, which is true by
430 default, then the variable @code{case-replace} controls whether to
431 copy the expansion verbatim.
433 However, if the expansion contains a complex mixed case pattern, and
434 the dynamic abbrev matches this pattern as far as it goes, then the
435 expansion is always copied verbatim, regardless of those variables.
436 Thus, for example, if the buffer contains
437 @code{variableWithSillyCasePattern}, and you type @kbd{v a M-/}, it
438 copies the expansion verbatim including its case pattern.
440 @vindex dabbrev-abbrev-char-regexp
441 The variable @code{dabbrev-abbrev-char-regexp}, if non-@code{nil},
442 controls which characters are considered part of a word, for dynamic expansion
443 purposes. The regular expression must match just one character, never
444 two or more. The same regular expression also determines which
445 characters are part of an expansion. The value @code{nil} has a special
446 meaning: dynamic abbrevs are made of word characters, but expansions are
447 made of word and symbol characters.
449 @vindex dabbrev-abbrev-skip-leading-regexp
450 In shell scripts and makefiles, a variable name is sometimes prefixed
451 with @samp{$} and sometimes not. Major modes for this kind of text can
452 customize dynamic abbrev expansion to handle optional prefixes by setting
453 the variable @code{dabbrev-abbrev-skip-leading-regexp}. Its value
454 should be a regular expression that matches the optional prefix that
455 dynamic abbrev expression should ignore.
458 arch-tag: 638e0079-9540-48ec-9166-414083e16445