Next: , Previous: , Up: Your .emacs File   [Contents][Index]

16.8 Keymaps

Emacs uses keymaps to record which keys call which commands. When you use keymap-global-set to set the key binding for a single command in all parts of Emacs, you are specifying the key binding in current-global-map.

Specific modes, such as C mode or Text mode, have their own keymaps; the mode-specific keymaps override the global map that is shared by all buffers.

The keymap-global-set function binds, or rebinds, the global keymap. For example, the following binds the key C-x C-b to the function buffer-menu:

(keymap-global-set "C-x C-b" 'buffer-menu)

Mode-specific keymaps are bound using the keymap-set function, which takes a specific keymap as an argument, as well as the key and the command. For example, the following expression binds the texinfo-insert-@group command to C-c C-c g:

(keymap-set texinfo-mode-map "C-c C-c g" 'texinfo-insert-@group)

Historically, keymaps are bound using a lower-level function, define-key, which is now considered legacy. While you are encouraged to use keymap-set, you likely would encounter define-key in various places. The above key binding can be rewritten using define-key as:

(define-key texinfo-mode-map "\C-c\C-cg" 'texinfo-insert-@group)

The texinfo-insert-@group function itself is a little extension to Texinfo mode that inserts ‘@group’ into a Texinfo file. I use this command all the time and prefer to type the three strokes C-c C-c g rather than the six strokes @ g r o u p. (‘@group’ and its matching ‘@end group’ are commands that keep all enclosed text together on one page; many multi-line examples in this book are surrounded by ‘@group … @end group’.)

Here is the texinfo-insert-@group function definition:

(defun texinfo-insert-@group ()
  "Insert the string @group in a Texinfo buffer."
  (interactive)
  (beginning-of-line)
  (insert "@group\n"))

(Of course, I could have used Abbrev mode to save typing, rather than write a function to insert a word; but I prefer key strokes consistent with other Texinfo mode key bindings.)

You will see numerous keymap-set and define-key expressions in loaddefs.el as well as in the various mode libraries, such as cc-mode.el and lisp-mode.el.

See Customizing Key Bindings in The GNU Emacs Manual, and Keymaps in The GNU Emacs Lisp Reference Manual, for more information about keymaps.

Next: Loading Files, Previous: Some Key Bindings, Up: Your .emacs File   [Contents][Index]