Node:Keymaps, Next:, Previous:Keybindings, Up:Emacs Initialization



16.8 Keymaps

Emacs uses keymaps to record which keys call which commands. When you use global-set-key to set the keybinding for a single command in all parts of Emacs, you are specifying the keybinding 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 global-set-key function binds, or rebinds, the global keymap. For example, the following binds the key C-x C-b to the function buffer-menu:

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

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

(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 define-key expressions in loaddefs.el as well as in the various mode libraries, such as cc-mode.el and lisp-mode.el.

See Key Bindings, and Keymaps, for more information about keymaps.