]> code.delx.au - gnu-emacs/blob - doc/emacs/custom.texi
43c61d769c740b9a849dfcb05be139222e1ee3af
[gnu-emacs] / doc / emacs / custom.texi
1 @c This is part of the Emacs manual.
2 @c Copyright (C) 1985-1987, 1993-1995, 1997, 2000-2015 Free Software
3 @c Foundation, Inc.
4 @c See file emacs.texi for copying conditions.
5 @node Customization
6 @chapter Customization
7 @cindex customization
8
9 This chapter describes some simple methods to customize the behavior
10 of Emacs.
11
12 Apart from the methods described here, see @ref{X Resources} for
13 information about using X resources to customize Emacs, and see
14 @ref{Keyboard Macros} for information about recording and replaying
15 keyboard macros. Making more far-reaching and open-ended changes
16 involves writing Emacs Lisp code; see
17 @iftex
18 @cite{The Emacs Lisp Reference Manual}.
19 @end iftex
20 @ifnottex
21 @ref{Top, Emacs Lisp, Emacs Lisp, elisp, The Emacs Lisp
22 Reference Manual}.
23 @end ifnottex
24
25 @menu
26 * Easy Customization:: Convenient way to browse and change settings.
27 * Variables:: Many Emacs commands examine Emacs variables
28 to decide what to do; by setting variables,
29 you can control their functioning.
30 * Key Bindings:: The keymaps say what command each key runs.
31 By changing them, you can ``redefine keys''.
32 * Init File:: How to write common customizations in the
33 initialization file.
34 @end menu
35
36 @node Easy Customization
37 @section Easy Customization Interface
38
39 @cindex settings
40 @cindex user option
41 @cindex customizable variable
42 Emacs has many @dfn{settings} which you can change. Most settings
43 are @dfn{customizable variables} (@pxref{Variables}), which are also
44 called @dfn{user options}. There is a huge number of customizable
45 variables, controlling numerous aspects of Emacs behavior; the
46 variables documented in this manual are listed in @ref{Variable
47 Index}. A separate class of settings are the @dfn{faces}, which
48 determine the fonts, colors, and other attributes of text
49 (@pxref{Faces}).
50
51 @findex customize
52 @cindex customization buffer
53 To browse and alter settings (both variables and faces), type
54 @kbd{M-x customize}. This creates a @dfn{customization buffer}, which
55 lets you navigate through a logically organized list of settings, edit
56 and set their values, and save them permanently.
57
58 @menu
59 * Customization Groups:: How settings are classified.
60 * Browsing Custom:: Browsing and searching for settings.
61 * Changing a Variable:: How to edit an option's value and set the option.
62 * Saving Customizations:: Saving customizations for future Emacs sessions.
63 * Face Customization:: How to edit the attributes of a face.
64 * Specific Customization:: Customizing specific settings or groups.
65 * Custom Themes:: Collections of customization settings.
66 * Creating Custom Themes:: How to create a new custom theme.
67 @end menu
68
69 @node Customization Groups
70 @subsection Customization Groups
71 @cindex customization groups
72
73 Customization settings are organized into @dfn{customization
74 groups}. These groups are collected into bigger groups, all the way
75 up to a master group called @code{Emacs}.
76
77 @kbd{M-x customize} creates a customization buffer that shows the
78 top-level @code{Emacs} group. It looks like this, in part:
79
80 @c we want the buffer example to all be on one page, but unfortunately
81 @c that's quite a bit of text, so force all space to the bottom.
82 @c @page
83 @smallexample
84 @group
85 For help, see [Easy Customization] in the [Emacs manual].
86
87 ________________________________________ [ Search ]
88
89 Operate on all settings in this buffer:
90 [ Revert... ] [ Apply ] [ Apply and Save ]
91
92
93 Emacs group: Customization of the One True Editor.
94 [State]: visible group members are all at standard values.
95 See also [Manual].
96
97 [Editing] : Basic text editing facilities.
98 [Convenience] : Convenience features for faster editing.
99
100 @var{more second-level groups}
101 @end group
102 @end smallexample
103
104 @noindent
105 The main part of this buffer shows the @samp{Emacs} customization
106 group, which contains several other groups (@samp{Editing},
107 @samp{Convenience}, etc.). The contents of those groups are not
108 listed here, only one line of documentation each.
109
110 The @dfn{state} of the group indicates whether setting in that group
111 has been edited, set or saved. @xref{Changing a Variable}.
112
113 @cindex editable fields (customization buffer)
114 @cindex buttons (customization buffer)
115 @cindex links (customization buffer)
116 Most of the customization buffer is read-only, but it includes some
117 @dfn{editable fields} that you can edit. For example, at the top of
118 the customization buffer is an editable field for searching for
119 settings (@pxref{Browsing Custom}). There are also @dfn{buttons} and
120 @dfn{links}, which you can activate by either clicking with the mouse,
121 or moving point there and typing @key{RET}. For example, the group
122 names like @samp{[Editing]} are links; activating one of these links
123 brings up the customization buffer for that group.
124
125 @kindex TAB @r{(customization buffer)}
126 @kindex S-TAB @r{(customization buffer)}
127 @findex widget-forward
128 @findex widget-backward
129 In the customizable buffer, you can type @key{TAB}
130 (@code{widget-forward}) to move forward to the next button or editable
131 field. @kbd{S-@key{TAB}} (@code{widget-backward}) moves back to the
132 previous button or editable field.
133
134 @node Browsing Custom
135 @subsection Browsing and Searching for Settings
136 @findex customize-browse
137
138 From the top-level customization buffer created by @kbd{M-x
139 customize}, you can follow the links to the subgroups of the
140 @samp{Emacs} customization group. These subgroups may contain
141 settings for you to customize; they may also contain further subgroups,
142 dealing with yet more specialized subsystems of Emacs. As you
143 navigate the hierarchy of customization groups, you should find some
144 settings that you want to customize.
145
146 If you are interested in customizing a particular setting or
147 customization group, you can go straight there with the commands
148 @kbd{M-x customize-option}, @kbd{M-x customize-face}, or @kbd{M-x
149 customize-group}. @xref{Specific Customization}.
150
151 @vindex custom-search-field
152 If you don't know exactly what groups or settings you want to
153 customize, you can search for them using the editable search field at
154 the top of each customization buffer. Here, you can type in a search
155 term---either one or more words separated by spaces, or a regular
156 expression (@pxref{Regexps}). Then type @key{RET} in the field, or
157 activate the @samp{Search} button next to it, to switch to a
158 customization buffer containing groups and settings that match those
159 terms. Note, however, that this feature only finds groups and
160 settings that are loaded in the current Emacs session.
161
162 If you don't want customization buffers to show the search field,
163 change the variable @code{custom-search-field} to @code{nil}.
164
165 The command @kbd{M-x customize-apropos} is similar to using the
166 search field, except that it reads the search term(s) using the
167 minibuffer. @xref{Specific Customization}.
168
169 @kbd{M-x customize-browse} is another way to browse the available
170 settings. This command creates a special customization buffer which
171 shows only the names of groups and settings, in a structured layout.
172 You can show the contents of a group, in the same buffer, by invoking
173 the @samp{[+]} button next to the group name. When the group contents
174 are shown, the button changes to @samp{[-]}; invoking that hides the
175 group contents again. Each group or setting in this buffer has a link
176 which says @samp{[Group]}, @samp{[Option]} or @samp{[Face]}. Invoking
177 this link creates an ordinary customization buffer showing just that
178 group, option, or face; this is the way to change settings that you
179 find with @kbd{M-x customize-browse}.
180
181 @node Changing a Variable
182 @subsection Changing a Variable
183
184 Here is an example of what a variable, or user option, looks like in
185 the customization buffer:
186
187 @smallexample
188 [Hide] Kill Ring Max: 60
189 [State]: STANDARD.
190 Maximum length of kill ring before oldest elements are thrown away.
191 @end smallexample
192
193 The first line shows that the variable is named
194 @code{kill-ring-max}, formatted as @samp{Kill Ring Max} for easier
195 viewing. Its value is @samp{60}. The button labeled @samp{[Hide]},
196 if activated, hides the variable's value and state; this is useful to
197 avoid cluttering up the customization buffer with very long values
198 (for this reason, variables that have very long values may start out
199 hidden). If you use the @samp{[Hide]} button, it changes to
200 @samp{[Show Value]}, which you can activate to reveal the value and
201 state. On a graphical display, the @samp{[Hide]} and @samp{[Show
202 Value]} buttons are replaced with graphical triangles pointing
203 downwards and rightwards respectively.
204
205 The line after the variable name indicates the @dfn{customization
206 state} of the variable: in this example, @samp{STANDARD} means you
207 have not changed the variable, so its value is the default one. The
208 @samp{[State]} button gives a menu of operations for customizing the
209 variable.
210
211 Below the customization state is the documentation for the variable.
212 This is the same documentation that would be shown by the @kbd{C-h v}
213 command (@pxref{Examining}). If the documentation is more than one
214 line long, only one line may be shown. If so, that line ends with a
215 @samp{[More]} button; activate this to see the full documentation.
216
217 @cindex user options, changing
218 @cindex customizing variables
219 @cindex variables, changing
220 To enter a new value for @samp{Kill Ring Max}, just move point to
221 the value and edit it. For example, type @kbd{M-d} to delete the
222 @samp{60} and type in another number. As you begin to alter the text,
223 the @samp{[State]} line will change:
224
225 @smallexample
226 [State]: EDITED, shown value does not take effect until you
227 set or save it.
228 @end smallexample
229
230 @noindent
231 Editing the value does not make it take effect right away. To do
232 that, you must @dfn{set} the variable by activating the @samp{[State]}
233 button and choosing @samp{Set for Current Session}. Then the
234 variable's state becomes:
235
236 @smallexample
237 [State]: SET for current session only.
238 @end smallexample
239
240 @noindent
241 You don't have to worry about specifying a value that is not valid;
242 the @samp{Set for Current Session} operation checks for validity and
243 will not install an unacceptable value.
244
245 @kindex M-TAB @r{(customization buffer)}
246 @kindex C-M-i @r{(customization buffer)}
247 @findex widget-complete
248 While editing certain kinds of values, such as file names, directory
249 names, and Emacs command names, you can perform completion with
250 @kbd{C-M-i} (@code{widget-complete}), or the equivalent keys
251 @kbd{M-@key{TAB}} or @kbd{@key{ESC} @key{TAB}}. This behaves much
252 like minibuffer completion (@pxref{Completion}).
253
254 Typing @key{RET} on an editable value field moves point forward to
255 the next field or button, like @key{TAB}. You can thus type @key{RET}
256 when you are finished editing a field, to move on to the next button
257 or field. To insert a newline within an editable field, use @kbd{C-o}
258 or @kbd{C-q C-j}.
259
260 For some variables, there is only a fixed set of legitimate values,
261 and you are not allowed to edit the value directly. Instead, a
262 @samp{[Value Menu]} button appears before the value; activating this
263 button presents a choice of values. For a boolean ``on or off''
264 value, the button says @samp{[Toggle]}, and flips the value. After
265 using the @samp{[Value Menu]} or @samp{[Toggle]} button, you must
266 again set the variable to make the chosen value take effect.
267
268 Some variables have values with complex structure. For example, the
269 value of @code{minibuffer-frame-alist} is an association list. Here
270 is how it appears in the customization buffer:
271
272 @smallexample
273 [Hide] Minibuffer Frame Alist:
274 [INS] [DEL] Parameter: width
275 Value: 80
276 [INS] [DEL] Parameter: height
277 Value: 2
278 [INS]
279 [ State ]: STANDARD.
280 Alist of parameters for the initial minibuffer frame. [Hide]
281 @r{[@dots{}more lines of documentation@dots{}]}
282 @end smallexample
283
284 @noindent
285 In this case, each association in the list consists of two items, one
286 labeled @samp{Parameter} and one labeled @samp{Value}; both are
287 editable fields. You can delete an association from the list with the
288 @samp{[DEL]} button next to it. To add an association, use the
289 @samp{[INS]} button at the position where you want to insert it; the
290 very last @samp{[INS]} button inserts at the end of the list.
291
292 @cindex saving a setting
293 @cindex settings, how to save
294 When you set a variable, the new value takes effect only in the
295 current Emacs session. To @dfn{save} the value for future sessions,
296 use the @samp{[State]} button and select the @samp{Save for Future
297 Sessions} operation. @xref{Saving Customizations}.
298
299 You can also restore the variable to its standard value by using the
300 @samp{[State]} button and selecting the @samp{Erase Customization}
301 operation. There are actually four reset operations:
302
303 @table @samp
304 @item Undo Edits
305 If you have modified but not yet set the variable, this restores the
306 text in the customization buffer to match the actual value.
307
308 @item Reset to Saved
309 This restores the value of the variable to the last saved value,
310 and updates the text accordingly.
311
312 @item Erase Customization
313 This sets the variable to its standard value. Any saved value that
314 you have is also eliminated.
315
316 @item Set to Backup Value
317 This sets the variable to a previous value that was set in the
318 customization buffer in this session. If you customize a variable
319 and then reset it, which discards the customized value,
320 you can get the discarded value back again with this operation.
321 @end table
322
323 @cindex comments on customized settings
324 Sometimes it is useful to record a comment about a specific
325 customization. Use the @samp{Add Comment} item from the
326 @samp{[State]} menu to create a field for entering the comment.
327
328 Near the top of the customization buffer are two lines of buttons:
329
330 @smallexample
331 [Set for Current Session] [Save for Future Sessions]
332 [Undo Edits] [Reset to Saved] [Erase Customization] [Exit]
333 @end smallexample
334
335 @noindent
336 Each of the first five buttons performs the stated operation---set,
337 save, reset, etc.---on all the settings in the buffer that could
338 meaningfully be affected. They do not operate on settings that are
339 hidden, nor on subgroups that are hidden or not visible in the buffer.
340
341 @kindex C-c C-c @r{(customization buffer)}
342 @kindex C-x C-c @r{(customization buffer)}
343 @findex Custom-set
344 @findex Custom-save
345 The command @kbd{C-c C-c} (@code{Custom-set}) is equivalent to using
346 the @samp{[Set for Current Session]} button. The command @kbd{C-x
347 C-s} (@code{Custom-save}) is like using the @samp{[Save for Future
348 Sessions]} button.
349
350 @vindex custom-buffer-done-kill
351 The @samp{[Exit]} button switches out of the customization buffer,
352 and buries the buffer at the bottom of the buffer list. To make it
353 kill the customization buffer instead, change the variable
354 @code{custom-buffer-done-kill} to @code{t}.
355
356 @node Saving Customizations
357 @subsection Saving Customizations
358
359 In the customization buffer, you can @dfn{save} a customization
360 setting by choosing the @samp{Save for Future Sessions} choice from
361 its @samp{[State]} button. The @kbd{C-x C-s} (@code{Custom-save})
362 command, or the @samp{[Save for Future Sessions]} button at the top of
363 the customization buffer, saves all applicable settings in the buffer.
364
365 Saving works by writing code to a file, usually your initialization
366 file (@pxref{Init File}). Future Emacs sessions automatically read
367 this file at startup, which sets up the customizations again.
368
369 @vindex custom-file
370 You can choose to save customizations somewhere other than your
371 initialization file. To make this work, you must add a couple of
372 lines of code to your initialization file, to set the variable
373 @code{custom-file} to the name of the desired file, and to load that
374 file. For example:
375
376 @example
377 (setq custom-file "~/.emacs-custom.el")
378 (load custom-file)
379 @end example
380
381 You can even specify different customization files for different
382 Emacs versions, like this:
383
384 @example
385 (cond ((< emacs-major-version 22)
386 ;; @r{Emacs 21 customization.}
387 (setq custom-file "~/.custom-21.el"))
388 ((and (= emacs-major-version 22)
389 (< emacs-minor-version 3))
390 ;; @r{Emacs 22 customization, before version 22.3.}
391 (setq custom-file "~/.custom-22.el"))
392 (t
393 ;; @r{Emacs version 22.3 or later.}
394 (setq custom-file "~/.emacs-custom.el")))
395
396 (load custom-file)
397 @end example
398
399 If Emacs was invoked with the @option{-q} or @option{--no-init-file}
400 options (@pxref{Initial Options}), it will not let you save your
401 customizations in your initialization file. This is because saving
402 customizations from such a session would wipe out all the other
403 customizations you might have on your initialization file.
404
405 Please note that any customizations you have not chosen to save for
406 future sessions will be lost when you terminate Emacs. If you'd like
407 to be prompted about unsaved customizations at termination time, add
408 the following to your initialization file:
409
410 @example
411 (add-hook 'kill-emacs-query-functions
412 'custom-prompt-customize-unsaved-options)
413 @end example
414
415 @node Face Customization
416 @subsection Customizing Faces
417 @cindex customizing faces
418 @cindex faces, customizing
419 @cindex fonts and faces
420
421 You can customize faces (@pxref{Faces}), which determine how Emacs
422 displays different types of text. Customization groups can contain
423 both variables and faces.
424
425 For example, in programming language modes, source code comments are
426 shown with @code{font-lock-comment-face} (@pxref{Font Lock}). In a
427 customization buffer, that face appears like this:
428
429 @smallexample
430 [Hide] Font Lock Comment Face:[sample]
431 [State] : STANDARD.
432 Font Lock mode face used to highlight comments.
433 [ ] Font Family: --
434 [ ] Font Foundry: --
435 [ ] Width: --
436 [ ] Height: --
437 [ ] Weight: --
438 [ ] Slant: --
439 [ ] Underline: --
440 [ ] Overline: --
441 [ ] Strike-through: --
442 [ ] Box around text: --
443 [ ] Inverse-video: --
444 [X] Foreground: Firebrick [Choose] (sample)
445 [ ] Background: --
446 [ ] Stipple: --
447 [ ] Inherit: --
448 [Hide Unused Attributes]
449 @end smallexample
450
451 @noindent
452 The first three lines show the name, @samp{[State]} button, and
453 documentation for the face. Below that is a list of @dfn{face
454 attributes}. In front of each attribute is a checkbox. A filled
455 checkbox, @samp{[X]}, means that the face specifies a value for this
456 attribute; an empty checkbox, @samp{[ ]}, means that the face does not
457 specify any special value for the attribute. You can activate a
458 checkbox to specify or unspecify its attribute.
459
460 A face does not have to specify every single attribute; in fact,
461 most faces only specify a few attributes. In the above example,
462 @code{font-lock-comment-face} only specifies the foreground color.
463 Any unspecified attribute is taken from the special face named
464 @code{default}, whose attributes are all specified. The
465 @code{default} face is the face used to display any text that does not
466 have an explicitly-assigned face; furthermore, its background color
467 attribute serves as the background color of the frame.
468
469 The @samp{Hide Unused Attributes} button, at the end of the
470 attribute list, hides the unspecified attributes of the face. When
471 attributes are being hidden, the button changes to @samp{[Show All
472 Attributes]}, which reveals the entire attribute list. The
473 customization buffer may start out with unspecified attributes hidden,
474 to avoid cluttering the interface.
475
476 When an attribute is specified, you can change its value in the
477 usual ways.
478
479 Foreground and background colors can be specified using either color
480 names or RGB triplets (@pxref{Colors}). You can also use the
481 @samp{[Choose]} button to switch to a list of color names; select a
482 color with @key{RET} in that buffer to put the color name in the value
483 field.
484
485 Setting, saving and resetting a face work like the same operations for
486 variables (@pxref{Changing a Variable}).
487
488 A face can specify different appearances for different types of
489 displays. For example, a face can make text red on a color display,
490 but use a bold font on a monochrome display. To specify multiple
491 appearances for a face, select @samp{For All Kinds of Displays} in the
492 menu you get from invoking @samp{[State]}.
493
494 @node Specific Customization
495 @subsection Customizing Specific Items
496
497 @table @kbd
498 @item M-x customize-option @key{RET} @var{option} @key{RET}
499 @itemx M-x customize-variable @key{RET} @var{option} @key{RET}
500 Set up a customization buffer for just one user option, @var{option}.
501 @item M-x customize-face @key{RET} @var{face} @key{RET}
502 Set up a customization buffer for just one face, @var{face}.
503 @item M-x customize-group @key{RET} @var{group} @key{RET}
504 Set up a customization buffer for just one group, @var{group}.
505 @item M-x customize-apropos @key{RET} @var{regexp} @key{RET}
506 Set up a customization buffer for all the settings and groups that
507 match @var{regexp}.
508 @item M-x customize-changed @key{RET} @var{version} @key{RET}
509 Set up a customization buffer with all the settings and groups
510 whose meaning has changed since Emacs version @var{version}.
511 @item M-x customize-saved
512 Set up a customization buffer containing all settings that you
513 have saved with customization buffers.
514 @item M-x customize-unsaved
515 Set up a customization buffer containing all settings that you have
516 set but not saved.
517 @end table
518
519 @findex customize-option
520 If you want to customize a particular user option, type @kbd{M-x
521 customize-option}. This reads the variable name, and sets up the
522 customization buffer with just that one user option. When entering
523 the variable name into the minibuffer, completion is available, but
524 only for the names of variables that have been loaded into Emacs.
525
526 @findex customize-face
527 @findex customize-group
528 Likewise, you can customize a specific face using @kbd{M-x
529 customize-face}. You can set up a customization buffer for a specific
530 customization group using @kbd{M-x customize-group}.
531
532 @findex customize-apropos
533 @kbd{M-x customize-apropos} prompts for a search term---either one
534 or more words separated by spaces, or a regular expression---and sets
535 up a customization buffer for all @emph{loaded} settings and groups
536 with matching names. This is like using the search field at the top
537 of the customization buffer (@pxref{Customization Groups}).
538
539 @findex customize-changed
540 When you upgrade to a new Emacs version, you might want to consider
541 customizing new settings, and settings whose meanings or default
542 values have changed. To do this, use @kbd{M-x customize-changed} and
543 specify a previous Emacs version number using the minibuffer. It
544 creates a customization buffer which shows all the settings and groups
545 whose definitions have been changed since the specified version,
546 loading them if necessary.
547
548 @findex customize-saved
549 @findex customize-unsaved
550 If you change settings and then decide the change was a mistake, you
551 can use two commands to revisit your changes. Use @kbd{M-x
552 customize-saved} to customize settings that you have saved. Use
553 @kbd{M-x customize-unsaved} to customize settings that you have set
554 but not saved.
555
556 @node Custom Themes
557 @subsection Custom Themes
558 @cindex custom themes
559
560 @dfn{Custom themes} are collections of settings that can be enabled
561 or disabled as a unit. You can use Custom themes to switch easily
562 between various collections of settings, and to transfer such
563 collections from one computer to another.
564
565 A Custom theme is stored as an Emacs Lisp source file. If the name of
566 the Custom theme is @var{name}, the theme file is named
567 @file{@var{name}-theme.el}. @xref{Creating Custom Themes}, for the
568 format of a theme file and how to make one.
569
570 @findex customize-themes
571 @vindex custom-theme-directory
572 @cindex color scheme
573 Type @kbd{M-x customize-themes} to switch to a buffer named
574 @file{*Custom Themes*}, which lists the Custom themes that Emacs knows
575 about. By default, Emacs looks for theme files in two locations: the
576 directory specified by the variable @code{custom-theme-directory}
577 (which defaults to @file{~/.emacs.d/}), and a directory named
578 @file{etc/themes} in your Emacs installation (see the variable
579 @code{data-directory}). The latter contains several Custom themes
580 which are distributed with Emacs, which customize Emacs's faces to fit
581 various color schemes. (Note, however, that Custom themes need not be
582 restricted to this purpose; they can be used to customize variables
583 too.)
584
585 @vindex custom-theme-load-path
586 If you want Emacs to look for Custom themes in some other directory,
587 add the directory name to the list variable
588 @code{custom-theme-load-path}. Its default value is
589 @code{(custom-theme-directory t)}; here, the symbol
590 @code{custom-theme-directory} has the special meaning of the value of
591 the variable @code{custom-theme-directory}, while @code{t} stands for
592 the built-in theme directory @file{etc/themes}. The themes listed in
593 the @file{*Custom Themes*} buffer are those found in the directories
594 specified by @code{custom-theme-load-path}.
595
596 @kindex C-x C-s @r{(Custom Themes buffer)}
597 In the @file{*Custom Themes*} buffer, you can activate the checkbox
598 next to a Custom theme to enable or disable the theme for the current
599 Emacs session. When a Custom theme is enabled, all of its settings
600 (variables and faces) take effect in the Emacs session. To apply the
601 choice of theme(s) to future Emacs sessions, type @kbd{C-x C-s}
602 (@code{custom-theme-save}) or use the @samp{[Save Theme Settings]}
603 button.
604
605 @vindex custom-safe-themes
606 When you first enable a Custom theme, Emacs displays the contents of
607 the theme file and asks if you really want to load it. Because
608 loading a Custom theme can execute arbitrary Lisp code, you should
609 only say yes if you know that the theme is safe; in that case, Emacs
610 offers to remember in the future that the theme is safe (this is done
611 by saving the theme file's SHA-256 hash to the variable
612 @code{custom-safe-themes}; if you want to treat all themes as safe,
613 change its value to @code{t}). Themes that come with Emacs (in the
614 @file{etc/themes} directory) are exempt from this check, and are
615 always considered safe.
616
617 @vindex custom-enabled-themes
618 Setting or saving Custom themes actually works by customizing the
619 variable @code{custom-enabled-themes}. The value of this variable is
620 a list of Custom theme names (as Lisp symbols, e.g., @code{tango}).
621 Instead of using the @file{*Custom Themes*} buffer to set
622 @code{custom-enabled-themes}, you can customize the variable using the
623 usual customization interface, e.g., with @kbd{M-x customize-option}.
624 Note that Custom themes are not allowed to set
625 @code{custom-enabled-themes} themselves.
626
627 Any customizations that you make through the customization buffer
628 take precedence over theme settings. This lets you easily override
629 individual theme settings that you disagree with. If settings from
630 two different themes overlap, the theme occurring earlier in
631 @code{custom-enabled-themes} takes precedence. In the customization
632 buffer, if a setting has been changed from its default by a Custom
633 theme, its @samp{State} display shows @samp{THEMED} instead of
634 @samp{STANDARD}.
635
636 @findex load-theme
637 @findex enable-theme
638 @findex disable-theme
639 You can enable a specific Custom theme in the current Emacs session
640 by typing @kbd{M-x load-theme}. This prompts for a theme name, loads
641 the theme from the theme file, and enables it. If a theme file
642 has been loaded before, you can enable the theme without loading its
643 file by typing @kbd{M-x enable-theme}. To disable a Custom theme,
644 type @kbd{M-x disable-theme}.
645
646 @findex describe-theme
647 To see a description of a Custom theme, type @kbd{?} on its line in
648 the @file{*Custom Themes*} buffer; or type @kbd{M-x describe-theme}
649 anywhere in Emacs and enter the theme name.
650
651 @node Creating Custom Themes
652 @subsection Creating Custom Themes
653 @cindex custom themes, creating
654
655 @findex customize-create-theme
656 You can define a Custom theme using an interface similar to the
657 customization buffer, by typing @kbd{M-x customize-create-theme}.
658 This switches to a buffer named @file{*Custom Theme*}. It also offers
659 to insert some common Emacs faces into the theme (a convenience, since
660 Custom themes are often used to customize faces). If you answer no,
661 the theme will initially contain no settings.
662
663 Near the top of the @file{*Custom Theme*} buffer are editable fields
664 where you can enter the theme's name and description. The name can be
665 anything except @samp{user}. The description is the one that will be
666 shown when you invoke @kbd{M-x describe-theme} for the theme. Its
667 first line should be a brief one-sentence summary; in the buffer made
668 by @kbd{M-x customize-themes}, this sentence is displayed next to the
669 theme name.
670
671 To add a new setting to the theme, use the @samp{[Insert Additional
672 Face]} or @samp{[Insert Additional Variable]} buttons. Each button
673 reads a face or variable name using the minibuffer, with completion,
674 and inserts a customization entry for the face or variable. You can
675 edit the variable values or face attributes in the same way as in a
676 normal customization buffer. To remove a face or variable from the
677 theme, uncheck the checkbox next to its name.
678
679 @vindex custom-theme-directory
680 After specifying the Custom theme's faces and variables, type
681 @kbd{C-x C-s} (@code{custom-theme-write}) or use the buffer's
682 @samp{[Save Theme]} button. This saves the theme file, named
683 @file{@var{name}-theme.el} where @var{name} is the theme name, in the
684 directory named by @code{custom-theme-directory}.
685
686 From the @file{*Custom Theme*} buffer, you can view and edit an
687 existing Custom theme by activating the @samp{[Visit Theme]} button
688 and specifying the theme name. You can also add the settings of
689 another theme into the buffer, using the @samp{[Merge Theme]} button.
690 You can import your non-theme settings into a Custom theme by using
691 the @samp{[Merge Theme]} button and specifying the special theme named
692 @samp{user}.
693
694 A theme file is simply an Emacs Lisp source file, and loading the
695 Custom theme works by loading the Lisp file. Therefore, you can edit
696 a theme file directly instead of using the @file{*Custom Theme*}
697 buffer. @xref{Custom Themes,,, elisp, The Emacs Lisp Reference
698 Manual}, for details.
699
700 @node Variables
701 @section Variables
702 @cindex variable
703
704 A @dfn{variable} is a Lisp symbol which has a value. The symbol's
705 name is also called the @dfn{variable name}. A variable name can
706 contain any characters that can appear in a file, but most variable
707 names consist of ordinary words separated by hyphens.
708
709 The name of the variable serves as a compact description of its
710 role. Most variables also have a @dfn{documentation string}, which
711 describes what the variable's purpose is, what kind of value it should
712 have, and how the value will be used. You can view this documentation
713 using the help command @kbd{C-h v} (@code{describe-variable}).
714 @xref{Examining}.
715
716 Emacs uses many Lisp variables for internal record keeping, but the
717 most interesting variables for a non-programmer user are those meant
718 for users to change---these are called @dfn{customizable variables} or
719 @dfn{user options} (@pxref{Easy Customization}). In the following
720 sections, we will describe other aspects of Emacs variables, such as
721 how to set them outside Customize.
722
723 Emacs Lisp allows any variable (with a few exceptions) to have any
724 kind of value. However, many variables are meaningful only if
725 assigned values of a certain type. For example, only numbers are
726 meaningful values for @code{kill-ring-max}, which specifies the
727 maximum length of the kill ring (@pxref{Earlier Kills}); if you give
728 @code{kill-ring-max} a string value, commands such as @kbd{C-y}
729 (@code{yank}) will signal an error. On the other hand, some variables
730 don't care about type; for instance, if a variable has one effect for
731 @code{nil} values and another effect for ``non-@code{nil}'' values,
732 then any value that is not the symbol @code{nil} induces the second
733 effect, regardless of its type (by convention, we usually use the
734 value @code{t}---a symbol which stands for ``true''---to specify a
735 non-@code{nil} value). If you set a variable using the customization
736 buffer, you need not worry about giving it an invalid type: the
737 customization buffer usually only allows you to enter meaningful
738 values. When in doubt, use @kbd{C-h v} (@code{describe-variable}) to
739 check the variable's documentation string to see kind of value it
740 expects (@pxref{Examining}).
741
742 @menu
743 * Examining:: Examining or setting one variable's value.
744 * Hooks:: Hook variables let you specify programs for parts
745 of Emacs to run on particular occasions.
746 * Locals:: Per-buffer values of variables.
747 * File Variables:: How files can specify variable values.
748 * Directory Variables:: How variable values can be specified by directory.
749 @end menu
750
751 @node Examining
752 @subsection Examining and Setting Variables
753 @cindex setting variables
754
755 @table @kbd
756 @item C-h v @var{var} @key{RET}
757 Display the value and documentation of variable @var{var}
758 (@code{describe-variable}).
759 @item M-x set-variable @key{RET} @var{var} @key{RET} @var{value} @key{RET}
760 Change the value of variable @var{var} to @var{value}.
761 @end table
762
763 To examine the value of a variable, use @kbd{C-h v}
764 (@code{describe-variable}). This reads a variable name using the
765 minibuffer, with completion, and displays both the value and the
766 documentation of the variable. For example,
767
768 @example
769 C-h v fill-column @key{RET}
770 @end example
771
772 @noindent
773 displays something like this:
774
775 @example
776 fill-column is a variable defined in `C source code'.
777 fill-column's value is 70
778
779 Automatically becomes buffer-local when set.
780 This variable is safe as a file local variable if its value
781 satisfies the predicate @code{integerp}.
782
783 Documentation:
784 Column beyond which automatic line-wrapping should happen.
785 Interactively, you can set the local value with C-x f.
786
787 You can customize this variable.
788 @end example
789
790 @noindent
791 The line that says ``You can customize the variable'' indicates that
792 this variable is a user option. @kbd{C-h v} is not restricted to user
793 options; it allows non-customizable variables too.
794
795 @findex set-variable
796 The most convenient way to set a specific customizable variable is
797 with @kbd{M-x set-variable}. This reads the variable name with the
798 minibuffer (with completion), and then reads a Lisp expression for the
799 new value using the minibuffer a second time (you can insert the old
800 value into the minibuffer for editing via @kbd{M-n}). For example,
801
802 @example
803 M-x set-variable @key{RET} fill-column @key{RET} 75 @key{RET}
804 @end example
805
806 @noindent
807 sets @code{fill-column} to 75.
808
809 @kbd{M-x set-variable} is limited to customizable variables, but you
810 can set any variable with a Lisp expression like this:
811
812 @example
813 (setq fill-column 75)
814 @end example
815
816 @noindent
817 To execute such an expression, type @kbd{M-:} (@code{eval-expression})
818 and enter the expression in the minibuffer (@pxref{Lisp Eval}).
819 Alternatively, go to the @file{*scratch*} buffer, type in the
820 expression, and then type @kbd{C-j} (@pxref{Lisp Interaction}).
821
822 Setting variables, like all means of customizing Emacs except where
823 otherwise stated, affects only the current Emacs session. The only
824 way to alter the variable in future sessions is to put something in
825 your initialization file (@pxref{Init File}).
826
827 @node Hooks
828 @subsection Hooks
829 @cindex hook
830 @cindex running a hook
831
832 @dfn{Hooks} are an important mechanism for customizing Emacs. A
833 hook is a Lisp variable which holds a list of functions, to be called
834 on some well-defined occasion. (This is called @dfn{running the
835 hook}.) The individual functions in the list are called the @dfn{hook
836 functions} of the hook. For example, the hook @code{kill-emacs-hook}
837 runs just before exiting Emacs (@pxref{Exiting}).
838
839 @cindex normal hook
840 Most hooks are @dfn{normal hooks}. This means that when Emacs runs
841 the hook, it calls each hook function in turn, with no arguments. We
842 have made an effort to keep most hooks normal, so that you can use
843 them in a uniform way. Every variable whose name ends in @samp{-hook}
844 is a normal hook.
845
846 @cindex abnormal hook
847 A few hooks are @dfn{abnormal hooks}. Their names end in
848 @samp{-functions}, instead of @samp{-hook} (some old code may also use
849 the deprecated suffix @samp{-hooks}). What
850 makes these hooks abnormal is the way its functions are
851 called---perhaps they are given arguments, or perhaps the values they
852 return are used in some way. For example,
853 @code{find-file-not-found-functions} is abnormal because as soon as
854 one hook function returns a non-@code{nil} value, the rest are not
855 called at all (@pxref{Visiting}). The documentation of each abnormal
856 hook variable explains how its functions are used.
857
858 @findex add-hook
859 You can set a hook variable with @code{setq} like any other Lisp
860 variable, but the recommended way to add a function to a hook (either
861 normal or abnormal) is to use @code{add-hook}, as shown by the
862 following examples. @xref{Hooks,,, elisp, The Emacs Lisp Reference
863 Manual}, for details.
864
865 Most major modes run one or more @dfn{mode hooks} as the last step
866 of initialization. Mode hooks are a convenient way to customize the
867 behavior of individual modes; they are always normal. For example,
868 here's how to set up a hook to turn on Auto Fill mode in Text mode and
869 other modes based on Text mode:
870
871 @example
872 (add-hook 'text-mode-hook 'auto-fill-mode)
873 @end example
874
875 @noindent
876 This works by calling @code{auto-fill-mode}, which enables the minor
877 mode when no argument is supplied (@pxref{Minor Modes}). Next,
878 suppose you don't want Auto Fill mode turned on in @LaTeX{} mode,
879 which is one of the modes based on Text mode. You can do this with
880 the following additional line:
881
882 @example
883 (add-hook 'latex-mode-hook (lambda () (auto-fill-mode -1)))
884 @end example
885
886 @noindent
887 Here we have used the special macro @code{lambda} to construct an
888 anonymous function (@pxref{Lambda Expressions,,, elisp, The Emacs Lisp
889 Reference Manual}), which calls @code{auto-fill-mode} with an argument
890 of @code{-1} to disable the minor mode. Because @LaTeX{} mode runs
891 @code{latex-mode-hook} after running @code{text-mode-hook}, the result
892 leaves Auto Fill mode disabled.
893
894 Here is a more complex example, showing how to use a hook to
895 customize the indentation of C code:
896
897 @example
898 @group
899 (setq my-c-style
900 '((c-comment-only-line-offset . 4)
901 @end group
902 @group
903 (c-cleanup-list . (scope-operator
904 empty-defun-braces
905 defun-close-semi))))
906 @end group
907
908 @group
909 (add-hook 'c-mode-common-hook
910 (lambda () (c-add-style "my-style" my-c-style t)))
911 @end group
912 @end example
913
914 @cindex Prog mode
915 @cindex program editing
916 Major mode hooks also apply to other major modes @dfn{derived} from
917 the original mode (@pxref{Derived Modes,,, elisp, The Emacs Lisp
918 Reference Manual}). For instance, HTML mode is derived from Text mode
919 (@pxref{HTML Mode}); when HTML mode is enabled, it runs
920 @code{text-mode-hook} before running @code{html-mode-hook}. This
921 provides a convenient way to use a single hook to affect several
922 related modes. In particular, if you want to apply a hook function to
923 any programming language mode, add it to @code{prog-mode-hook}; Prog
924 mode is a major mode that does little else than to let other major
925 modes inherit from it, exactly for this purpose.
926
927 It is best to design your hook functions so that the order in which
928 they are executed does not matter. Any dependence on the order is
929 asking for trouble. However, the order is predictable: the hook
930 functions are executed in the order they appear in the hook.
931
932 @findex remove-hook
933 If you play with adding various different versions of a hook
934 function by calling @code{add-hook} over and over, remember that all
935 the versions you added will remain in the hook variable together. You
936 can clear out individual functions by calling @code{remove-hook}, or
937 do @code{(setq @var{hook-variable} nil)} to remove everything.
938
939 @cindex buffer-local hooks
940 If the hook variable is buffer-local, the buffer-local variable will
941 be used instead of the global variable. However, if the buffer-local
942 variable contains the element @code{t}, the global hook variable will
943 be run as well.
944
945 @node Locals
946 @subsection Local Variables
947
948 @table @kbd
949 @item M-x make-local-variable @key{RET} @var{var} @key{RET}
950 Make variable @var{var} have a local value in the current buffer.
951 @item M-x kill-local-variable @key{RET} @var{var} @key{RET}
952 Make variable @var{var} use its global value in the current buffer.
953 @item M-x make-variable-buffer-local @key{RET} @var{var} @key{RET}
954 Mark variable @var{var} so that setting it will make it local to the
955 buffer that is current at that time.
956 @end table
957
958 @cindex local variables
959 Almost any variable can be made @dfn{local} to a specific Emacs
960 buffer. This means that its value in that buffer is independent of its
961 value in other buffers. A few variables are always local in every
962 buffer. Every other Emacs variable has a @dfn{global} value which is in
963 effect in all buffers that have not made the variable local.
964
965 @findex make-local-variable
966 @kbd{M-x make-local-variable} reads the name of a variable and makes
967 it local to the current buffer. Changing its value subsequently in
968 this buffer will not affect others, and changes in its global value
969 will not affect this buffer.
970
971 @findex make-variable-buffer-local
972 @cindex per-buffer variables
973 @kbd{M-x make-variable-buffer-local} marks a variable so it will
974 become local automatically whenever it is set. More precisely, once a
975 variable has been marked in this way, the usual ways of setting the
976 variable automatically do @code{make-local-variable} first. We call
977 such variables @dfn{per-buffer} variables. Many variables in Emacs
978 are normally per-buffer; the variable's document string tells you when
979 this is so. A per-buffer variable's global value is normally never
980 effective in any buffer, but it still has a meaning: it is the initial
981 value of the variable for each new buffer.
982
983 Major modes (@pxref{Major Modes}) always make variables local to the
984 buffer before setting the variables. This is why changing major modes
985 in one buffer has no effect on other buffers. Minor modes also work
986 by setting variables---normally, each minor mode has one controlling
987 variable which is non-@code{nil} when the mode is enabled
988 (@pxref{Minor Modes}). For many minor modes, the controlling variable
989 is per buffer, and thus always buffer-local. Otherwise, you can make
990 it local in a specific buffer like any other variable.
991
992 A few variables cannot be local to a buffer because they are always
993 local to each display instead (@pxref{Multiple Displays}). If you try to
994 make one of these variables buffer-local, you'll get an error message.
995
996 @findex kill-local-variable
997 @kbd{M-x kill-local-variable} makes a specified variable cease to be
998 local to the current buffer. The global value of the variable
999 henceforth is in effect in this buffer. Setting the major mode kills
1000 all the local variables of the buffer except for a few variables
1001 specially marked as @dfn{permanent locals}.
1002
1003 @findex setq-default
1004 To set the global value of a variable, regardless of whether the
1005 variable has a local value in the current buffer, you can use the Lisp
1006 construct @code{setq-default}. This construct is used just like
1007 @code{setq}, but it sets variables' global values instead of their local
1008 values (if any). When the current buffer does have a local value, the
1009 new global value may not be visible until you switch to another buffer.
1010 Here is an example:
1011
1012 @example
1013 (setq-default fill-column 75)
1014 @end example
1015
1016 @noindent
1017 @code{setq-default} is the only way to set the global value of a variable
1018 that has been marked with @code{make-variable-buffer-local}.
1019
1020 @findex default-value
1021 Lisp programs can use @code{default-value} to look at a variable's
1022 default value. This function takes a symbol as argument and returns its
1023 default value. The argument is evaluated; usually you must quote it
1024 explicitly. For example, here's how to obtain the default value of
1025 @code{fill-column}:
1026
1027 @example
1028 (default-value 'fill-column)
1029 @end example
1030
1031 @node File Variables
1032 @subsection Local Variables in Files
1033 @cindex local variables in files
1034 @cindex file local variables
1035
1036 A file can specify local variable values to use when editing the
1037 file with Emacs. Visiting the file checks for local variable
1038 specifications; it automatically makes these variables local to the
1039 buffer, and sets them to the values specified in the file.
1040
1041 @menu
1042 * Specifying File Variables:: Specifying file local variables.
1043 * Safe File Variables:: Making sure file local variables are safe.
1044 @end menu
1045
1046 @node Specifying File Variables
1047 @subsubsection Specifying File Variables
1048
1049 There are two ways to specify file local variable values: in the first
1050 line, or with a local variables list. Here's how to specify them in the
1051 first line:
1052
1053 @example
1054 -*- mode: @var{modename}; @var{var}: @var{value}; @dots{} -*-
1055 @end example
1056
1057 @noindent
1058 You can specify any number of variable/value pairs in this way, each
1059 pair with a colon and semicolon. The special variable/value pair
1060 @code{mode: @var{modename};}, if present, specifies a major mode. The
1061 @var{value}s are used literally, and not evaluated.
1062
1063 @findex add-file-local-variable-prop-line
1064 @findex delete-file-local-variable-prop-line
1065 @findex copy-dir-locals-to-file-locals-prop-line
1066 You can use @kbd{M-x add-file-local-variable-prop-line} instead of
1067 adding entries by hand. This command prompts for a variable and
1068 value, and adds them to the first line in the appropriate way.
1069 @kbd{M-x delete-file-local-variable-prop-line} prompts for a variable,
1070 and deletes its entry from the line. The command @kbd{M-x
1071 copy-dir-locals-to-file-locals-prop-line} copies the current
1072 directory-local variables to the first line (@pxref{Directory
1073 Variables}).
1074
1075 Here is an example first line that specifies Lisp mode and sets two
1076 variables with numeric values:
1077
1078 @smallexample
1079 ;; -*- mode: Lisp; fill-column: 75; comment-column: 50; -*-
1080 @end smallexample
1081
1082 @noindent
1083 Aside from @code{mode}, other keywords that have special meanings as
1084 file variables are @code{coding}, @code{unibyte}, and @code{eval}.
1085 These are described below.
1086
1087 @cindex shell scripts, and local file variables
1088 @cindex man pages, and local file variables
1089 In shell scripts, the first line is used to identify the script
1090 interpreter, so you cannot put any local variables there. To
1091 accommodate this, Emacs looks for local variable specifications in the
1092 @emph{second} line if the first line specifies an interpreter. The
1093 same is true for man pages which start with the magic string
1094 @samp{'\"} to specify a list of troff preprocessors (not all do,
1095 however).
1096
1097 Apart from using a @samp{-*-} line, you can define file local
1098 variables using a @dfn{local variables list} near the end of the file.
1099 The start of the local variables list should be no more than 3000
1100 characters from the end of the file, and must be on the last page if
1101 the file is divided into pages.
1102
1103 If a file has both a local variables list and a @samp{-*-} line,
1104 Emacs processes @emph{everything} in the @samp{-*-} line first, and
1105 @emph{everything} in the local variables list afterward. The exception
1106 to this is a major mode specification. Emacs applies this first,
1107 wherever it appears, since most major modes kill all local variables as
1108 part of their initialization.
1109
1110 A local variables list starts with a line containing the string
1111 @samp{Local Variables:}, and ends with a line containing the string
1112 @samp{End:}. In between come the variable names and values, one set
1113 per line, like this:
1114
1115 @example
1116 /* Local Variables: */
1117 /* mode: c */
1118 /* comment-column: 0 */
1119 /* End: */
1120 @end example
1121
1122 @noindent
1123 In this example, each line starts with the prefix @samp{/*} and ends
1124 with the suffix @samp{*/}. Emacs recognizes the prefix and suffix by
1125 finding them surrounding the magic string @samp{Local Variables:}, on
1126 the first line of the list; it then automatically discards them from
1127 the other lines of the list. The usual reason for using a prefix
1128 and/or suffix is to embed the local variables list in a comment, so it
1129 won't confuse other programs that the file is intended for. The
1130 example above is for the C programming language, where comments start
1131 with @samp{/*} and end with @samp{*/}.
1132
1133 @findex add-file-local-variable
1134 @findex delete-file-local-variable
1135 @findex copy-dir-locals-to-file-locals
1136 Instead of typing in the local variables list directly, you can use
1137 the command @kbd{M-x add-file-local-variable}. This prompts for a
1138 variable and value, and adds them to the list, adding the @samp{Local
1139 Variables:} string and start and end markers as necessary. The
1140 command @kbd{M-x delete-file-local-variable} deletes a variable from
1141 the list. @kbd{M-x copy-dir-locals-to-file-locals} copies
1142 directory-local variables to the list (@pxref{Directory Variables}).
1143
1144 As with the @samp{-*-} line, the variables in a local variables list
1145 are used literally, and are not evaluated first. If you want to split
1146 a long string value across multiple lines of the file, you can use
1147 backslash-newline, which is ignored in Lisp string constants; you
1148 should put the prefix and suffix on each line, even lines that start
1149 or end within the string, as they will be stripped off when processing
1150 the list. Here is an example:
1151
1152 @example
1153 # Local Variables:
1154 # compile-command: "cc foo.c -Dfoo=bar -Dhack=whatever \
1155 # -Dmumble=blaah"
1156 # End:
1157 @end example
1158
1159 Some ``variable names'' have special meanings in a local variables
1160 list:
1161
1162 @itemize
1163 @item
1164 @code{mode} enables the specified major mode.
1165
1166 @item
1167 @code{eval} evaluates the specified Lisp expression (the value
1168 returned by that expression is ignored).
1169
1170 @item
1171 @code{coding} specifies the coding system for character code
1172 conversion of this file. @xref{Coding Systems}.
1173
1174 @item
1175 @code{unibyte} says to load or compile a file of Emacs Lisp in unibyte
1176 mode, if the value is @code{t}. @xref{Disabling Multibyte, ,
1177 Disabling Multibyte Characters, elisp, GNU Emacs Lisp Reference
1178 Manual}.
1179
1180 @end itemize
1181
1182 @noindent
1183 These four keywords are not really variables; setting them in any
1184 other context has no special meaning.
1185
1186 Do not use the @code{mode} keyword for minor modes. To enable or
1187 disable a minor mode in a local variables list, use the @code{eval}
1188 keyword with a Lisp expression that runs the mode command
1189 (@pxref{Minor Modes}). For example, the following local variables
1190 list enables Eldoc mode (@pxref{Lisp Doc}) by calling
1191 @code{eldoc-mode} with no argument (calling it with an argument of 1
1192 would do the same), and disables Font Lock mode (@pxref{Font Lock}) by
1193 calling @code{font-lock-mode} with an argument of -1.
1194
1195 @example
1196 ;; Local Variables:
1197 ;; eval: (eldoc-mode)
1198 ;; eval: (font-lock-mode -1)
1199 ;; End:
1200 @end example
1201
1202 @noindent
1203 Note, however, that it is often a mistake to specify minor modes this
1204 way. Minor modes represent individual user preferences, and it may be
1205 inappropriate to impose your preferences on another user who might
1206 edit the file. If you wish to automatically enable or disable a minor
1207 mode in a situation-dependent way, it is often better to do it in a
1208 major mode hook (@pxref{Hooks}).
1209
1210 Use the command @kbd{M-x normal-mode} to reset the local variables
1211 and major mode of a buffer according to the file name and contents,
1212 including the local variables list if any. @xref{Choosing Modes}.
1213
1214 @node Safe File Variables
1215 @subsubsection Safety of File Variables
1216
1217 File-local variables can be dangerous; when you visit someone else's
1218 file, there's no telling what its local variables list could do to
1219 your Emacs. Improper values of the @code{eval} ``variable'', and
1220 other variables such as @code{load-path}, could execute Lisp code you
1221 didn't intend to run.
1222
1223 Therefore, whenever Emacs encounters file local variable values that
1224 are not known to be safe, it displays the file's entire local
1225 variables list, and asks you for confirmation before setting them.
1226 You can type @kbd{y} or @key{SPC} to put the local variables list into
1227 effect, or @kbd{n} to ignore it. When Emacs is run in batch mode
1228 (@pxref{Initial Options}), it can't really ask you, so it assumes the
1229 answer @kbd{n}.
1230
1231 Emacs normally recognizes certain variable/value pairs as safe.
1232 For instance, it is safe to give @code{comment-column} or
1233 @code{fill-column} any integer value. If a file specifies only
1234 known-safe variable/value pairs, Emacs does not ask for confirmation
1235 before setting them. Otherwise, you can tell Emacs to record all the
1236 variable/value pairs in this file as safe, by typing @kbd{!} at the
1237 confirmation prompt. When Emacs encounters these variable/value pairs
1238 subsequently, in the same file or others, it will assume they are
1239 safe.
1240
1241 @vindex safe-local-variable-values
1242 @cindex risky variable
1243 Some variables, such as @code{load-path}, are considered
1244 particularly @dfn{risky}: there is seldom any reason to specify them
1245 as local variables, and changing them can be dangerous. If a file
1246 contains only risky local variables, Emacs neither offers nor accepts
1247 @kbd{!} as input at the confirmation prompt. If some of the local
1248 variables in a file are risky, and some are only potentially unsafe, you
1249 can enter @kbd{!} at the prompt. It applies all the variables, but only
1250 marks the non-risky ones as safe for the future. If you really want to
1251 record safe values for risky variables, do it directly by customizing
1252 @samp{safe-local-variable-values} (@pxref{Easy Customization}).
1253
1254 @vindex enable-local-variables
1255 The variable @code{enable-local-variables} allows you to change the
1256 way Emacs processes local variables. Its default value is @code{t},
1257 which specifies the behavior described above. If it is @code{nil},
1258 Emacs simply ignores all file local variables. @code{:safe} means use
1259 only the safe values and ignore the rest. Any other value says to
1260 query you about each file that has local variables, without trying to
1261 determine whether the values are known to be safe.
1262
1263 @vindex enable-local-eval
1264 @vindex safe-local-eval-forms
1265 The variable @code{enable-local-eval} controls whether Emacs
1266 processes @code{eval} variables. The three possibilities for the
1267 variable's value are @code{t}, @code{nil}, and anything else, just as
1268 for @code{enable-local-variables}. The default is @code{maybe}, which
1269 is neither @code{t} nor @code{nil}, so normally Emacs does ask for
1270 confirmation about processing @code{eval} variables.
1271
1272 As an exception, Emacs never asks for confirmation to evaluate any
1273 @code{eval} form if that form occurs within the variable
1274 @code{safe-local-eval-forms}.
1275
1276 @node Directory Variables
1277 @subsection Per-Directory Local Variables
1278 @cindex local variables, for all files in a directory
1279 @cindex directory-local variables
1280 @cindex per-directory local variables
1281
1282 Sometimes, you may wish to define the same set of local variables to
1283 all the files in a certain directory and its subdirectories, such as
1284 the directory tree of a large software project. This can be
1285 accomplished with @dfn{directory-local variables}.
1286
1287 @cindex @file{.dir-locals.el} file
1288 The usual way to define directory-local variables is to put a file
1289 named @file{.dir-locals.el}@footnote{ On MS-DOS, the name of this file
1290 should be @file{_dir-locals.el}, due to limitations of the DOS
1291 filesystems. If the filesystem is limited to 8+3 file names, the name
1292 of the file will be truncated by the OS to @file{_dir-loc.el}. } in a
1293 directory. Whenever Emacs visits any file in that directory or any of
1294 its subdirectories, it will apply the directory-local variables
1295 specified in @file{.dir-locals.el}, as though they had been defined as
1296 file-local variables for that file (@pxref{File Variables}). Emacs
1297 searches for @file{.dir-locals.el} starting in the directory of the
1298 visited file, and moving up the directory tree. To avoid slowdown,
1299 this search is skipped for remote files. If needed, the search can be
1300 extended for remote files by setting the variable
1301 @code{enable-remote-dir-locals} to @code{t}.
1302
1303 The @file{.dir-locals.el} file should hold a specially-constructed
1304 list, which maps major mode names (symbols) to alists
1305 (@pxref{Association Lists,,, elisp, The Emacs Lisp Reference Manual}).
1306 Each alist entry consists of a variable name and the directory-local
1307 value to assign to that variable, when the specified major mode is
1308 enabled. Instead of a mode name, you can specify @samp{nil}, which
1309 means that the alist applies to any mode; or you can specify a
1310 subdirectory name (a string), in which case the alist applies to all
1311 files in that subdirectory.
1312
1313 Here's an example of a @file{.dir-locals.el} file:
1314
1315 @example
1316 ((nil . ((indent-tabs-mode . t)
1317 (fill-column . 80)))
1318 (c-mode . ((c-file-style . "BSD")
1319 (subdirs . nil)))
1320 ("src/imported"
1321 . ((nil . ((change-log-default-name
1322 . "ChangeLog.local"))))))
1323 @end example
1324
1325 @noindent
1326 This sets @samp{indent-tabs-mode} and @code{fill-column} for any file
1327 in the directory tree, and the indentation style for any C source
1328 file. The special @code{subdirs} element is not a variable, but a
1329 special keyword which indicates that the C mode settings are only to
1330 be applied in the current directory, not in any subdirectories.
1331 Finally, it specifies a different @file{ChangeLog} file name for any
1332 file in the @file{src/imported} subdirectory.
1333
1334 @findex add-dir-local-variable
1335 @findex delete-dir-local-variable
1336 @findex copy-file-locals-to-dir-locals
1337 Instead of editing the @file{.dir-locals.el} file by hand, you can
1338 use the command @kbd{M-x add-dir-local-variable}. This prompts for a
1339 mode or subdirectory name, and for variable and value, and adds the
1340 entry defining the directory-local variable. @kbd{M-x
1341 delete-dir-local-variable} deletes an entry. @kbd{M-x
1342 copy-file-locals-to-dir-locals} copies the file-local variables in the
1343 current file into @file{.dir-locals.el}.
1344
1345 @findex dir-locals-set-class-variables
1346 @findex dir-locals-set-directory-class
1347 Another method of specifying directory-local variables is to define
1348 a group of variables/value pairs in a @dfn{directory class}, using the
1349 @code{dir-locals-set-class-variables} function; then, tell Emacs which
1350 directories correspond to the class by using the
1351 @code{dir-locals-set-directory-class} function. These function calls
1352 normally go in your initialization file (@pxref{Init File}). This
1353 method is useful when you can't put @file{.dir-locals.el} in a
1354 directory for some reason. For example, you could apply settings to
1355 an unwritable directory this way:
1356
1357 @example
1358 (dir-locals-set-class-variables 'unwritable-directory
1359 '((nil . ((some-useful-setting . value)))))
1360
1361 (dir-locals-set-directory-class
1362 "/usr/include/" 'unwritable-directory)
1363 @end example
1364
1365 If a variable has both a directory-local and file-local value
1366 specified, the file-local value takes effect. Unsafe directory-local
1367 variables are handled in the same way as unsafe file-local variables
1368 (@pxref{Safe File Variables}).
1369
1370 Directory-local variables also take effect in certain buffers that
1371 do not visit a file directly but perform work within a directory, such
1372 as Dired buffers (@pxref{Dired}).
1373
1374 @node Key Bindings
1375 @section Customizing Key Bindings
1376 @cindex key bindings
1377
1378 This section describes @dfn{key bindings}, which map keys to
1379 commands, and @dfn{keymaps}, which record key bindings. It also
1380 explains how to customize key bindings, which is done by editing your
1381 init file (@pxref{Init Rebinding}).
1382
1383 @menu
1384 * Keymaps:: Generalities. The global keymap.
1385 * Prefix Keymaps:: Keymaps for prefix keys.
1386 * Local Keymaps:: Major and minor modes have their own keymaps.
1387 * Minibuffer Maps:: The minibuffer uses its own local keymaps.
1388 * Rebinding:: How to redefine one key's meaning conveniently.
1389 * Init Rebinding:: Rebinding keys with your initialization file.
1390 * Modifier Keys:: Using modifier keys in key bindings.
1391 * Function Keys:: Rebinding terminal function keys.
1392 * Named ASCII Chars:: Distinguishing @key{TAB} from @kbd{C-i}, and so on.
1393 * Mouse Buttons:: Rebinding mouse buttons in Emacs.
1394 * Disabling:: Disabling a command means confirmation is required
1395 before it can be executed. This is done to protect
1396 beginners from surprises.
1397 @end menu
1398
1399 @node Keymaps
1400 @subsection Keymaps
1401 @cindex keymap
1402
1403 As described in @ref{Commands}, each Emacs command is a Lisp
1404 function whose definition provides for interactive use. Like every
1405 Lisp function, a command has a function name, which usually consists
1406 of lower-case letters and hyphens.
1407
1408 A @dfn{key sequence} (@dfn{key}, for short) is a sequence of
1409 @dfn{input events} that have a meaning as a unit. Input events
1410 include characters, function keys and mouse buttons---all the inputs
1411 that you can send to the computer. A key sequence gets its meaning
1412 from its @dfn{binding}, which says what command it runs.
1413
1414 The bindings between key sequences and command functions are
1415 recorded in data structures called @dfn{keymaps}. Emacs has many of
1416 these, each used on particular occasions.
1417
1418 @cindex global keymap
1419 The @dfn{global} keymap is the most important keymap because it is
1420 always in effect. The global keymap defines keys for Fundamental mode
1421 (@pxref{Major Modes}); most of these definitions are common to most or
1422 all major modes. Each major or minor mode can have its own keymap
1423 which overrides the global definitions of some keys.
1424
1425 For example, a self-inserting character such as @kbd{g} is
1426 self-inserting because the global keymap binds it to the command
1427 @code{self-insert-command}. The standard Emacs editing characters
1428 such as @kbd{C-a} also get their standard meanings from the global
1429 keymap. Commands to rebind keys, such as @kbd{M-x global-set-key},
1430 work by storing the new binding in the proper place in the global map
1431 (@pxref{Rebinding}).
1432
1433 @cindex function key
1434 Most modern keyboards have function keys as well as character keys.
1435 Function keys send input events just as character keys do, and keymaps
1436 can have bindings for them. Key sequences can mix function keys and
1437 characters. For example, if your keyboard has a @key{Home} function
1438 key, Emacs can recognize key sequences like @kbd{C-x @key{Home}}. You
1439 can even mix mouse events with keyboard events, such as
1440 @kbd{S-down-mouse-1}.
1441
1442 On text terminals, typing a function key actually sends the computer
1443 a sequence of characters; the precise details of the sequence depends
1444 on the function key and on the terminal type. (Often the sequence
1445 starts with @kbd{@key{ESC} [}.) If Emacs understands your terminal
1446 type properly, it automatically handles such sequences as single input
1447 events.
1448
1449 @node Prefix Keymaps
1450 @subsection Prefix Keymaps
1451
1452 Internally, Emacs records only single events in each keymap.
1453 Interpreting a key sequence of multiple events involves a chain of
1454 keymaps: the first keymap gives a definition for the first event,
1455 which is another keymap, which is used to look up the second event in
1456 the sequence, and so on. Thus, a prefix key such as @kbd{C-x} or
1457 @key{ESC} has its own keymap, which holds the definition for the event
1458 that immediately follows that prefix.
1459
1460 The definition of a prefix key is usually the keymap to use for
1461 looking up the following event. The definition can also be a Lisp
1462 symbol whose function definition is the following keymap; the effect is
1463 the same, but it provides a command name for the prefix key that can be
1464 used as a description of what the prefix key is for. Thus, the binding
1465 of @kbd{C-x} is the symbol @code{Control-X-prefix}, whose function
1466 definition is the keymap for @kbd{C-x} commands. The definitions of
1467 @kbd{C-c}, @kbd{C-x}, @kbd{C-h} and @key{ESC} as prefix keys appear in
1468 the global map, so these prefix keys are always available.
1469
1470 Aside from ordinary prefix keys, there is a fictitious ``prefix key''
1471 which represents the menu bar; see @ref{Menu Bar,,,elisp, The Emacs Lisp
1472 Reference Manual}, for special information about menu bar key bindings.
1473 Mouse button events that invoke pop-up menus are also prefix keys; see
1474 @ref{Menu Keymaps,,,elisp, The Emacs Lisp Reference Manual}, for more
1475 details.
1476
1477 Some prefix keymaps are stored in variables with names:
1478
1479 @itemize @bullet
1480 @item
1481 @vindex ctl-x-map
1482 @code{ctl-x-map} is the variable name for the map used for characters that
1483 follow @kbd{C-x}.
1484 @item
1485 @vindex help-map
1486 @code{help-map} is for characters that follow @kbd{C-h}.
1487 @item
1488 @vindex esc-map
1489 @code{esc-map} is for characters that follow @key{ESC}. Thus, all Meta
1490 characters are actually defined by this map.
1491 @item
1492 @vindex ctl-x-4-map
1493 @code{ctl-x-4-map} is for characters that follow @kbd{C-x 4}.
1494 @item
1495 @vindex mode-specific-map
1496 @code{mode-specific-map} is for characters that follow @kbd{C-c}.
1497 @end itemize
1498
1499 @node Local Keymaps
1500 @subsection Local Keymaps
1501
1502 @cindex local keymap
1503 @cindex minor mode keymap
1504 So far, we have explained the ins and outs of the global map. Major
1505 modes customize Emacs by providing their own key bindings in
1506 @dfn{local keymaps}. For example, C mode overrides @key{TAB} to make
1507 it indent the current line for C code. Minor modes can also have
1508 local keymaps; whenever a minor mode is in effect, the definitions in
1509 its keymap override both the major mode's local keymap and the global
1510 keymap. In addition, portions of text in the buffer can specify their
1511 own keymaps, which override all other keymaps.
1512
1513 A local keymap can redefine a key as a prefix key by defining it as
1514 a prefix keymap. If the key is also defined globally as a prefix, its
1515 local and global definitions (both keymaps) effectively combine: both
1516 definitions are used to look up the event that follows the prefix key.
1517 For example, if a local keymap defines @kbd{C-c} as a prefix keymap,
1518 and that keymap defines @kbd{C-z} as a command, this provides a local
1519 meaning for @kbd{C-c C-z}. This does not affect other sequences that
1520 start with @kbd{C-c}; if those sequences don't have their own local
1521 bindings, their global bindings remain in effect.
1522
1523 Another way to think of this is that Emacs handles a multi-event key
1524 sequence by looking in several keymaps, one by one, for a binding of the
1525 whole key sequence. First it checks the minor mode keymaps for minor
1526 modes that are enabled, then it checks the major mode's keymap, and then
1527 it checks the global keymap. This is not precisely how key lookup
1528 works, but it's good enough for understanding the results in ordinary
1529 circumstances.
1530
1531 @node Minibuffer Maps
1532 @subsection Minibuffer Keymaps
1533
1534 @cindex minibuffer keymaps
1535 @vindex minibuffer-local-map
1536 @vindex minibuffer-local-ns-map
1537 @vindex minibuffer-local-completion-map
1538 @vindex minibuffer-local-must-match-map
1539 @vindex minibuffer-local-filename-completion-map
1540 @vindex minibuffer-local-filename-must-match-map
1541 The minibuffer has its own set of local keymaps; they contain various
1542 completion and exit commands.
1543
1544 @itemize @bullet
1545 @item
1546 @code{minibuffer-local-map} is used for ordinary input (no completion).
1547 @item
1548 @code{minibuffer-local-ns-map} is similar, except that @key{SPC} exits
1549 just like @key{RET}.
1550 @item
1551 @code{minibuffer-local-completion-map} is for permissive completion.
1552 @item
1553 @code{minibuffer-local-must-match-map} is for strict completion and
1554 for cautious completion.
1555 @item
1556 @code{minibuffer-local-filename-completion-map} and
1557 @code{minibuffer-local-filename-must-match-map} are like the two
1558 previous ones, but they are specifically for file name completion.
1559 They do not bind @key{SPC}.
1560 @end itemize
1561
1562 @node Rebinding
1563 @subsection Changing Key Bindings Interactively
1564 @cindex key rebinding, this session
1565 @cindex redefining keys, this session
1566 @cindex binding keys
1567
1568 The way to redefine an Emacs key is to change its entry in a keymap.
1569 You can change the global keymap, in which case the change is
1570 effective in all major modes (except those that have their own
1571 overriding local bindings for the same key). Or you can change a
1572 local keymap, which affects all buffers using the same major mode.
1573
1574 In this section, we describe how to rebind keys for the present
1575 Emacs session. @xref{Init Rebinding}, for a description of how to
1576 make key rebindings affect future Emacs sessions.
1577
1578 @findex global-set-key
1579 @findex local-set-key
1580 @findex global-unset-key
1581 @findex local-unset-key
1582 @table @kbd
1583 @item M-x global-set-key @key{RET} @var{key} @var{cmd} @key{RET}
1584 Define @var{key} globally to run @var{cmd}.
1585 @item M-x local-set-key @key{RET} @var{key} @var{cmd} @key{RET}
1586 Define @var{key} locally (in the major mode now in effect) to run
1587 @var{cmd}.
1588 @item M-x global-unset-key @key{RET} @var{key}
1589 Make @var{key} undefined in the global map.
1590 @item M-x local-unset-key @key{RET} @var{key}
1591 Make @var{key} undefined locally (in the major mode now in effect).
1592 @end table
1593
1594 For example, the following binds @kbd{C-z} to the @code{shell}
1595 command (@pxref{Interactive Shell}), replacing the normal global
1596 definition of @kbd{C-z}:
1597
1598 @example
1599 M-x global-set-key @key{RET} C-z shell @key{RET}
1600 @end example
1601
1602 @noindent
1603 The @code{global-set-key} command reads the command name after the
1604 key. After you press the key, a message like this appears so that you
1605 can confirm that you are binding the key you want:
1606
1607 @example
1608 Set key C-z to command:
1609 @end example
1610
1611 You can redefine function keys and mouse events in the same way; just
1612 type the function key or click the mouse when it's time to specify the
1613 key to rebind.
1614
1615 You can rebind a key that contains more than one event in the same
1616 way. Emacs keeps reading the key to rebind until it is a complete key
1617 (that is, not a prefix key). Thus, if you type @kbd{C-f} for
1618 @var{key}, that's the end; it enters the minibuffer immediately to
1619 read @var{cmd}. But if you type @kbd{C-x}, since that's a prefix, it
1620 reads another character; if that is @kbd{4}, another prefix character,
1621 it reads one more character, and so on. For example,
1622
1623 @example
1624 M-x global-set-key @key{RET} C-x 4 $ spell-other-window @key{RET}
1625 @end example
1626
1627 @noindent
1628 redefines @kbd{C-x 4 $} to run the (fictitious) command
1629 @code{spell-other-window}.
1630
1631 You can remove the global definition of a key with
1632 @code{global-unset-key}. This makes the key @dfn{undefined}; if you
1633 type it, Emacs will just beep. Similarly, @code{local-unset-key} makes
1634 a key undefined in the current major mode keymap, which makes the global
1635 definition (or lack of one) come back into effect in that major mode.
1636
1637 If you have redefined (or undefined) a key and you subsequently wish
1638 to retract the change, undefining the key will not do the job---you need
1639 to redefine the key with its standard definition. To find the name of
1640 the standard definition of a key, go to a Fundamental mode buffer in a
1641 fresh Emacs and use @kbd{C-h c}. The documentation of keys in this
1642 manual also lists their command names.
1643
1644 If you want to prevent yourself from invoking a command by mistake, it
1645 is better to disable the command than to undefine the key. A disabled
1646 command is less work to invoke when you really want to.
1647 @xref{Disabling}.
1648
1649 @node Init Rebinding
1650 @subsection Rebinding Keys in Your Init File
1651 @cindex rebinding major mode keys
1652 @c This node is referenced in the tutorial. When renaming or deleting
1653 @c it, the tutorial needs to be adjusted. (TUTORIAL.de)
1654
1655 If you have a set of key bindings that you like to use all the time,
1656 you can specify them in your initialization file by writing Lisp code.
1657 @xref{Init File}, for a description of the initialization file.
1658
1659 @findex kbd
1660 There are several ways to write a key binding using Lisp. The
1661 simplest is to use the @code{kbd} function, which converts a textual
1662 representation of a key sequence---similar to how we have written key
1663 sequences in this manual---into a form that can be passed as an
1664 argument to @code{global-set-key}. For example, here's how to bind
1665 @kbd{C-z} to the @code{shell} command (@pxref{Interactive Shell}):
1666
1667 @example
1668 (global-set-key (kbd "C-z") 'shell)
1669 @end example
1670
1671 @noindent
1672 The single-quote before the command name, @code{shell}, marks it as a
1673 constant symbol rather than a variable. If you omit the quote, Emacs
1674 would try to evaluate @code{shell} as a variable. This probably
1675 causes an error; it certainly isn't what you want.
1676
1677 Here are some additional examples, including binding function keys
1678 and mouse events:
1679
1680 @example
1681 (global-set-key (kbd "C-c y") 'clipboard-yank)
1682 (global-set-key (kbd "C-M-q") 'query-replace)
1683 (global-set-key (kbd "<f5>") 'flyspell-mode)
1684 (global-set-key (kbd "C-<f5>") 'linum-mode)
1685 (global-set-key (kbd "C-<right>") 'forward-sentence)
1686 (global-set-key (kbd "<mouse-2>") 'mouse-save-then-kill)
1687 @end example
1688
1689 Instead of using @code{kbd}, you can use a Lisp string or vector to
1690 specify the key sequence. Using a string is simpler, but only works
1691 for @acronym{ASCII} characters and Meta-modified @acronym{ASCII}
1692 characters. For example, here's how to bind @kbd{C-x M-l} to
1693 @code{make-symbolic-link} (@pxref{Misc File Ops}):
1694
1695 @example
1696 (global-set-key "\C-x\M-l" 'make-symbolic-link)
1697 @end example
1698
1699 To put @key{TAB}, @key{RET}, @key{ESC}, or @key{DEL} in the string,
1700 use the Emacs Lisp escape sequences @samp{\t}, @samp{\r}, @samp{\e},
1701 and @samp{\d} respectively. Here is an example which binds @kbd{C-x
1702 @key{TAB}} to @code{indent-rigidly} (@pxref{Indentation}):
1703
1704 @example
1705 (global-set-key "\C-x\t" 'indent-rigidly)
1706 @end example
1707
1708 When the key sequence includes function keys or mouse button events,
1709 or non-@acronym{ASCII} characters such as @code{C-=} or @code{H-a},
1710 you can use a vector to specify the key sequence. Each element in the
1711 vector stands for an input event; the elements are separated by spaces
1712 and surrounded by a pair of square brackets. If a vector element is a
1713 character, write it as a Lisp character constant: @samp{?} followed by
1714 the character as it would appear in a string. Function keys are
1715 represented by symbols (@pxref{Function Keys}); simply write the
1716 symbol's name, with no other delimiters or punctuation. Here are some
1717 examples:
1718
1719 @example
1720 (global-set-key [?\C-=] 'make-symbolic-link)
1721 (global-set-key [?\M-\C-=] 'make-symbolic-link)
1722 (global-set-key [?\H-a] 'make-symbolic-link)
1723 (global-set-key [f7] 'make-symbolic-link)
1724 (global-set-key [C-mouse-1] 'make-symbolic-link)
1725 @end example
1726
1727 @noindent
1728 You can use a vector for the simple cases too:
1729
1730 @example
1731 (global-set-key [?\C-z ?\M-l] 'make-symbolic-link)
1732 @end example
1733
1734 Language and coding systems may cause problems with key bindings for
1735 non-@acronym{ASCII} characters. @xref{Init Non-ASCII}.
1736
1737 As described in @ref{Local Keymaps}, major modes and minor modes can
1738 define local keymaps. These keymaps are constructed when the mode is
1739 used for the first time in a session. If you wish to change one of
1740 these keymaps, you must use the @dfn{mode hook} (@pxref{Hooks}).
1741
1742 @findex define-key
1743 For example, Texinfo mode runs the hook @code{texinfo-mode-hook}.
1744 Here's how you can use the hook to add local bindings for @kbd{C-c n}
1745 and @kbd{C-c p} in Texinfo mode:
1746
1747 @example
1748 (add-hook 'texinfo-mode-hook
1749 (lambda ()
1750 (define-key texinfo-mode-map "\C-cp"
1751 'backward-paragraph)
1752 (define-key texinfo-mode-map "\C-cn"
1753 'forward-paragraph)))
1754 @end example
1755
1756 @node Modifier Keys
1757 @subsection Modifier Keys
1758 @cindex modifier keys
1759
1760 The default key bindings in Emacs are set up so that modified
1761 alphabetical characters are case-insensitive. In other words,
1762 @kbd{C-A} does the same thing as @kbd{C-a}, and @kbd{M-A} does the
1763 same thing as @kbd{M-a}. This concerns only alphabetical characters,
1764 and does not apply to ``shifted'' versions of other keys; for
1765 instance, @kbd{C-@@} is not the same as @kbd{C-2}.
1766
1767 A @key{Control}-modified alphabetical character is always considered
1768 case-insensitive: Emacs always treats @kbd{C-A} as @kbd{C-a},
1769 @kbd{C-B} as @kbd{C-b}, and so forth. The reason for this is
1770 historical.
1771
1772 For all other modifiers, you can make the modified alphabetical
1773 characters case-sensitive when you customize Emacs. For instance, you
1774 could make @kbd{M-a} and @kbd{M-A} run different commands.
1775
1776 Although only the @key{Control} and @key{META} modifier keys are
1777 commonly used, Emacs supports three other modifier keys. These are
1778 called @key{Super}, @key{Hyper} and @key{Alt}. Few terminals provide
1779 ways to use these modifiers; the key labeled @key{Alt} on most
1780 keyboards usually issues the @key{META} modifier, not @key{Alt}. The
1781 standard key bindings in Emacs do not include any characters with
1782 these modifiers. However, you can customize Emacs to assign meanings
1783 to them. The modifier bits are labeled as @samp{s-}, @samp{H-} and
1784 @samp{A-} respectively.
1785
1786 Even if your keyboard lacks these additional modifier keys, you can
1787 enter it using @kbd{C-x @@}: @kbd{C-x @@ h} adds the ``hyper'' flag to
1788 the next character, @kbd{C-x @@ s} adds the ``super'' flag, and
1789 @kbd{C-x @@ a} adds the ``alt'' flag. For instance, @kbd{C-x @@ h
1790 C-a} is a way to enter @kbd{Hyper-Control-a}. (Unfortunately, there
1791 is no way to add two modifiers by using @kbd{C-x @@} twice for the
1792 same character, because the first one goes to work on the @kbd{C-x}.)
1793
1794 @node Function Keys
1795 @subsection Rebinding Function Keys
1796
1797 Key sequences can contain function keys as well as ordinary
1798 characters. Just as Lisp characters (actually integers) represent
1799 keyboard characters, Lisp symbols represent function keys. If the
1800 function key has a word as its label, then that word is also the name of
1801 the corresponding Lisp symbol. Here are the conventional Lisp names for
1802 common function keys:
1803
1804 @table @asis
1805 @item @code{LEFT}, @code{UP}, @code{RIGHT}, @code{DOWN}
1806 Cursor arrow keys.
1807
1808 @item @code{Begin}, @code{End}, @code{Home}, @code{next}, @code{prior}
1809 Other cursor repositioning keys.
1810
1811 @item @code{select}, @code{print}, @code{execute}, @code{backtab}
1812 @itemx @code{insert}, @code{undo}, @code{redo}, @code{clearline}
1813 @itemx @code{insertline}, @code{deleteline}, @code{insertchar}, @code{deletechar}
1814 Miscellaneous function keys.
1815
1816 @item @code{f1}, @code{f2}, @dots{} @code{f35}
1817 Numbered function keys (across the top of the keyboard).
1818
1819 @item @code{kp-add}, @code{kp-subtract}, @code{kp-multiply}, @code{kp-divide}
1820 @itemx @code{kp-backtab}, @code{kp-space}, @code{kp-tab}, @code{kp-enter}
1821 @itemx @code{kp-separator}, @code{kp-decimal}, @code{kp-equal}
1822 Keypad keys (to the right of the regular keyboard), with names or punctuation.
1823
1824 @item @code{kp-0}, @code{kp-1}, @dots{} @code{kp-9}
1825 Keypad keys with digits.
1826
1827 @item @code{kp-f1}, @code{kp-f2}, @code{kp-f3}, @code{kp-f4}
1828 Keypad PF keys.
1829 @end table
1830
1831 These names are conventional, but some systems (especially when using
1832 X) may use different names. To make certain what symbol is used for a
1833 given function key on your terminal, type @kbd{C-h c} followed by that
1834 key.
1835
1836 @xref{Init Rebinding}, for examples of binding function keys.
1837
1838 @cindex keypad
1839 Many keyboards have a ``numeric keypad'' on the right hand side.
1840 The numeric keys in the keypad double up as cursor motion keys,
1841 toggled by a key labeled @samp{Num Lock}. By default, Emacs
1842 translates these keys to the corresponding keys in the main keyboard.
1843 For example, when @samp{Num Lock} is on, the key labeled @samp{8} on
1844 the numeric keypad produces @code{kp-8}, which is translated to
1845 @kbd{8}; when @samp{Num Lock} is off, the same key produces
1846 @code{kp-up}, which is translated to @key{UP}. If you rebind a key
1847 such as @kbd{8} or @key{UP}, it affects the equivalent keypad key too.
1848 However, if you rebind a @samp{kp-} key directly, that won't affect
1849 its non-keypad equivalent. Note that the modified keys are not
1850 translated: for instance, if you hold down the @key{META} key while
1851 pressing the @samp{8} key on the numeric keypad, that generates
1852 @kbd{M-@key{kp-8}}.
1853
1854 Emacs provides a convenient method for binding the numeric keypad
1855 keys, using the variables @code{keypad-setup},
1856 @code{keypad-numlock-setup}, @code{keypad-shifted-setup}, and
1857 @code{keypad-numlock-shifted-setup}. These can be found in the
1858 @samp{keyboard} customization group (@pxref{Easy Customization}). You
1859 can rebind the keys to perform other tasks, such as issuing numeric
1860 prefix arguments.
1861
1862 @node Named ASCII Chars
1863 @subsection Named @acronym{ASCII} Control Characters
1864
1865 @key{TAB}, @key{RET}, @key{BS}, @key{LFD}, @key{ESC} and @key{DEL}
1866 started out as names for certain @acronym{ASCII} control characters,
1867 used so often that they have special keys of their own. For instance,
1868 @key{TAB} was another name for @kbd{C-i}. Later, users found it
1869 convenient to distinguish in Emacs between these keys and the ``same''
1870 control characters typed with the @key{Ctrl} key. Therefore, on most
1871 modern terminals, they are no longer the same: @key{TAB} is different
1872 from @kbd{C-i}.
1873
1874 Emacs can distinguish these two kinds of input if the keyboard does.
1875 It treats the ``special'' keys as function keys named @code{tab},
1876 @code{return}, @code{backspace}, @code{linefeed}, @code{escape}, and
1877 @code{delete}. These function keys translate automatically into the
1878 corresponding @acronym{ASCII} characters @emph{if} they have no
1879 bindings of their own. As a result, neither users nor Lisp programs
1880 need to pay attention to the distinction unless they care to.
1881
1882 If you do not want to distinguish between (for example) @key{TAB} and
1883 @kbd{C-i}, make just one binding, for the @acronym{ASCII} character @key{TAB}
1884 (octal code 011). If you do want to distinguish, make one binding for
1885 this @acronym{ASCII} character, and another for the ``function key'' @code{tab}.
1886
1887 With an ordinary @acronym{ASCII} terminal, there is no way to distinguish
1888 between @key{TAB} and @kbd{C-i} (and likewise for other such pairs),
1889 because the terminal sends the same character in both cases.
1890
1891 @node Mouse Buttons
1892 @subsection Rebinding Mouse Buttons
1893 @cindex mouse button events
1894 @cindex rebinding mouse buttons
1895 @cindex click events
1896 @cindex drag events
1897 @cindex down events
1898 @cindex button down events
1899
1900 Emacs uses Lisp symbols to designate mouse buttons, too. The ordinary
1901 mouse events in Emacs are @dfn{click} events; these happen when you
1902 press a button and release it without moving the mouse. You can also
1903 get @dfn{drag} events, when you move the mouse while holding the button
1904 down. Drag events happen when you finally let go of the button.
1905
1906 The symbols for basic click events are @code{mouse-1} for the leftmost
1907 button, @code{mouse-2} for the next, and so on. Here is how you can
1908 redefine the second mouse button to split the current window:
1909
1910 @example
1911 (global-set-key [mouse-2] 'split-window-below)
1912 @end example
1913
1914 The symbols for drag events are similar, but have the prefix
1915 @samp{drag-} before the word @samp{mouse}. For example, dragging the
1916 first button generates a @code{drag-mouse-1} event.
1917
1918 You can also define bindings for events that occur when a mouse button
1919 is pressed down. These events start with @samp{down-} instead of
1920 @samp{drag-}. Such events are generated only if they have key bindings.
1921 When you get a button-down event, a corresponding click or drag event
1922 will always follow.
1923
1924 @cindex double clicks
1925 @cindex triple clicks
1926 If you wish, you can distinguish single, double, and triple clicks. A
1927 double click means clicking a mouse button twice in approximately the
1928 same place. The first click generates an ordinary click event. The
1929 second click, if it comes soon enough, generates a double-click event
1930 instead. The event type for a double-click event starts with
1931 @samp{double-}: for example, @code{double-mouse-3}.
1932
1933 This means that you can give a special meaning to the second click at
1934 the same place, but it must act on the assumption that the ordinary
1935 single click definition has run when the first click was received.
1936
1937 This constrains what you can do with double clicks, but user interface
1938 designers say that this constraint ought to be followed in any case. A
1939 double click should do something similar to the single click, only
1940 ``more so''. The command for the double-click event should perform the
1941 extra work for the double click.
1942
1943 If a double-click event has no binding, it changes to the
1944 corresponding single-click event. Thus, if you don't define a
1945 particular double click specially, it executes the single-click command
1946 twice.
1947
1948 Emacs also supports triple-click events whose names start with
1949 @samp{triple-}. Emacs does not distinguish quadruple clicks as event
1950 types; clicks beyond the third generate additional triple-click events.
1951 However, the full number of clicks is recorded in the event list, so
1952 if you know Emacs Lisp you can distinguish if you really want to
1953 (@pxref{Click Events,,, elisp, The Emacs Lisp Reference Manual}).
1954 We don't recommend distinct meanings for more than three clicks, but
1955 sometimes it is useful for subsequent clicks to cycle through the same
1956 set of three meanings, so that four clicks are equivalent to one
1957 click, five are equivalent to two, and six are equivalent to three.
1958
1959 Emacs also records multiple presses in drag and button-down events.
1960 For example, when you press a button twice, then move the mouse while
1961 holding the button, Emacs gets a @samp{double-drag-} event. And at the
1962 moment when you press it down for the second time, Emacs gets a
1963 @samp{double-down-} event (which is ignored, like all button-down
1964 events, if it has no binding).
1965
1966 @vindex double-click-time
1967 The variable @code{double-click-time} specifies how much time can
1968 elapse between clicks and still allow them to be grouped as a multiple
1969 click. Its value is in units of milliseconds. If the value is
1970 @code{nil}, double clicks are not detected at all. If the value is
1971 @code{t}, then there is no time limit. The default is 500.
1972
1973 @vindex double-click-fuzz
1974 The variable @code{double-click-fuzz} specifies how much the mouse
1975 can move between clicks and still allow them to be grouped as a multiple
1976 click. Its value is in units of pixels on windowed displays and in
1977 units of 1/8 of a character cell on text-mode terminals; the default is
1978 3.
1979
1980 The symbols for mouse events also indicate the status of the modifier
1981 keys, with the usual prefixes @samp{C-}, @samp{M-}, @samp{H-},
1982 @samp{s-}, @samp{A-} and @samp{S-}. These always precede @samp{double-}
1983 or @samp{triple-}, which always precede @samp{drag-} or @samp{down-}.
1984
1985 A frame includes areas that don't show text from the buffer, such as
1986 the mode line and the scroll bar. You can tell whether a mouse button
1987 comes from a special area of the screen by means of dummy ``prefix
1988 keys''. For example, if you click the mouse in the mode line, you get
1989 the prefix key @code{mode-line} before the ordinary mouse-button symbol.
1990 Thus, here is how to define the command for clicking the first button in
1991 a mode line to run @code{scroll-up-command}:
1992
1993 @example
1994 (global-set-key [mode-line mouse-1] 'scroll-up-command)
1995 @end example
1996
1997 Here is the complete list of these dummy prefix keys and their
1998 meanings:
1999
2000 @table @code
2001 @item mode-line
2002 The mouse was in the mode line of a window.
2003 @item vertical-line
2004 The mouse was in the vertical line separating side-by-side windows. (If
2005 you use scroll bars, they appear in place of these vertical lines.)
2006 @item vertical-scroll-bar
2007 The mouse was in a vertical scroll bar. (This is the only kind of
2008 scroll bar Emacs currently supports.)
2009 @item menu-bar
2010 The mouse was in the menu bar.
2011 @item header-line
2012 The mouse was in a header line.
2013 @ignore
2014 @item horizontal-scroll-bar
2015 The mouse was in a horizontal scroll bar. Horizontal scroll bars do
2016 horizontal scrolling, and people don't use them often.
2017 @end ignore
2018 @end table
2019
2020 You can put more than one mouse button in a key sequence, but it isn't
2021 usual to do so.
2022
2023 @node Disabling
2024 @subsection Disabling Commands
2025 @cindex disabled command
2026
2027 Disabling a command means that invoking it interactively asks for
2028 confirmation from the user. The purpose of disabling a command is to
2029 prevent users from executing it by accident; we do this for commands
2030 that might be confusing to the uninitiated.
2031
2032 Attempting to invoke a disabled command interactively in Emacs
2033 displays a window containing the command's name, its documentation,
2034 and some instructions on what to do immediately; then Emacs asks for
2035 input saying whether to execute the command as requested, enable it
2036 and execute it, or cancel. If you decide to enable the command, you
2037 must then answer another question---whether to do this permanently, or
2038 just for the current session. (Enabling permanently works by
2039 automatically editing your initialization file.) You can also type
2040 @kbd{!} to enable @emph{all} commands, for the current session only.
2041
2042 The direct mechanism for disabling a command is to put a
2043 non-@code{nil} @code{disabled} property on the Lisp symbol for the
2044 command. Here is the Lisp program to do this:
2045
2046 @example
2047 (put 'delete-region 'disabled t)
2048 @end example
2049
2050 If the value of the @code{disabled} property is a string, that string
2051 is included in the message displayed when the command is used:
2052
2053 @example
2054 (put 'delete-region 'disabled
2055 "It's better to use `kill-region' instead.\n")
2056 @end example
2057
2058 @findex disable-command
2059 @findex enable-command
2060 You can make a command disabled either by editing the initialization
2061 file directly, or with the command @kbd{M-x disable-command}, which
2062 edits the initialization file for you. Likewise, @kbd{M-x
2063 enable-command} edits the initialization file to enable a command
2064 permanently. @xref{Init File}.
2065
2066 If Emacs was invoked with the @option{-q} or @option{--no-init-file}
2067 options (@pxref{Initial Options}), it will not edit your
2068 initialization file. Doing so could lose information because Emacs
2069 has not read your initialization file.
2070
2071 Whether a command is disabled is independent of what key is used to
2072 invoke it; disabling also applies if the command is invoked using
2073 @kbd{M-x}. However, disabling a command has no effect on calling it
2074 as a function from Lisp programs.
2075
2076 @node Init File
2077 @section The Emacs Initialization File
2078 @cindex init file
2079 @cindex .emacs file
2080 @cindex ~/.emacs file
2081 @cindex Emacs initialization file
2082 @cindex key rebinding, permanent
2083 @cindex rebinding keys, permanently
2084 @cindex startup (init file)
2085
2086 When Emacs is started, it normally tries to load a Lisp program from
2087 an @dfn{initialization file}, or @dfn{init file} for short. This
2088 file, if it exists, specifies how to initialize Emacs for you. Emacs
2089 looks for your init file using the filenames @file{~/.emacs},
2090 @file{~/.emacs.el}, or @file{~/.emacs.d/init.el}; you can choose to
2091 use any one of these three names (@pxref{Find Init}). Here, @file{~/}
2092 stands for your home directory.
2093
2094 You can use the command line switch @samp{-q} to prevent loading
2095 your init file, and @samp{-u} (or @samp{--user}) to specify a
2096 different user's init file (@pxref{Initial Options}).
2097
2098 @cindex @file{default.el}, the default init file
2099 There can also be a @dfn{default init file}, which is the library
2100 named @file{default.el}, found via the standard search path for
2101 libraries. The Emacs distribution contains no such library; your site
2102 may create one for local customizations. If this library exists, it is
2103 loaded whenever you start Emacs (except when you specify @samp{-q}).
2104 But your init file, if any, is loaded first; if it sets
2105 @code{inhibit-default-init} non-@code{nil}, then @file{default} is not
2106 loaded.
2107
2108 @cindex site init file
2109 @cindex @file{site-start.el}, the site startup file
2110 Your site may also have a @dfn{site startup file}; this is named
2111 @file{site-start.el}, if it exists. Like @file{default.el}, Emacs
2112 finds this file via the standard search path for Lisp libraries.
2113 Emacs loads this library before it loads your init file. To inhibit
2114 loading of this library, use the option @samp{--no-site-file}.
2115 @xref{Initial Options}. We recommend against using
2116 @file{site-start.el} for changes that some users may not like. It is
2117 better to put them in @file{default.el}, so that users can more easily
2118 override them.
2119
2120 @cindex site-lisp directories
2121 You can place @file{default.el} and @file{site-start.el} in any of
2122 the directories which Emacs searches for Lisp libraries. The variable
2123 @code{load-path} (@pxref{Lisp Libraries}) specifies these directories.
2124 Many sites put these files in a subdirectory named @file{site-lisp} in
2125 the Emacs installation directory, such as
2126 @file{/usr/local/share/emacs/site-lisp}.
2127
2128 Byte-compiling your init file is not recommended (@pxref{Byte
2129 Compilation,, Byte Compilation, elisp, the Emacs Lisp Reference
2130 Manual}). It generally does not speed up startup very much, and often
2131 leads to problems when you forget to recompile the file. A better
2132 solution is to use the Emacs server to reduce the number of times you
2133 have to start Emacs (@pxref{Emacs Server}). If your init file defines
2134 many functions, consider moving them to a separate (byte-compiled)
2135 file that you load in your init file.
2136
2137 If you are going to write actual Emacs Lisp programs that go beyond
2138 minor customization, you should read the @cite{Emacs Lisp Reference Manual}.
2139 @ifnottex
2140 @xref{Top, Emacs Lisp, Emacs Lisp, elisp, the Emacs Lisp Reference
2141 Manual}.
2142 @end ifnottex
2143
2144 @menu
2145 * Init Syntax:: Syntax of constants in Emacs Lisp.
2146 * Init Examples:: How to do some things with an init file.
2147 * Terminal Init:: Each terminal type can have an init file.
2148 * Find Init:: How Emacs finds the init file.
2149 * Init Non-ASCII:: Using non-@acronym{ASCII} characters in an init file.
2150 @end menu
2151
2152 @node Init Syntax
2153 @subsection Init File Syntax
2154
2155 The init file contains one or more Lisp expressions. Each of these
2156 consists of a function name followed by arguments, all surrounded by
2157 parentheses. For example, @code{(setq fill-column 60)} calls the
2158 function @code{setq} to set the variable @code{fill-column}
2159 (@pxref{Filling}) to 60.
2160
2161 You can set any Lisp variable with @code{setq}, but with certain
2162 variables @code{setq} won't do what you probably want in the
2163 @file{.emacs} file. Some variables automatically become buffer-local
2164 when set with @code{setq}; what you want in @file{.emacs} is to set
2165 the default value, using @code{setq-default}. Some customizable minor
2166 mode variables do special things to enable the mode when you set them
2167 with Customize, but ordinary @code{setq} won't do that; to enable the
2168 mode in your @file{.emacs} file, call the minor mode command. The
2169 following section has examples of both of these methods.
2170
2171 The second argument to @code{setq} is an expression for the new
2172 value of the variable. This can be a constant, a variable, or a
2173 function call expression. In @file{.emacs}, constants are used most
2174 of the time. They can be:
2175
2176 @table @asis
2177 @item Numbers:
2178 Numbers are written in decimal, with an optional initial minus sign.
2179
2180 @item Strings:
2181 @cindex Lisp string syntax
2182 @cindex string syntax
2183 Lisp string syntax is the same as C string syntax with a few extra
2184 features. Use a double-quote character to begin and end a string constant.
2185
2186 In a string, you can include newlines and special characters literally.
2187 But often it is cleaner to use backslash sequences for them: @samp{\n}
2188 for newline, @samp{\b} for backspace, @samp{\r} for carriage return,
2189 @samp{\t} for tab, @samp{\f} for formfeed (control-L), @samp{\e} for
2190 escape, @samp{\\} for a backslash, @samp{\"} for a double-quote, or
2191 @samp{\@var{ooo}} for the character whose octal code is @var{ooo}.
2192 Backslash and double-quote are the only characters for which backslash
2193 sequences are mandatory.
2194
2195 @samp{\C-} can be used as a prefix for a control character, as in
2196 @samp{\C-s} for @acronym{ASCII} control-S, and @samp{\M-} can be used as a prefix for
2197 a Meta character, as in @samp{\M-a} for @kbd{@key{META}-A} or
2198 @samp{\M-\C-a} for @kbd{@key{Ctrl}-@key{META}-A}.
2199
2200 @xref{Init Non-ASCII}, for information about including
2201 non-@acronym{ASCII} in your init file.
2202
2203 @item Characters:
2204 @cindex Lisp character syntax
2205 @cindex character syntax
2206 Lisp character constant syntax consists of a @samp{?} followed by
2207 either a character or an escape sequence starting with @samp{\}.
2208 Examples: @code{?x}, @code{?\n}, @code{?\"}, @code{?\)}. Note that
2209 strings and characters are not interchangeable in Lisp; some contexts
2210 require one and some contexts require the other.
2211
2212 @xref{Init Non-ASCII}, for information about binding commands to
2213 keys which send non-@acronym{ASCII} characters.
2214
2215 @item True:
2216 @code{t} stands for ``true''.
2217
2218 @item False:
2219 @code{nil} stands for ``false''.
2220
2221 @item Other Lisp objects:
2222 @cindex Lisp object syntax
2223 Write a single-quote (@code{'}) followed by the Lisp object you want.
2224 @end table
2225
2226 @node Init Examples
2227 @subsection Init File Examples
2228
2229 Here are some examples of doing certain commonly desired things with
2230 Lisp expressions:
2231
2232 @itemize @bullet
2233 @item
2234 Add a directory to the variable @code{load-path}. You can then put
2235 Lisp libraries that are not included with Emacs in this directory, and
2236 load them with @kbd{M-x load-library}. @xref{Lisp Libraries}.
2237
2238 @example
2239 (add-to-list 'load-path "/path/to/lisp/libraries")
2240 @end example
2241
2242 @item
2243 Make @key{TAB} in C mode just insert a tab if point is in the middle of a
2244 line.
2245
2246 @example
2247 (setq c-tab-always-indent nil)
2248 @end example
2249
2250 Here we have a variable whose value is normally @code{t} for ``true''
2251 and the alternative is @code{nil} for ``false''.
2252
2253 @item
2254 Make searches case sensitive by default (in all buffers that do not
2255 override this).
2256
2257 @example
2258 (setq-default case-fold-search nil)
2259 @end example
2260
2261 This sets the default value, which is effective in all buffers that do
2262 not have local values for the variable (@pxref{Locals}). Setting
2263 @code{case-fold-search} with @code{setq} affects only the current
2264 buffer's local value, which is probably not what you want to do in an
2265 init file.
2266
2267 @item
2268 @vindex user-mail-address
2269 Specify your own email address, if Emacs can't figure it out correctly.
2270
2271 @example
2272 (setq user-mail-address "cheney@@torture.gov")
2273 @end example
2274
2275 Various Emacs packages, such as Message mode, consult
2276 @code{user-mail-address} when they need to know your email address.
2277 @xref{Mail Headers}.
2278
2279 @item
2280 Make Text mode the default mode for new buffers.
2281
2282 @example
2283 (setq-default major-mode 'text-mode)
2284 @end example
2285
2286 Note that @code{text-mode} is used because it is the command for
2287 entering Text mode. The single-quote before it makes the symbol a
2288 constant; otherwise, @code{text-mode} would be treated as a variable
2289 name.
2290
2291 @need 1500
2292 @item
2293 Set up defaults for the Latin-1 character set
2294 which supports most of the languages of Western Europe.
2295
2296 @example
2297 (set-language-environment "Latin-1")
2298 @end example
2299
2300 @need 1500
2301 @item
2302 Turn off Line Number mode, a global minor mode.
2303
2304 @example
2305 (line-number-mode 0)
2306 @end example
2307
2308 @need 1500
2309 @item
2310 Turn on Auto Fill mode automatically in Text mode and related modes
2311 (@pxref{Hooks}).
2312
2313 @example
2314 (add-hook 'text-mode-hook 'auto-fill-mode)
2315 @end example
2316
2317 @item
2318 Load the installed Lisp library named @file{foo} (actually a file
2319 @file{foo.elc} or @file{foo.el} in a standard Emacs directory).
2320
2321 @example
2322 (load "foo")
2323 @end example
2324
2325 When the argument to @code{load} is a relative file name, not starting
2326 with @samp{/} or @samp{~}, @code{load} searches the directories in
2327 @code{load-path} (@pxref{Lisp Libraries}).
2328
2329 @item
2330 Load the compiled Lisp file @file{foo.elc} from your home directory.
2331
2332 @example
2333 (load "~/foo.elc")
2334 @end example
2335
2336 Here a full file name is used, so no searching is done.
2337
2338 @item
2339 @cindex loading Lisp libraries automatically
2340 @cindex autoload Lisp libraries
2341 Tell Emacs to find the definition for the function @code{myfunction}
2342 by loading a Lisp library named @file{mypackage} (i.e., a file
2343 @file{mypackage.elc} or @file{mypackage.el}):
2344
2345 @example
2346 (autoload 'myfunction "mypackage" "Do what I say." t)
2347 @end example
2348
2349 @noindent
2350 Here the string @code{"Do what I say."} is the function's
2351 documentation string. You specify it in the @code{autoload}
2352 definition so it will be available for help commands even when the
2353 package is not loaded. The last argument, @code{t}, indicates that
2354 this function is interactive; that is, it can be invoked interactively
2355 by typing @kbd{M-x myfunction @key{RET}} or by binding it to a key.
2356 If the function is not interactive, omit the @code{t} or use
2357 @code{nil}.
2358
2359 @item
2360 Rebind the key @kbd{C-x l} to run the function @code{make-symbolic-link}
2361 (@pxref{Init Rebinding}).
2362
2363 @example
2364 (global-set-key "\C-xl" 'make-symbolic-link)
2365 @end example
2366
2367 or
2368
2369 @example
2370 (define-key global-map "\C-xl" 'make-symbolic-link)
2371 @end example
2372
2373 Note once again the single-quote used to refer to the symbol
2374 @code{make-symbolic-link} instead of its value as a variable.
2375
2376 @item
2377 Do the same thing for Lisp mode only.
2378
2379 @example
2380 (define-key lisp-mode-map "\C-xl" 'make-symbolic-link)
2381 @end example
2382
2383 @item
2384 Redefine all keys which now run @code{next-line} in Fundamental mode
2385 so that they run @code{forward-line} instead.
2386
2387 @findex substitute-key-definition
2388 @example
2389 (substitute-key-definition 'next-line 'forward-line
2390 global-map)
2391 @end example
2392
2393 @item
2394 Make @kbd{C-x C-v} undefined.
2395
2396 @example
2397 (global-unset-key "\C-x\C-v")
2398 @end example
2399
2400 One reason to undefine a key is so that you can make it a prefix.
2401 Simply defining @kbd{C-x C-v @var{anything}} will make @kbd{C-x C-v} a
2402 prefix, but @kbd{C-x C-v} must first be freed of its usual non-prefix
2403 definition.
2404
2405 @item
2406 Make @samp{$} have the syntax of punctuation in Text mode.
2407 Note the use of a character constant for @samp{$}.
2408
2409 @example
2410 (modify-syntax-entry ?\$ "." text-mode-syntax-table)
2411 @end example
2412
2413 @item
2414 Enable the use of the command @code{narrow-to-region} without confirmation.
2415
2416 @example
2417 (put 'narrow-to-region 'disabled nil)
2418 @end example
2419
2420 @item
2421 Adjusting the configuration to various platforms and Emacs versions.
2422
2423 Users typically want Emacs to behave the same on all systems, so the
2424 same init file is right for all platforms. However, sometimes it
2425 happens that a function you use for customizing Emacs is not available
2426 on some platforms or in older Emacs versions. To deal with that
2427 situation, put the customization inside a conditional that tests whether
2428 the function or facility is available, like this:
2429
2430 @example
2431 (if (fboundp 'blink-cursor-mode)
2432 (blink-cursor-mode 0))
2433
2434 (if (boundp 'coding-category-utf-8)
2435 (set-coding-priority '(coding-category-utf-8)))
2436 @end example
2437
2438 @noindent
2439 You can also simply disregard the errors that occur if the
2440 function is not defined.
2441
2442 @example
2443 (ignore-errors (set-face-background 'region "grey75"))
2444 @end example
2445
2446 A @code{setq} on a variable which does not exist is generally
2447 harmless, so those do not need a conditional.
2448 @end itemize
2449
2450 @node Terminal Init
2451 @subsection Terminal-specific Initialization
2452
2453 @vindex term-file-aliases
2454 Each terminal type can have a Lisp library to be loaded into Emacs when
2455 it is run on that type of terminal. For a terminal type named
2456 @var{termtype}, the library is called @file{term/@var{termtype}}.
2457 (If there is an entry of the form @code{(@var{termtype} . @var{alias})}
2458 in the @code{term-file-aliases} association list, Emacs uses
2459 @var{alias} in place of @var{termtype}.) The library is
2460 found by searching the directories @code{load-path} as usual and trying the
2461 suffixes @samp{.elc} and @samp{.el}. Normally it appears in the
2462 subdirectory @file{term} of the directory where most Emacs libraries are
2463 kept.
2464
2465 The usual purpose of the terminal-specific library is to map the
2466 escape sequences used by the terminal's function keys onto more
2467 meaningful names, using @code{input-decode-map} (or
2468 @code{function-key-map} before it). See the file
2469 @file{term/lk201.el} for an example of how this is done. Many function
2470 keys are mapped automatically according to the information in the
2471 Termcap data base; the terminal-specific library needs to map only the
2472 function keys that Termcap does not specify.
2473
2474 When the terminal type contains a hyphen, only the part of the name
2475 before the first hyphen is significant in choosing the library name.
2476 Thus, terminal types @samp{aaa-48} and @samp{aaa-30-rv} both use
2477 the library @file{term/aaa}. The code in the library can use
2478 @code{(getenv "TERM")} to find the full terminal type name.
2479
2480 @vindex term-file-prefix
2481 The library's name is constructed by concatenating the value of the
2482 variable @code{term-file-prefix} and the terminal type. Your @file{.emacs}
2483 file can prevent the loading of the terminal-specific library by setting
2484 @code{term-file-prefix} to @code{nil}.
2485
2486 @vindex tty-setup-hook
2487 Emacs runs the hook @code{tty-setup-hook} at the end of
2488 initialization, after both your @file{.emacs} file and any
2489 terminal-specific library have been read in. Add hook functions to this
2490 hook if you wish to override part of any of the terminal-specific
2491 libraries and to define initializations for terminals that do not have a
2492 library. @xref{Hooks}.
2493
2494 @node Find Init
2495 @subsection How Emacs Finds Your Init File
2496
2497 Normally Emacs uses the environment variable @env{HOME}
2498 (@pxref{General Variables, HOME}) to find @file{.emacs}; that's what
2499 @samp{~} means in a file name. If @file{.emacs} is not found inside
2500 @file{~/} (nor @file{.emacs.el}), Emacs looks for
2501 @file{~/.emacs.d/init.el} (which, like @file{~/.emacs.el}, can be
2502 byte-compiled).
2503
2504 However, if you run Emacs from a shell started by @code{su}, Emacs
2505 tries to find your own @file{.emacs}, not that of the user you are
2506 currently pretending to be. The idea is that you should get your own
2507 editor customizations even if you are running as the super user.
2508
2509 More precisely, Emacs first determines which user's init file to use.
2510 It gets your user name from the environment variables @env{LOGNAME} and
2511 @env{USER}; if neither of those exists, it uses effective user-ID@.
2512 If that user name matches the real user-ID, then Emacs uses @env{HOME};
2513 otherwise, it looks up the home directory corresponding to that user
2514 name in the system's data base of users.
2515 @c LocalWords: backtab
2516
2517 @node Init Non-ASCII
2518 @subsection Non-@acronym{ASCII} Characters in Init Files
2519 @cindex international characters in @file{.emacs}
2520 @cindex non-@acronym{ASCII} characters in @file{.emacs}
2521 @cindex non-@acronym{ASCII} keys, binding
2522 @cindex rebinding non-@acronym{ASCII} keys
2523
2524 Language and coding systems may cause problems if your init file
2525 contains non-@acronym{ASCII} characters, such as accented letters, in
2526 strings or key bindings.
2527
2528 If you want to use non-@acronym{ASCII} characters in your init file,
2529 you should put a @w{@samp{-*-coding: @var{coding-system}-*-}} tag on
2530 the first line of the init file, and specify a coding system that
2531 supports the character(s) in question. @xref{Recognize Coding}. This
2532 is because the defaults for decoding non-@acronym{ASCII} text might
2533 not yet be set up by the time Emacs reads those parts of your init
2534 file which use such strings, possibly leading Emacs to decode those
2535 strings incorrectly. You should then avoid adding Emacs Lisp code
2536 that modifies the coding system in other ways, such as calls to
2537 @code{set-language-environment}.
2538
2539 To bind non-@acronym{ASCII} keys, you must use a vector (@pxref{Init
2540 Rebinding}). The string syntax cannot be used, since the
2541 non-@acronym{ASCII} characters will be interpreted as meta keys. For
2542 instance:
2543
2544 @example
2545 (global-set-key [?@var{char}] 'some-function)
2546 @end example
2547
2548 @noindent
2549 Type @kbd{C-q}, followed by the key you want to bind, to insert @var{char}.
2550
2551 @strong{Warning:} if you change the keyboard encoding, or change
2552 between multibyte and unibyte mode, or anything that would alter which
2553 code @kbd{C-q} would insert for that character, this key binding may
2554 stop working. It is therefore advisable to use one and only one
2555 coding system, for your init file as well as the files you edit. For
2556 example, don't mix the @samp{latin-1} and @samp{latin-9} coding
2557 systems.