Update copyright year to 2015
[emacs.git] / doc / emacs / abbrevs.texi
bloba3cb989320ae242ab50529c20718a1106fef7171
1 @c This is part of the Emacs manual.
2 @c Copyright (C) 1985-1987, 1993-1995, 1997, 2001-2015 Free Software
3 @c Foundation, Inc.
4 @c See file emacs.texi for copying conditions.
5 @node Abbrevs
6 @chapter Abbrevs
7 @cindex abbrevs
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
15 @key{SPC}}.
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
21 Abbrevs}.
23   ``Hippie'' expansion generalizes abbreviation expansion.
24 @xref{Hippie Expand, , Hippie Expansion, autotype, Features for
25 Automatic Typing}.
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
37 @node Abbrev Concepts
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.}.
47 @findex abbrev-mode
48 @cindex Abbrev mode
49 @cindex mode, Abbrev
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
66 in later sessions.
68 @node Defining Abbrevs
69 @section Defining Abbrevs
71 @table @kbd
72 @item C-x a g
73 Define an abbrev, using one or more words before point as its expansion
74 (@code{add-global-abbrev}).
75 @item C-x a l
76 Similar, but define an abbrev specific to the current major mode
77 (@code{add-mode-abbrev}).
78 @item C-x a i g
79 Define a word in the buffer as an abbrev (@code{inverse-add-global-abbrev}).
80 @item C-x a i l
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.
89 @end table
91 @kindex C-x a g
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.
105 @kindex C-x a l
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}.
111 @kindex C-x a i g
112 @findex inverse-add-global-abbrev
113 @kindex C-x a i l
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:
159 @table @kbd
160 @item M-'
161 Separate a prefix from a following abbrev to be expanded
162 (@code{abbrev-prefix-mark}).
163 @item C-x a e
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.
169 @end table
171 @kindex M-'
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
213 Reference Manual}.
215 @node Editing Abbrevs
216 @section Examining and Editing Abbrevs
218 @table @kbd
219 @item M-x list-abbrevs
220 Display a list of all abbrev definitions.  With a numeric argument, list
221 only local abbrevs.
222 @item M-x edit-abbrevs
223 Edit a list of abbrevs; you can add, alter or remove definitions.
224 @end table
226 @findex list-abbrevs
227   The output from @kbd{M-x list-abbrevs} looks like this:
229 @example
230 @var{various other tables@dots{}}
231 (lisp-mode-abbrev-table)
232 "dk"          0    "define-key"
233 (global-abbrev-table)
234 "dfn"         0    "definition"
235 @end example
237 @noindent
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 ``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.
259 @findex edit-abbrevs
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.
272 @node Saving Abbrevs
273 @section Saving Abbrevs
275   These commands allow you to keep abbrev definitions between editing
276 sessions.
278 @table @kbd
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.
287 @end table
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
299 @cindex abbrev file
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.)
313 @vindex save-abbrevs
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.
337 @kindex M-/
338 @kindex C-M-/
339 @findex dabbrev-expand
340 @findex dabbrev-completion
341 @table @kbd
342 @item M-/
343 Expand the word in the buffer before point as a @dfn{dynamic abbrev},
344 by searching in the buffer for words starting with that abbreviation
345 (@code{dabbrev-expand}).
347 @item C-M-/
348 Complete the word before point as a dynamic abbrev
349 (@code{dabbrev-completion}).
350 @end table
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   After scanning the current buffer, @kbd{M-/} normally searches other
365 buffers, unless you have set @code{dabbrev-check-all-buffers} to
366 @code{nil}.
368 @vindex dabbrev-ignored-buffer-regexps
369   For finer control over which buffers to scan, customize the variable
370 @code{dabbrev-ignored-buffer-regexps}.  Its value is a list of regular
371 expressions.  If a buffer's name matches any of these regular
372 expressions, dynamic abbrev expansion skips that buffer.
374   A negative argument to @kbd{M-/}, as in @kbd{C-u - M-/}, says to
375 search first for expansions after point, then other buffers, and
376 consider expansions before point only as a last resort.  If you repeat
377 the @kbd{M-/} to look for another expansion, do not specify an
378 argument.  Repeating @kbd{M-/} cycles through all the expansions after
379 point and then the expansions before point.
381   After you have expanded a dynamic abbrev, you can copy additional
382 words that follow the expansion in its original context.  Simply type
383 @kbd{@key{SPC} M-/} for each additional word you want to copy.  The
384 spacing and punctuation between words is copied along with the words.
386   The command @kbd{C-M-/} (@code{dabbrev-completion}) performs
387 completion of a dynamic abbrev.  Instead of trying the possible
388 expansions one by one, it finds all of them, then inserts the text
389 that they have in common.  If they have nothing in common, @kbd{C-M-/}
390 displays a list of completions, from which you can select a choice in
391 the usual manner.  @xref{Completion}.
393   Dynamic abbrev expansion is completely independent of Abbrev mode; the
394 expansion of a word with @kbd{M-/} is completely independent of whether
395 it has a definition as an ordinary abbrev.
397 @node Dabbrev Customization
398 @section Customizing Dynamic Abbreviation
400   Normally, dynamic abbrev expansion ignores case when searching for
401 expansions.  That is, the expansion need not agree in case with the word
402 you are expanding.
404 @vindex dabbrev-case-fold-search
405   This feature is controlled by the variable
406 @code{dabbrev-case-fold-search}.  If it is @code{t}, case is ignored
407 in this search; if it is @code{nil}, the word and the expansion must
408 match in case.  If the value is @code{case-fold-search} (the default),
409 then the variable @code{case-fold-search} controls whether to ignore
410 case while searching for expansions (@pxref{Search Case}).
412 @vindex dabbrev-case-replace
413   Normally, dynamic abbrev expansion preserves the case pattern
414 @emph{of the dynamic abbrev you are expanding}, by converting the
415 expansion to that case pattern.
417 @vindex dabbrev-case-fold-search
418   The variable @code{dabbrev-case-replace} controls whether to
419 preserve the case pattern of the dynamic abbrev.  If it is @code{t},
420 the dynamic abbrev's case pattern is preserved in most cases; if it is
421 @code{nil}, the expansion is always copied verbatim.  If the value is
422 @code{case-replace} (the default), then the variable
423 @code{case-replace} controls whether to copy the expansion verbatim
424 (@pxref{Replacement and Case}).
426   However, if the expansion contains a complex mixed case pattern, and
427 the dynamic abbrev matches this pattern as far as it goes, then the
428 expansion is always copied verbatim, regardless of those variables.
429 Thus, for example, if the buffer contains
430 @code{variableWithSillyCasePattern}, and you type @kbd{v a M-/}, it
431 copies the expansion verbatim including its case pattern.
433 @vindex dabbrev-abbrev-char-regexp
434   The variable @code{dabbrev-abbrev-char-regexp}, if non-@code{nil},
435 controls which characters are considered part of a word, for dynamic expansion
436 purposes.  The regular expression must match just one character, never
437 two or more.  The same regular expression also determines which
438 characters are part of an expansion.  The value @code{nil} has a special
439 meaning: dynamic abbrevs are made of word characters, but expansions are
440 made of word and symbol characters.
442 @vindex dabbrev-abbrev-skip-leading-regexp
443   In shell scripts and makefiles, a variable name is sometimes prefixed
444 with @samp{$} and sometimes not.  Major modes for this kind of text can
445 customize dynamic abbrev expansion to handle optional prefixes by setting
446 the variable @code{dabbrev-abbrev-skip-leading-regexp}.  Its value
447 should be a regular expression that matches the optional prefix that
448 dynamic abbrev expression should ignore.