declare smobs in alloc.c
[bpt/emacs.git] / doc / emacs / abbrevs.texi
CommitLineData
6bf7aab6 1@c This is part of the Emacs manual.
ba318903 2@c Copyright (C) 1985-1987, 1993-1995, 1997, 2001-2014 Free Software
ab422c4d 3@c Foundation, Inc.
6bf7aab6 4@c See file emacs.texi for copying conditions.
622a113e 5@node Abbrevs
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
eceeb5fc 40 An @dfn{abbrev} is a word that has been defined to @dfn{expand} into
6bf7aab6
DL
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
eceeb5fc
CY
44expanding to @samp{find outer otter}, then typing @kbd{f o o .} will
45insert @samp{find outer otter.}.
6bf7aab6
DL
46
47@findex abbrev-mode
6bf7aab6
DL
48@cindex Abbrev mode
49@cindex mode, Abbrev
c40a7de7
CY
50 Abbrevs expand only when Abbrev mode, a buffer-local minor mode, is
51enabled. Disabling Abbrev mode does not cause abbrev definitions to
52be forgotten, but they do not expand until Abbrev mode is enabled
53again. The command @kbd{M-x abbrev-mode} toggles Abbrev mode; with a
54numeric argument, it turns Abbrev mode on if the argument is positive,
55off otherwise. @xref{Minor Modes}.
6bf7aab6 56
3f7ba267 57 Abbrevs can have @dfn{mode-specific} definitions, active only in one major
6bf7aab6
DL
58mode. Abbrevs can also have @dfn{global} definitions that are active in
59all major modes. The same abbrev can have a global definition and various
60mode-specific definitions for different major modes. A mode-specific
61definition for the current major mode overrides a global definition.
62
ea16568d 63 You can define abbrevs interactively during the editing session,
eceeb5fc
CY
64irrespective of whether Abbrev mode is enabled. You can also save
65lists of abbrev definitions in files, which you can the reload for use
66in later sessions.
6bf7aab6
DL
67
68@node Defining Abbrevs
69@section Defining Abbrevs
70
71@table @kbd
72@item C-x a g
73Define an abbrev, using one or more words before point as its expansion
74(@code{add-global-abbrev}).
75@item C-x a l
76Similar, but define an abbrev specific to the current major mode
77(@code{add-mode-abbrev}).
78@item C-x a i g
79Define a word in the buffer as an abbrev (@code{inverse-add-global-abbrev}).
80@item C-x a i l
81Define a word in the buffer as a mode-specific abbrev
82(@code{inverse-add-mode-abbrev}).
387ac9c1
RS
83@item M-x define-global-abbrev @key{RET} @var{abbrev} @key{RET} @var{exp} @key{RET}
84Define @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}
86Define @var{abbrev} as a mode-specific abbrev expanding into @var{exp}.
6bf7aab6 87@item M-x kill-all-abbrevs
3f7ba267 88Discard all abbrev definitions, leaving a blank slate.
6bf7aab6
DL
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
94abbrev 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
96minibuffer, and then defines it as an abbrev for one or more words before
97point. Use a numeric argument to say how many words before point should be
98taken as the expansion. For example, to define the abbrev @samp{foo} as
99mentioned 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
103region 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
c40a7de7
CY
108defines a mode-specific abbrev for the current major mode. The
109arguments work the same as for @kbd{C-x a g}.
6bf7aab6
DL
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
c40a7de7
CY
115 @kbd{C-x a i g} (@code{inverse-add-global-abbrev}) and @kbd{C-x a i
116l} (@code{inverse-add-mode-abbrev}) perform the opposite task: if the
117abbrev text is already in the buffer, you use these commands to define
118an abbrev by specifying the expansion in the minibuffer. These
119commands will expand the abbrev text used for the definition.
6bf7aab6 120
387ac9c1
RS
121@findex define-mode-abbrev
122@findex define-global-abbrev
123 You can define an abbrev without inserting either the abbrev or its
124expansion in the buffer using the command @code{define-global-abbrev}.
110c9495 125It reads two arguments---the abbrev, and its expansion. The command
387ac9c1
RS
126@code{define-mode-abbrev} does likewise for a mode-specific abbrev.
127
c40a7de7
CY
128 To change the definition of an abbrev, just make a new definition.
129When an abbrev has a prior definition, the abbrev definition commands
58fa012d 130ask for confirmation before replacing it.
6bf7aab6 131
6bf7aab6 132@findex kill-all-abbrevs
3f7ba267
RS
133 To remove an abbrev definition, give a negative argument to the
134abbrev definition command: @kbd{C-u - C-x a g} or @kbd{C-u - C-x a l}.
135The former removes a global definition, while the latter removes a
136mode-specific definition. @kbd{M-x kill-all-abbrevs} removes all
137abbrev definitions, both global and local.
6bf7aab6
DL
138
139@node Expanding Abbrevs
140@section Controlling Abbrev Expansion
141
ccb467ec
RS
142 When Abbrev mode is enabled, an abbrev expands whenever it is
143present in the buffer just before point and you type a self-inserting
1df7defd 144whitespace or punctuation character (@key{SPC}, comma, etc.). More
ccb467ec
RS
145precisely, any character that is not a word constituent expands an
146abbrev, and any word-constituent character can be part of an abbrev.
147The most common way to use an abbrev is to insert it and then insert a
148punctuation or whitespace character to expand it.
6bf7aab6
DL
149
150@vindex abbrev-all-caps
c40a7de7
CY
151 Abbrev expansion preserves case: @samp{foo} expands to @samp{find
152outer otter}, and @samp{Foo} to @samp{Find outer otter}. @samp{FOO}
153expands to @samp{Find Outer Otter} by default, but if you change the
154variable @code{abbrev-all-caps} to a non-@code{nil} value, it expands
155to @samp{FIND OUTER OTTER}.
6bf7aab6
DL
156
157 These commands are used to control abbrev expansion:
158
159@table @kbd
160@item M-'
161Separate a prefix from a following abbrev to be expanded
162(@code{abbrev-prefix-mark}).
163@item C-x a e
164@findex expand-abbrev
165Expand the abbrev before point (@code{expand-abbrev}).
166This is effective even when Abbrev mode is not enabled.
167@item M-x expand-region-abbrevs
168Expand some or all abbrevs found in the region.
169@end table
170
171@kindex M-'
172@findex abbrev-prefix-mark
3f7ba267
RS
173 You may wish to expand an abbrev and attach a prefix to the expansion;
174for example, if @samp{cnst} expands into @samp{construction}, you might want
175to use it to enter @samp{reconstruction}. It does not work to type
6bf7aab6
DL
176@kbd{recnst}, because that is not necessarily a defined abbrev. What
177you can do is use the command @kbd{M-'} (@code{abbrev-prefix-mark}) in
178between 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
180indicate that it has done its work. Then insert the abbrev @samp{cnst};
181the buffer now contains @samp{re-cnst}. Now insert a non-word character
182to expand the abbrev @samp{cnst} into @samp{construction}. This
183expansion step also deletes the hyphen that indicated @kbd{M-'} had been
184used. The result is the desired @samp{reconstruction}.
185
186 If you actually want the text of the abbrev in the buffer, rather than
187its expansion, you can accomplish this by inserting the following
188punctuation with @kbd{C-q}. Thus, @kbd{foo C-q ,} leaves @samp{foo,} in
3f7ba267 189the buffer, not expanding it.
6bf7aab6
DL
190
191@findex unexpand-abbrev
c40a7de7
CY
192 If you expand an abbrev by mistake, you can undo the expansion by
193typing @kbd{C-/} (@code{undo}). @xref{Undo}. This undoes the
194insertion of the abbrev expansion and brings back the abbrev text. If
195the result you want is the terminating non-word character plus the
196unexpanded abbrev, you must reinsert the terminating character,
197quoting it with @kbd{C-q}. You can also use the command @kbd{M-x
198unexpand-abbrev} to cancel the last expansion without deleting the
199terminating character.
6bf7aab6
DL
200
201@findex expand-region-abbrevs
202 @kbd{M-x expand-region-abbrevs} searches through the region for defined
203abbrevs, and for each one found offers to replace it with its expansion.
204This command is useful if you have typed in text using abbrevs but forgot
205to turn on Abbrev mode first. It may also be useful together with a
206special set of abbrev definitions for making several global replacements at
207once. This command is effective even if Abbrev mode is not enabled.
208
6c5fa28d 209 The function @code{expand-abbrev} performs the expansion by calling
e38a5ebe
GM
210the function that @code{abbrev-expand-function} specifies. By
211changing this function you can make arbitrary changes to
718279c0
CY
212the abbrev expansion. @xref{Abbrev Expansion,,, elisp, The Emacs Lisp
213Reference Manual}.
6bf7aab6 214
6bf7aab6
DL
215@node Editing Abbrevs
216@section Examining and Editing Abbrevs
217
218@table @kbd
219@item M-x list-abbrevs
58fa012d 220Display a list of all abbrev definitions. With a numeric argument, list
bc3d8689 221only local abbrevs.
6bf7aab6
DL
222@item M-x edit-abbrevs
223Edit a list of abbrevs; you can add, alter or remove definitions.
224@end table
225
226@findex list-abbrevs
227 The output from @kbd{M-x list-abbrevs} looks like this:
228
229@example
3f7ba267 230@var{various other tables@dots{}}
6bf7aab6 231(lisp-mode-abbrev-table)
8838673e 232"dk" 0 "define-key"
6bf7aab6 233(global-abbrev-table)
8838673e 234"dfn" 0 "definition"
6bf7aab6
DL
235@end example
236
237@noindent
238(Some blank lines of no semantic significance, and some other abbrev
239tables, have been omitted.)
240
241 A line containing a name in parentheses is the header for abbrevs in a
242particular abbrev table; @code{global-abbrev-table} contains all the global
243abbrevs, and the other abbrev tables that are named after major modes
244contain the mode-specific abbrevs.
245
246 Within each abbrev table, each nonblank line defines one abbrev. The
247word at the beginning of the line is the abbrev. The number that
248follows is the number of times the abbrev has been expanded. Emacs
249keeps track of this to help you see which abbrevs you actually use, so
250that you can eliminate those that you don't use often. The string at
251the end of the line is the expansion.
252
dc12b2be 253 Some abbrevs are marked with @samp{(sys)}. These ``system'' abbrevs
c4ad63f6
GM
254(@pxref{Abbrevs,,, elisp, The Emacs Lisp Reference Manual}) are
255pre-defined by various modes, and are not saved to your abbrev file.
fcca8c59
GM
256To disable a ``system'' abbrev, define an abbrev of the same name that
257expands to itself, and save it to your abbrev file.
c4ad63f6 258
6bf7aab6
DL
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
262definitions by editing a list of them in an Emacs buffer. The list has
263the same format described above. The buffer of abbrevs is called
1c64e6ed 264@file{*Abbrevs*}, and is in Edit-Abbrevs mode. Type @kbd{C-c C-c} in
6bf7aab6
DL
265this buffer to install the abbrev definitions as specified in the
266buffer---and delete any abbrev definitions not listed.
267
268 The command @code{edit-abbrevs} is actually the same as
1c64e6ed 269@code{list-abbrevs} except that it selects the buffer @file{*Abbrevs*}
6bf7aab6
DL
270whereas @code{list-abbrevs} merely displays it in another window.
271
272@node Saving Abbrevs
273@section Saving Abbrevs
274
275 These commands allow you to keep abbrev definitions between editing
276sessions.
277
278@table @kbd
279@item M-x write-abbrev-file @key{RET} @var{file} @key{RET}
280Write a file @var{file} describing all defined abbrevs.
281@item M-x read-abbrev-file @key{RET} @var{file} @key{RET}
282Read the file @var{file} and define abbrevs as specified therein.
6bf7aab6
DL
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
444faccc 299@cindex abbrev file
b59c5368
RS
300 @kbd{M-x read-abbrev-file} reads a file name using the minibuffer
301and then reads the file, defining abbrevs according to the contents of
302the file. The function @code{quietly-read-abbrev-file} is similar
303except that it does not display a message in the echo area; you cannot
91ed7ea8
CY
304invoke 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
308default. This is your standard abbrev definition file, and Emacs
309loads abbrevs from it automatically when it starts up. (As an
310exception, Emacs does not load the abbrev file when it is started in
311batch mode. @xref{Initial Options}, for a description of batch mode.)
6bf7aab6
DL
312
313@vindex save-abbrevs
b59c5368
RS
314 Emacs will offer to save abbrevs automatically if you have changed
315any 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
318variable @code{save-abbrevs} to @code{nil}.
6bf7aab6
DL
319
320@findex insert-abbrevs
321@findex define-abbrevs
322 The commands @kbd{M-x insert-abbrevs} and @kbd{M-x define-abbrevs} are
323similar to the previous commands but work on text in an Emacs buffer.
6a080ff1 324@kbd{M-x insert-abbrevs} inserts text into the current buffer after point,
6bf7aab6 325describing all current abbrev definitions; @kbd{M-x define-abbrevs} parses
3f7ba267 326the entire current buffer and defines abbrevs accordingly.
6bf7aab6
DL
327
328@node Dynamic Abbrevs
329@section Dynamic Abbrev Expansion
330
3f7ba267
RS
331 The abbrev facility described above operates automatically as you
332insert text, but all abbrevs must be defined explicitly. By contrast,
333@dfn{dynamic abbrevs} allow the meanings of abbreviations to be
334determined automatically from the contents of the buffer, but dynamic
335abbrev expansion happens only when you request it explicitly.
6bf7aab6
DL
336
337@kindex M-/
338@kindex C-M-/
339@findex dabbrev-expand
340@findex dabbrev-completion
341@table @kbd
342@item M-/
343Expand the word in the buffer before point as a @dfn{dynamic abbrev},
344by searching in the buffer for words starting with that abbreviation
345(@code{dabbrev-expand}).
346
347@item C-M-/
348Complete the word before point as a dynamic abbrev
349(@code{dabbrev-completion}).
350@end table
351
352@vindex dabbrev-limit
353 For example, if the buffer contains @samp{does this follow } and you
354type @kbd{f o M-/}, the effect is to insert @samp{follow} because that
355is the last word in the buffer that starts with @samp{fo}. A numeric
356argument to @kbd{M-/} says to take the second, third, etc.@: distinct
357expansion found looking backward from point. Repeating @kbd{M-/}
358searches for an alternative expansion by looking farther back. After
359scanning all the text before point, it searches the text after point.
360The variable @code{dabbrev-limit}, if non-@code{nil}, specifies how far
3f7ba267 361away in the buffer to search for an expansion.
6bf7aab6
DL
362
363@vindex dabbrev-check-all-buffers
364 After scanning the current buffer, @kbd{M-/} normally searches other
365buffers, unless you have set @code{dabbrev-check-all-buffers} to
366@code{nil}.
367
78cab5d8 368@vindex dabbrev-ignored-buffer-regexps
36710aa6
GM
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
371expressions. If a buffer's name matches any of these regular
372expressions, dynamic abbrev expansion skips that buffer.
0a396e5a 373
6bf7aab6 374 A negative argument to @kbd{M-/}, as in @kbd{C-u - M-/}, says to
6a080ff1 375search first for expansions after point, then other buffers, and
3f7ba267
RS
376consider expansions before point only as a last resort. If you repeat
377the @kbd{M-/} to look for another expansion, do not specify an
378argument. Repeating @kbd{M-/} cycles through all the expansions after
379point and then the expansions before point.
6bf7aab6
DL
380
381 After you have expanded a dynamic abbrev, you can copy additional
382words that follow the expansion in its original context. Simply type
3f7ba267
RS
383@kbd{@key{SPC} M-/} for each additional word you want to copy. The
384spacing and punctuation between words is copied along with the words.
6bf7aab6
DL
385
386 The command @kbd{C-M-/} (@code{dabbrev-completion}) performs
3f7ba267
RS
387completion of a dynamic abbrev. Instead of trying the possible
388expansions one by one, it finds all of them, then inserts the text
389that they have in common. If they have nothing in common, @kbd{C-M-/}
6bf7aab6
DL
390displays a list of completions, from which you can select a choice in
391the usual manner. @xref{Completion}.
392
393 Dynamic abbrev expansion is completely independent of Abbrev mode; the
394expansion of a word with @kbd{M-/} is completely independent of whether
395it has a definition as an ordinary abbrev.
396
397@node Dabbrev Customization
398@section Customizing Dynamic Abbreviation
399
400 Normally, dynamic abbrev expansion ignores case when searching for
401expansions. That is, the expansion need not agree in case with the word
402you are expanding.
403
404@vindex dabbrev-case-fold-search
405 This feature is controlled by the variable
c40a7de7
CY
406@code{dabbrev-case-fold-search}. If it is @code{t}, case is ignored
407in this search; if it is @code{nil}, the word and the expansion must
408match in case. If the value is @code{case-fold-search} (the default),
409then the variable @code{case-fold-search} controls whether to ignore
410case while searching for expansions (@pxref{Search Case}).
6bf7aab6
DL
411
412@vindex dabbrev-case-replace
3f7ba267
RS
413 Normally, dynamic abbrev expansion preserves the case pattern
414@emph{of the dynamic abbrev you are expanding}, by converting the
415expansion to that case pattern.
6bf7aab6
DL
416
417@vindex dabbrev-case-fold-search
3f7ba267
RS
418 The variable @code{dabbrev-case-replace} controls whether to
419preserve the case pattern of the dynamic abbrev. If it is @code{t},
420the dynamic abbrev's case pattern is preserved in most cases; if it is
c40a7de7
CY
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}).
6bf7aab6
DL
425
426 However, if the expansion contains a complex mixed case pattern, and
3f7ba267
RS
427the dynamic abbrev matches this pattern as far as it goes, then the
428expansion is always copied verbatim, regardless of those variables.
429Thus, for example, if the buffer contains
430@code{variableWithSillyCasePattern}, and you type @kbd{v a M-/}, it
431copies the expansion verbatim including its case pattern.
6bf7aab6
DL
432
433@vindex dabbrev-abbrev-char-regexp
434 The variable @code{dabbrev-abbrev-char-regexp}, if non-@code{nil},
435controls which characters are considered part of a word, for dynamic expansion
436purposes. The regular expression must match just one character, never
437two or more. The same regular expression also determines which
438characters are part of an expansion. The value @code{nil} has a special
3f7ba267 439meaning: dynamic abbrevs are made of word characters, but expansions are
6bf7aab6
DL
440made of word and symbol characters.
441
442@vindex dabbrev-abbrev-skip-leading-regexp
443 In shell scripts and makefiles, a variable name is sometimes prefixed
444with @samp{$} and sometimes not. Major modes for this kind of text can
3f7ba267 445customize dynamic abbrev expansion to handle optional prefixes by setting
6bf7aab6
DL
446the variable @code{dabbrev-abbrev-skip-leading-regexp}. Its value
447should be a regular expression that matches the optional prefix that
3f7ba267 448dynamic abbrev expression should ignore.