* lisp/textmodes/rst.el (electric-pair-pairs): Declare.
[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
718279c0
CY
209 Expanding any abbrev runs @code{abbrev-expand-functions}, a special
210hook. Functions in this special hook can make arbitrary changes to
211the abbrev expansion. @xref{Abbrev Expansion,,, elisp, The Emacs Lisp
212Reference Manual}.
6bf7aab6 213
6bf7aab6
DL
214@node Editing Abbrevs
215@section Examining and Editing Abbrevs
216
217@table @kbd
218@item M-x list-abbrevs
58fa012d 219Display a list of all abbrev definitions. With a numeric argument, list
bc3d8689 220only local abbrevs.
6bf7aab6
DL
221@item M-x edit-abbrevs
222Edit 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
3f7ba267 229@var{various other tables@dots{}}
6bf7aab6 230(lisp-mode-abbrev-table)
8838673e 231"dk" 0 "define-key"
6bf7aab6 232(global-abbrev-table)
8838673e 233"dfn" 0 "definition"
6bf7aab6
DL
234@end example
235
236@noindent
237(Some blank lines of no semantic significance, and some other abbrev
238tables, have been omitted.)
239
240 A line containing a name in parentheses is the header for abbrevs in a
241particular abbrev table; @code{global-abbrev-table} contains all the global
242abbrevs, and the other abbrev tables that are named after major modes
243contain the mode-specific abbrevs.
244
245 Within each abbrev table, each nonblank line defines one abbrev. The
246word at the beginning of the line is the abbrev. The number that
247follows is the number of times the abbrev has been expanded. Emacs
248keeps track of this to help you see which abbrevs you actually use, so
249that you can eliminate those that you don't use often. The string at
250the end of the line is the expansion.
251
dc12b2be 252 Some abbrevs are marked with @samp{(sys)}. These ``system'' abbrevs
c4ad63f6
GM
253(@pxref{Abbrevs,,, elisp, The Emacs Lisp Reference Manual}) are
254pre-defined by various modes, and are not saved to your abbrev file.
fcca8c59
GM
255To disable a ``system'' abbrev, define an abbrev of the same name that
256expands to itself, and save it to your abbrev file.
c4ad63f6 257
6bf7aab6
DL
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
261definitions by editing a list of them in an Emacs buffer. The list has
262the same format described above. The buffer of abbrevs is called
1c64e6ed 263@file{*Abbrevs*}, and is in Edit-Abbrevs mode. Type @kbd{C-c C-c} in
6bf7aab6
DL
264this buffer to install the abbrev definitions as specified in the
265buffer---and delete any abbrev definitions not listed.
266
267 The command @code{edit-abbrevs} is actually the same as
1c64e6ed 268@code{list-abbrevs} except that it selects the buffer @file{*Abbrevs*}
6bf7aab6
DL
269whereas @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
275sessions.
276
277@table @kbd
278@item M-x write-abbrev-file @key{RET} @var{file} @key{RET}
279Write a file @var{file} describing all defined abbrevs.
280@item M-x read-abbrev-file @key{RET} @var{file} @key{RET}
281Read the file @var{file} and define abbrevs as specified therein.
6bf7aab6
DL
282@item M-x define-abbrevs
283Define abbrevs from definitions in current buffer.
284@item M-x insert-abbrevs
285Insert 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
290then writes a description of all current abbrev definitions into that
291file. This is used to save abbrev definitions for use in a later
292session. The text stored in the file is a series of Lisp expressions
293that, 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
444faccc 298@cindex abbrev file
b59c5368
RS
299 @kbd{M-x read-abbrev-file} reads a file name using the minibuffer
300and then reads the file, defining abbrevs according to the contents of
301the file. The function @code{quietly-read-abbrev-file} is similar
302except that it does not display a message in the echo area; you cannot
91ed7ea8
CY
303invoke 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
307default. This is your standard abbrev definition file, and Emacs
308loads abbrevs from it automatically when it starts up. (As an
309exception, Emacs does not load the abbrev file when it is started in
310batch mode. @xref{Initial Options}, for a description of batch mode.)
6bf7aab6
DL
311
312@vindex save-abbrevs
b59c5368
RS
313 Emacs will offer to save abbrevs automatically if you have changed
314any 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
317variable @code{save-abbrevs} to @code{nil}.
6bf7aab6
DL
318
319@findex insert-abbrevs
320@findex define-abbrevs
321 The commands @kbd{M-x insert-abbrevs} and @kbd{M-x define-abbrevs} are
322similar to the previous commands but work on text in an Emacs buffer.
6a080ff1 323@kbd{M-x insert-abbrevs} inserts text into the current buffer after point,
6bf7aab6 324describing all current abbrev definitions; @kbd{M-x define-abbrevs} parses
3f7ba267 325the entire current buffer and defines abbrevs accordingly.
6bf7aab6
DL
326
327@node Dynamic Abbrevs
328@section Dynamic Abbrev Expansion
329
3f7ba267
RS
330 The abbrev facility described above operates automatically as you
331insert text, but all abbrevs must be defined explicitly. By contrast,
332@dfn{dynamic abbrevs} allow the meanings of abbreviations to be
333determined automatically from the contents of the buffer, but dynamic
334abbrev expansion happens only when you request it explicitly.
6bf7aab6
DL
335
336@kindex M-/
337@kindex C-M-/
338@findex dabbrev-expand
339@findex dabbrev-completion
340@table @kbd
341@item M-/
342Expand the word in the buffer before point as a @dfn{dynamic abbrev},
343by searching in the buffer for words starting with that abbreviation
344(@code{dabbrev-expand}).
345
346@item C-M-/
347Complete 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
353type @kbd{f o M-/}, the effect is to insert @samp{follow} because that
354is the last word in the buffer that starts with @samp{fo}. A numeric
355argument to @kbd{M-/} says to take the second, third, etc.@: distinct
356expansion found looking backward from point. Repeating @kbd{M-/}
357searches for an alternative expansion by looking farther back. After
358scanning all the text before point, it searches the text after point.
359The variable @code{dabbrev-limit}, if non-@code{nil}, specifies how far
3f7ba267 360away in the buffer to search for an expansion.
6bf7aab6
DL
361
362@vindex dabbrev-check-all-buffers
363 After scanning the current buffer, @kbd{M-/} normally searches other
364buffers, unless you have set @code{dabbrev-check-all-buffers} to
365@code{nil}.
366
78cab5d8 367@vindex dabbrev-ignored-buffer-regexps
36710aa6
GM
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
370expressions. If a buffer's name matches any of these regular
371expressions, dynamic abbrev expansion skips that buffer.
0a396e5a 372
6bf7aab6 373 A negative argument to @kbd{M-/}, as in @kbd{C-u - M-/}, says to
6a080ff1 374search first for expansions after point, then other buffers, and
3f7ba267
RS
375consider expansions before point only as a last resort. If you repeat
376the @kbd{M-/} to look for another expansion, do not specify an
377argument. Repeating @kbd{M-/} cycles through all the expansions after
378point and then the expansions before point.
6bf7aab6
DL
379
380 After you have expanded a dynamic abbrev, you can copy additional
381words that follow the expansion in its original context. Simply type
3f7ba267
RS
382@kbd{@key{SPC} M-/} for each additional word you want to copy. The
383spacing and punctuation between words is copied along with the words.
6bf7aab6
DL
384
385 The command @kbd{C-M-/} (@code{dabbrev-completion}) performs
3f7ba267
RS
386completion of a dynamic abbrev. Instead of trying the possible
387expansions one by one, it finds all of them, then inserts the text
388that they have in common. If they have nothing in common, @kbd{C-M-/}
6bf7aab6
DL
389displays a list of completions, from which you can select a choice in
390the usual manner. @xref{Completion}.
391
392 Dynamic abbrev expansion is completely independent of Abbrev mode; the
393expansion of a word with @kbd{M-/} is completely independent of whether
394it 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
400expansions. That is, the expansion need not agree in case with the word
401you are expanding.
402
403@vindex dabbrev-case-fold-search
404 This feature is controlled by the variable
c40a7de7
CY
405@code{dabbrev-case-fold-search}. If it is @code{t}, case is ignored
406in this search; if it is @code{nil}, the word and the expansion must
407match in case. If the value is @code{case-fold-search} (the default),
408then the variable @code{case-fold-search} controls whether to ignore
409case while searching for expansions (@pxref{Search Case}).
6bf7aab6
DL
410
411@vindex dabbrev-case-replace
3f7ba267
RS
412 Normally, dynamic abbrev expansion preserves the case pattern
413@emph{of the dynamic abbrev you are expanding}, by converting the
414expansion to that case pattern.
6bf7aab6
DL
415
416@vindex dabbrev-case-fold-search
3f7ba267
RS
417 The variable @code{dabbrev-case-replace} controls whether to
418preserve the case pattern of the dynamic abbrev. If it is @code{t},
419the dynamic abbrev's case pattern is preserved in most cases; if it is
c40a7de7
CY
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}).
6bf7aab6
DL
424
425 However, if the expansion contains a complex mixed case pattern, and
3f7ba267
RS
426the dynamic abbrev matches this pattern as far as it goes, then the
427expansion is always copied verbatim, regardless of those variables.
428Thus, for example, if the buffer contains
429@code{variableWithSillyCasePattern}, and you type @kbd{v a M-/}, it
430copies the expansion verbatim including its case pattern.
6bf7aab6
DL
431
432@vindex dabbrev-abbrev-char-regexp
433 The variable @code{dabbrev-abbrev-char-regexp}, if non-@code{nil},
434controls which characters are considered part of a word, for dynamic expansion
435purposes. The regular expression must match just one character, never
436two or more. The same regular expression also determines which
437characters are part of an expansion. The value @code{nil} has a special
3f7ba267 438meaning: dynamic abbrevs are made of word characters, but expansions are
6bf7aab6
DL
439made 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
443with @samp{$} and sometimes not. Major modes for this kind of text can
3f7ba267 444customize dynamic abbrev expansion to handle optional prefixes by setting
6bf7aab6
DL
445the variable @code{dabbrev-abbrev-skip-leading-regexp}. Its value
446should be a regular expression that matches the optional prefix that
3f7ba267 447dynamic abbrev expression should ignore.