custom.texi 106 KB

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