1 @c This is part of the Emacs manual.
2 @c Copyright (C) 1985-1987, 1993-1995, 1997, 2001-2017 Free Software
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 A third kind, @dfn{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 that 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 typing @kbd{f o o .} will
45 insert @samp{find outer otter.}.
50 Abbrevs expand only when Abbrev mode, a buffer-local minor mode, is
51 enabled. Disabling Abbrev mode does not cause abbrev definitions to
52 be forgotten, but they do not expand until Abbrev mode is enabled
53 again. The command @kbd{M-x abbrev-mode} toggles Abbrev mode; with a
54 numeric argument, it turns Abbrev mode on if the argument is positive,
55 off otherwise. @xref{Minor Modes}.
57 Abbrevs can have @dfn{mode-specific} definitions, active only in one major
58 mode. Abbrevs can also have @dfn{global} definitions that are active in
59 all major modes. The same abbrev can have a global definition and various
60 mode-specific definitions for different major modes. A mode-specific
61 definition for the current major mode overrides a global definition.
63 You can define abbrevs interactively during the editing session,
64 irrespective of whether Abbrev mode is enabled. You can also save
65 lists of abbrev definitions in files, which you can the reload for use
68 @node Defining Abbrevs
69 @section Defining Abbrevs
73 Define an abbrev, using one or more words before point as its expansion
74 (@code{add-global-abbrev}).
76 Similar, but define an abbrev specific to the current major mode
77 (@code{add-mode-abbrev}).
79 Define a word in the buffer as an abbrev (@code{inverse-add-global-abbrev}).
81 Define a word in the buffer as a mode-specific abbrev
82 (@code{inverse-add-mode-abbrev}).
83 @item M-x define-global-abbrev @key{RET} @var{abbrev} @key{RET} @var{exp} @key{RET}
84 Define @var{abbrev} as an abbrev expanding into @var{exp}.
85 @item M-x define-mode-abbrev @key{RET} @var{abbrev} @key{RET} @var{exp} @key{RET}
86 Define @var{abbrev} as a mode-specific abbrev expanding into @var{exp}.
87 @item M-x kill-all-abbrevs
88 Discard all abbrev definitions, leaving a blank slate.
92 @findex add-global-abbrev
93 The usual way to define an abbrev is to enter the text you want the
94 abbrev to expand to, position point after it, and type @kbd{C-x a g}
95 (@code{add-global-abbrev}). This reads the abbrev itself using the
96 minibuffer, and then defines it as an abbrev for one or more words before
97 point. Use a numeric argument to say how many words before point should be
98 taken as the expansion. For example, to define the abbrev @samp{foo} as
99 mentioned above, insert the text @samp{find outer otter} and then type
100 @kbd{C-u 3 C-x a g f o o @key{RET}}.
102 An argument of zero to @kbd{C-x a g} means to use the contents of the
103 region as the expansion of the abbrev being defined.
106 @findex add-mode-abbrev
107 The command @kbd{C-x a l} (@code{add-mode-abbrev}) is similar, but
108 defines a mode-specific abbrev for the current major mode. The
109 arguments work the same as for @kbd{C-x a g}.
112 @findex inverse-add-global-abbrev
114 @findex inverse-add-mode-abbrev
115 @kbd{C-x a i g} (@code{inverse-add-global-abbrev}) and @kbd{C-x a i
116 l} (@code{inverse-add-mode-abbrev}) perform the opposite task: if the
117 abbrev text is already in the buffer, you use these commands to define
118 an abbrev by specifying the expansion in the minibuffer. These
119 commands will expand the abbrev text used for the definition.
121 @findex define-mode-abbrev
122 @findex define-global-abbrev
123 You can define an abbrev without inserting either the abbrev or its
124 expansion in the buffer using the command @code{define-global-abbrev}.
125 It reads two arguments---the abbrev, and its expansion. The command
126 @code{define-mode-abbrev} does likewise for a mode-specific abbrev.
128 To change the definition of an abbrev, just make a new definition.
129 When an abbrev has a prior definition, the abbrev definition commands
130 ask for confirmation before replacing it.
132 @findex kill-all-abbrevs
133 To remove an abbrev definition, give a negative argument to the
134 abbrev definition command: @kbd{C-u - C-x a g} or @kbd{C-u - C-x a l}.
135 The former removes a global definition, while the latter removes a
136 mode-specific definition. @kbd{M-x kill-all-abbrevs} removes all
137 abbrev definitions, both global and local.
139 @node Expanding Abbrevs
140 @section Controlling Abbrev Expansion
142 When Abbrev mode is enabled, an abbrev expands whenever it is
143 present in the buffer just before point and you type a self-inserting
144 whitespace or punctuation character (@key{SPC}, comma, etc.). More
145 precisely, any character that is not a word constituent expands an
146 abbrev, and any word-constituent character can be part of an abbrev.
147 The most common way to use an abbrev is to insert it and then insert a
148 punctuation or whitespace character to expand it.
150 @vindex abbrev-all-caps
151 Abbrev expansion preserves case: @samp{foo} expands to @samp{find
152 outer otter}, and @samp{Foo} to @samp{Find outer otter}. @samp{FOO}
153 expands to @samp{Find Outer Otter} by default, but if you change the
154 variable @code{abbrev-all-caps} to a non-@code{nil} value, it expands
155 to @samp{FIND OUTER OTTER}.
157 These commands are used to control abbrev expansion:
161 Separate a prefix from a following abbrev to be expanded
162 (@code{abbrev-prefix-mark}).
164 @findex expand-abbrev
165 Expand the abbrev before point (@code{expand-abbrev}).
166 This is effective even when Abbrev mode is not enabled.
167 @item M-x expand-region-abbrevs
168 Expand some or all abbrevs found in the region.
172 @findex abbrev-prefix-mark
173 You may wish to expand an abbrev and attach a prefix to the expansion;
174 for example, if @samp{cnst} expands into @samp{construction}, you might want
175 to use it to enter @samp{reconstruction}. It does not work to type
176 @kbd{recnst}, because that is not necessarily a defined abbrev. What
177 you can do is use the command @kbd{M-'} (@code{abbrev-prefix-mark}) in
178 between the prefix @samp{re} and the abbrev @samp{cnst}. First, insert
179 @samp{re}. Then type @kbd{M-'}; this inserts a hyphen in the buffer to
180 indicate that it has done its work. Then insert the abbrev @samp{cnst};
181 the buffer now contains @samp{re-cnst}. Now insert a non-word character
182 to expand the abbrev @samp{cnst} into @samp{construction}. This
183 expansion step also deletes the hyphen that indicated @kbd{M-'} had been
184 used. The result is the desired @samp{reconstruction}.
186 If you actually want the text of the abbrev in the buffer, rather than
187 its expansion, you can accomplish this by inserting the following
188 punctuation with @kbd{C-q}. Thus, @kbd{foo C-q ,} leaves @samp{foo,} in
189 the buffer, not expanding it.
191 @findex unexpand-abbrev
192 If you expand an abbrev by mistake, you can undo the expansion by
193 typing @kbd{C-/} (@code{undo}). @xref{Undo}. This undoes the
194 insertion of the abbrev expansion and brings back the abbrev text. If
195 the result you want is the terminating non-word character plus the
196 unexpanded abbrev, you must reinsert the terminating character,
197 quoting it with @kbd{C-q}. You can also use the command @kbd{M-x
198 unexpand-abbrev} to cancel the last expansion without deleting the
199 terminating character.
201 @findex expand-region-abbrevs
202 @kbd{M-x expand-region-abbrevs} searches through the region for defined
203 abbrevs, and for each one found offers to replace it with its expansion.
204 This command is useful if you have typed in text using abbrevs but forgot
205 to turn on Abbrev mode first. It may also be useful together with a
206 special set of abbrev definitions for making several global replacements at
207 once. This command is effective even if Abbrev mode is not enabled.
209 The function @code{expand-abbrev} performs the expansion by calling
210 the function that @code{abbrev-expand-function} specifies. By
211 changing this function you can make arbitrary changes to
212 the abbrev expansion. @xref{Abbrev Expansion,,, elisp, The Emacs Lisp
215 @node Editing Abbrevs
216 @section Examining and Editing Abbrevs
219 @item M-x list-abbrevs
220 Display a list of all abbrev definitions. With a numeric argument, list
222 @item M-x edit-abbrevs
223 Edit a list of abbrevs; you can add, alter or remove definitions.
227 The output from @kbd{M-x list-abbrevs} looks like this:
230 @var{various other tables@dots{}}
231 (lisp-mode-abbrev-table)
233 (global-abbrev-table)
238 (Some blank lines of no semantic significance, and some other abbrev
239 tables, have been omitted.)
241 A line containing a name in parentheses is the header for abbrevs in a
242 particular abbrev table; @code{global-abbrev-table} contains all the global
243 abbrevs, and the other abbrev tables that are named after major modes
244 contain the mode-specific abbrevs.
246 Within each abbrev table, each nonblank line defines one abbrev. The
247 word at the beginning of the line is the abbrev. The number that
248 follows is the number of times the abbrev has been expanded. Emacs
249 keeps track of this to help you see which abbrevs you actually use, so
250 that you can eliminate those that you don't use often. The string at
251 the end of the line is the expansion.
253 Some abbrevs are marked with @samp{(sys)}. These @dfn{system abbrevs}
254 (@pxref{Abbrevs,,, elisp, The Emacs Lisp Reference Manual}) are
255 pre-defined by various modes, and are not saved to your abbrev file.
256 To disable a system abbrev, define an abbrev of the same name that
257 expands to itself, and save it to your abbrev file.
260 @kindex C-c C-c @r{(Edit Abbrevs)}
261 @kbd{M-x edit-abbrevs} allows you to add, change or kill abbrev
262 definitions by editing a list of them in an Emacs buffer. The list has
263 the same format described above. The buffer of abbrevs is called
264 @file{*Abbrevs*}, and is in Edit-Abbrevs mode. Type @kbd{C-c C-c} in
265 this buffer to install the abbrev definitions as specified in the
266 buffer---and delete any abbrev definitions not listed.
268 The command @code{edit-abbrevs} is actually the same as
269 @code{list-abbrevs} except that it selects the buffer @file{*Abbrevs*}
270 whereas @code{list-abbrevs} merely displays it in another window.
273 @section Saving Abbrevs
275 These commands allow you to keep abbrev definitions between editing
279 @item M-x write-abbrev-file @key{RET} @var{file} @key{RET}
280 Write a file @var{file} describing all defined abbrevs.
281 @item M-x read-abbrev-file @key{RET} @var{file} @key{RET}
282 Read the file @var{file} and define abbrevs as specified therein.
283 @item M-x define-abbrevs
284 Define abbrevs from definitions in current buffer.
285 @item M-x insert-abbrevs
286 Insert all abbrevs and their expansions into current buffer.
289 @findex write-abbrev-file
290 @kbd{M-x write-abbrev-file} reads a file name using the minibuffer and
291 then writes a description of all current abbrev definitions into that
292 file. This is used to save abbrev definitions for use in a later
293 session. The text stored in the file is a series of Lisp expressions
294 that, when executed, define the same abbrevs that you currently have.
296 @findex read-abbrev-file
297 @findex quietly-read-abbrev-file
298 @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 your init file
305 (@pxref{Init File}). If either of these functions is called with
306 @code{nil} as the argument, it uses the file given by the variable
307 @code{abbrev-file-name}, which is @file{~/.emacs.d/abbrev_defs} by
308 default. This is your standard abbrev definition file, and Emacs
309 loads abbrevs from it automatically when it starts up. (As an
310 exception, Emacs does not load the abbrev file when it is started in
311 batch mode. @xref{Initial Options}, for a description of batch mode.)
314 Emacs will offer to save abbrevs automatically if you have changed
315 any of them, whenever it offers to save all files (for @kbd{C-x s} or
316 @kbd{C-x C-c}). It saves them in the file specified by
317 @code{abbrev-file-name}. This feature can be inhibited by setting the
318 variable @code{save-abbrevs} to @code{nil}.
320 @findex insert-abbrevs
321 @findex define-abbrevs
322 The commands @kbd{M-x insert-abbrevs} and @kbd{M-x define-abbrevs} are
323 similar to the previous commands but work on text in an Emacs buffer.
324 @kbd{M-x insert-abbrevs} inserts text into the current buffer after point,
325 describing all current abbrev definitions; @kbd{M-x define-abbrevs} parses
326 the entire current buffer and defines abbrevs accordingly.
328 @node Dynamic Abbrevs
329 @section Dynamic Abbrev Expansion
331 The abbrev facility described above operates automatically as you
332 insert text, but all abbrevs must be defined explicitly. By contrast,
333 @dfn{dynamic abbrevs} allow the meanings of abbreviations to be
334 determined automatically from the contents of the buffer, but dynamic
335 abbrev expansion happens only when you request it explicitly.
339 @findex dabbrev-expand
340 @findex dabbrev-completion
343 Expand the word in the buffer before point as a @dfn{dynamic abbrev},
344 by searching for words starting with that abbreviation
345 (@code{dabbrev-expand}).
348 Complete the word before point as a dynamic abbrev
349 (@code{dabbrev-completion}).
352 @vindex dabbrev-limit
353 For example, if the buffer contains @samp{does this follow } and you
354 type @kbd{f o M-/}, the effect is to insert @samp{follow} because that
355 is the last word in the buffer that starts with @samp{fo}. A numeric
356 argument to @kbd{M-/} says to take the second, third, etc.@: distinct
357 expansion found looking backward from point. Repeating @kbd{M-/}
358 searches for an alternative expansion by looking farther back. After
359 scanning all the text before point, it searches the text after point.
360 The variable @code{dabbrev-limit}, if non-@code{nil}, specifies how far
361 away in the buffer to search for an expansion.
363 @vindex dabbrev-check-all-buffers
364 @vindex dabbrev-check-other-buffers
365 After scanning the current buffer, @kbd{M-/} normally searches other
366 buffers. The variables @code{dabbrev-check-all-buffers} and
367 @code{dabbrev-check-other-buffers} can be used to determine which
368 other buffers, if any, are searched.
370 @vindex dabbrev-ignored-buffer-names
371 @vindex dabbrev-ignored-buffer-regexps
372 For finer control over which buffers to scan, customize the
373 variables @code{dabbrev-ignored-buffer-names} and
374 @code{dabbrev-ignored-buffer-regexps}. The value of the former is a
375 list of buffer names to skip. The value of the latter is a list of
376 regular expressions; if a buffer's name matches any of these regular
377 expressions, dynamic abbrev expansion skips that buffer.
379 A negative argument to @kbd{M-/}, as in @kbd{C-u - M-/}, says to
380 search first for expansions after point, then other buffers, and
381 consider expansions before point only as a last resort. If you repeat
382 the @kbd{M-/} to look for another expansion, do not specify an
383 argument. Repeating @kbd{M-/} cycles through all the expansions after
384 point and then the expansions before point.
386 After you have expanded a dynamic abbrev, you can copy additional
387 words that follow the expansion in its original context. Simply type
388 @kbd{@key{SPC} M-/} for each additional word you want to copy. The
389 spacing and punctuation between words is copied along with the words.
391 You can control the way @kbd{M-/} determines the word to expand and
392 how to expand it, see @ref{Dabbrev Customization}.
394 The command @kbd{C-M-/} (@code{dabbrev-completion}) performs
395 completion of a dynamic abbrev. Instead of trying the possible
396 expansions one by one, it finds all of them, then inserts the text
397 that they have in common. If they have nothing in common, @kbd{C-M-/}
398 displays a list of completions, from which you can select a choice in
399 the usual manner. @xref{Completion}.
401 Dynamic abbrev expansion is completely independent of Abbrev mode; the
402 expansion of a word with @kbd{M-/} is completely independent of whether
403 it has a definition as an ordinary abbrev.
405 @node Dabbrev Customization
406 @section Customizing Dynamic Abbreviation
408 Normally, dynamic abbrev expansion ignores case when searching for
409 expansions. That is, the expansion need not agree in case with the word
412 @vindex dabbrev-case-fold-search
413 This feature is controlled by the variable
414 @code{dabbrev-case-fold-search}. If it is @code{t}, case is ignored
415 in this search; if it is @code{nil}, the word and the expansion must
416 match in case. If the value is @code{case-fold-search} (the default),
417 then the variable @code{case-fold-search} controls whether to ignore
418 case while searching for expansions (@pxref{Lax Search}).
420 @vindex dabbrev-case-replace
421 Normally, dynamic abbrev expansion preserves the case pattern
422 @emph{of the dynamic abbrev you are expanding}, by converting the
423 expansion to that case pattern.
425 @vindex dabbrev-case-fold-search
426 The variable @code{dabbrev-case-replace} controls whether to
427 preserve the case pattern of the dynamic abbrev. If it is @code{t},
428 the dynamic abbrev's case pattern is preserved in most cases; if it is
429 @code{nil}, the expansion is always copied verbatim. If the value is
430 @code{case-replace} (the default), then the variable
431 @code{case-replace} controls whether to copy the expansion verbatim
432 (@pxref{Replacement and Lax Matches}).
434 However, if the expansion contains a complex mixed case pattern, and
435 the dynamic abbrev matches this pattern as far as it goes, then the
436 expansion is always copied verbatim, regardless of those variables.
437 Thus, for example, if the buffer contains
438 @code{variableWithSillyCasePattern}, and you type @kbd{v a M-/}, it
439 copies the expansion verbatim including its case pattern.
441 @vindex dabbrev-abbrev-char-regexp
442 The variable @code{dabbrev-abbrev-char-regexp}, if non-@code{nil},
443 controls which characters are considered part of a word, for dynamic
444 expansion purposes. The regular expression must match just one
445 character, never two or more. The same regular expression also
446 determines which characters are part of an expansion. The (default)
447 value @code{nil} has a special meaning: dynamic abbrevs (i.e.@: the
448 word at point) are made of word characters, but their expansions are
449 looked for as sequences of word and symbol characters. This is
450 generally appropriate for expanding symbols in a program source and
451 also for human-readable text in many languages, but may not be what
452 you want in a text buffer that includes unusual punctuation characters;
453 in that case, the value @code{"\\sw"} might produce better results.
455 @vindex dabbrev-abbrev-skip-leading-regexp
456 In shell scripts and makefiles, a variable name is sometimes prefixed
457 with @samp{$} and sometimes not. Major modes for this kind of text can
458 customize dynamic abbrev expansion to handle optional prefixes by setting
459 the variable @code{dabbrev-abbrev-skip-leading-regexp}. Its value
460 should be a regular expression that matches the optional prefix that
461 dynamic abbrev expression should ignore. The default is @code{nil},
462 which means no characters should be skipped.