Update copyright year to 2014 by running admin/update-copyright.
[bpt/emacs.git] / doc / emacs / abbrevs.texi
1 @c This is part of the Emacs manual.
2 @c Copyright (C) 1985-1987, 1993-1995, 1997, 2001-2014 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)
9
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}}.
16
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}.
22
23 ``Hippie'' expansion generalizes abbreviation expansion.
24 @xref{Hippie Expand, , Hippie Expansion, autotype, Features for
25 Automatic Typing}.
26
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 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.}.
46
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}.
56
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.
62
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.
67
68 @node Defining Abbrevs
69 @section Defining Abbrevs
70
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
90
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}}.
101
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.
104
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}.
110
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.
120
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.
127
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.
131
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.
138
139 @node Expanding Abbrevs
140 @section Controlling Abbrev Expansion
141
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.
149
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}.
156
157 These commands are used to control abbrev expansion:
158
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
170
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}.
185
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.
190
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.
200
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.
208
209 Expanding any abbrev runs @code{abbrev-expand-functions}, a special
210 hook. Functions in this special hook can make arbitrary changes to
211 the abbrev expansion. @xref{Abbrev Expansion,,, elisp, The Emacs Lisp
212 Reference Manual}.
213
214 @node Editing Abbrevs
215 @section Examining and Editing Abbrevs
216
217 @table @kbd
218 @item M-x list-abbrevs
219 Display a list of all abbrev definitions. With a numeric argument, list
220 only local abbrevs.
221 @item M-x edit-abbrevs
222 Edit a list of abbrevs; you can add, alter or remove definitions.
223 @end table
224
225 @findex list-abbrevs
226 The output from @kbd{M-x list-abbrevs} looks like this:
227
228 @example
229 @var{various other tables@dots{}}
230 (lisp-mode-abbrev-table)
231 "dk" 0 "define-key"
232 (global-abbrev-table)
233 "dfn" 0 "definition"
234 @end example
235
236 @noindent
237 (Some blank lines of no semantic significance, and some other abbrev
238 tables, have been omitted.)
239
240 A line containing a name in parentheses is the header for abbrevs in a
241 particular abbrev table; @code{global-abbrev-table} contains all the global
242 abbrevs, and the other abbrev tables that are named after major modes
243 contain the mode-specific abbrevs.
244
245 Within each abbrev table, each nonblank line defines one abbrev. The
246 word at the beginning of the line is the abbrev. The number that
247 follows is the number of times the abbrev has been expanded. Emacs
248 keeps track of this to help you see which abbrevs you actually use, so
249 that you can eliminate those that you don't use often. The string at
250 the end of the line is the expansion.
251
252 Some abbrevs are marked with @samp{(sys)}. These ``system'' abbrevs
253 (@pxref{Abbrevs,,, elisp, The Emacs Lisp Reference Manual}) are
254 pre-defined by various modes, and are not saved to your abbrev file.
255 To disable a ``system'' abbrev, define an abbrev of the same name that
256 expands to itself, and save it to your abbrev file.
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 @file{*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 @file{*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 define-abbrevs
283 Define abbrevs from definitions in current buffer.
284 @item M-x insert-abbrevs
285 Insert all abbrevs and their expansions into current buffer.
286 @end table
287
288 @findex write-abbrev-file
289 @kbd{M-x write-abbrev-file} reads a file name using the minibuffer and
290 then writes a description of all current abbrev definitions into that
291 file. This is used to save abbrev definitions for use in a later
292 session. The text stored in the file is a series of Lisp expressions
293 that, when executed, define the same abbrevs that you currently have.
294
295 @findex read-abbrev-file
296 @findex quietly-read-abbrev-file
297 @vindex abbrev-file-name
298 @cindex abbrev file
299 @kbd{M-x read-abbrev-file} reads a file name using the minibuffer
300 and then reads the file, defining abbrevs according to the contents of
301 the file. The function @code{quietly-read-abbrev-file} is similar
302 except that it does not display a message in the echo area; you cannot
303 invoke it interactively, and it is used primarily in your init file
304 (@pxref{Init File}). If either of these functions is called with
305 @code{nil} as the argument, it uses the file given by the variable
306 @code{abbrev-file-name}, which is @file{~/.emacs.d/abbrev_defs} by
307 default. This is your standard abbrev definition file, and Emacs
308 loads abbrevs from it automatically when it starts up. (As an
309 exception, Emacs does not load the abbrev file when it is started in
310 batch mode. @xref{Initial Options}, for a description of batch mode.)
311
312 @vindex save-abbrevs
313 Emacs will offer to save abbrevs automatically if you have changed
314 any of them, whenever it offers to save all files (for @kbd{C-x s} or
315 @kbd{C-x C-c}). It saves them in the file specified by
316 @code{abbrev-file-name}. This feature can be inhibited by setting the
317 variable @code{save-abbrevs} to @code{nil}.
318
319 @findex insert-abbrevs
320 @findex define-abbrevs
321 The commands @kbd{M-x insert-abbrevs} and @kbd{M-x define-abbrevs} are
322 similar to the previous commands but work on text in an Emacs buffer.
323 @kbd{M-x insert-abbrevs} inserts text into the current buffer after point,
324 describing all current abbrev definitions; @kbd{M-x define-abbrevs} parses
325 the entire current buffer and defines abbrevs accordingly.
326
327 @node Dynamic Abbrevs
328 @section Dynamic Abbrev Expansion
329
330 The abbrev facility described above operates automatically as you
331 insert text, but all abbrevs must be defined explicitly. By contrast,
332 @dfn{dynamic abbrevs} allow the meanings of abbreviations to be
333 determined automatically from the contents of the buffer, but dynamic
334 abbrev expansion happens only when you request it explicitly.
335
336 @kindex M-/
337 @kindex C-M-/
338 @findex dabbrev-expand
339 @findex dabbrev-completion
340 @table @kbd
341 @item M-/
342 Expand the word in the buffer before point as a @dfn{dynamic abbrev},
343 by searching in the buffer for words starting with that abbreviation
344 (@code{dabbrev-expand}).
345
346 @item C-M-/
347 Complete the word before point as a dynamic abbrev
348 (@code{dabbrev-completion}).
349 @end table
350
351 @vindex dabbrev-limit
352 For example, if the buffer contains @samp{does this follow } and you
353 type @kbd{f o M-/}, the effect is to insert @samp{follow} because that
354 is the last word in the buffer that starts with @samp{fo}. A numeric
355 argument to @kbd{M-/} says to take the second, third, etc.@: distinct
356 expansion found looking backward from point. Repeating @kbd{M-/}
357 searches for an alternative expansion by looking farther back. After
358 scanning all the text before point, it searches the text after point.
359 The variable @code{dabbrev-limit}, if non-@code{nil}, specifies how far
360 away in the buffer to search for an expansion.
361
362 @vindex dabbrev-check-all-buffers
363 After scanning the current buffer, @kbd{M-/} normally searches other
364 buffers, unless you have set @code{dabbrev-check-all-buffers} to
365 @code{nil}.
366
367 @vindex dabbrev-ignored-buffer-regexps
368 For finer control over which buffers to scan, customize the variable
369 @code{dabbrev-ignored-buffer-regexps}. Its value is a list of regular
370 expressions. If a buffer's name matches any of these regular
371 expressions, dynamic abbrev expansion skips that buffer.
372
373 A negative argument to @kbd{M-/}, as in @kbd{C-u - M-/}, says to
374 search first for expansions after point, then other buffers, and
375 consider expansions before point only as a last resort. If you repeat
376 the @kbd{M-/} to look for another expansion, do not specify an
377 argument. Repeating @kbd{M-/} cycles through all the expansions after
378 point and then the expansions before point.
379
380 After you have expanded a dynamic abbrev, you can copy additional
381 words that follow the expansion in its original context. Simply type
382 @kbd{@key{SPC} M-/} for each additional word you want to copy. The
383 spacing and punctuation between words is copied along with the words.
384
385 The command @kbd{C-M-/} (@code{dabbrev-completion}) performs
386 completion of a dynamic abbrev. Instead of trying the possible
387 expansions one by one, it finds all of them, then inserts the text
388 that they have in common. If they have nothing in common, @kbd{C-M-/}
389 displays a list of completions, from which you can select a choice in
390 the usual manner. @xref{Completion}.
391
392 Dynamic abbrev expansion is completely independent of Abbrev mode; the
393 expansion of a word with @kbd{M-/} is completely independent of whether
394 it has a definition as an ordinary abbrev.
395
396 @node Dabbrev Customization
397 @section Customizing Dynamic Abbreviation
398
399 Normally, dynamic abbrev expansion ignores case when searching for
400 expansions. That is, the expansion need not agree in case with the word
401 you are expanding.
402
403 @vindex dabbrev-case-fold-search
404 This feature is controlled by the variable
405 @code{dabbrev-case-fold-search}. If it is @code{t}, case is ignored
406 in this search; if it is @code{nil}, the word and the expansion must
407 match in case. If the value is @code{case-fold-search} (the default),
408 then the variable @code{case-fold-search} controls whether to ignore
409 case while searching for expansions (@pxref{Search Case}).
410
411 @vindex dabbrev-case-replace
412 Normally, dynamic abbrev expansion preserves the case pattern
413 @emph{of the dynamic abbrev you are expanding}, by converting the
414 expansion to that case pattern.
415
416 @vindex dabbrev-case-fold-search
417 The variable @code{dabbrev-case-replace} controls whether to
418 preserve the case pattern of the dynamic abbrev. If it is @code{t},
419 the dynamic abbrev's case pattern is preserved in most cases; if it is
420 @code{nil}, the expansion is always copied verbatim. If the value is
421 @code{case-replace} (the default), then the variable
422 @code{case-replace} controls whether to copy the expansion verbatim
423 (@pxref{Replacement and Case}).
424
425 However, if the expansion contains a complex mixed case pattern, and
426 the dynamic abbrev matches this pattern as far as it goes, then the
427 expansion is always copied verbatim, regardless of those variables.
428 Thus, for example, if the buffer contains
429 @code{variableWithSillyCasePattern}, and you type @kbd{v a M-/}, it
430 copies the expansion verbatim including its case pattern.
431
432 @vindex dabbrev-abbrev-char-regexp
433 The variable @code{dabbrev-abbrev-char-regexp}, if non-@code{nil},
434 controls which characters are considered part of a word, for dynamic expansion
435 purposes. The regular expression must match just one character, never
436 two or more. The same regular expression also determines which
437 characters are part of an expansion. The value @code{nil} has a special
438 meaning: dynamic abbrevs are made of word characters, but expansions are
439 made of word and symbol characters.
440
441 @vindex dabbrev-abbrev-skip-leading-regexp
442 In shell scripts and makefiles, a variable name is sometimes prefixed
443 with @samp{$} and sometimes not. Major modes for this kind of text can
444 customize dynamic abbrev expansion to handle optional prefixes by setting
445 the variable @code{dabbrev-abbrev-skip-leading-regexp}. Its value
446 should be a regular expression that matches the optional prefix that
447 dynamic abbrev expression should ignore.