Next: Why does Emacs say ‘Key sequence XXX uses invalid prefix characters’?, Up: Key bindings [Contents][Index]
Keys can be bound to commands either interactively or in your init file (see How do I set up an init file properly?). To interactively bind keys for all modes, type M-x global-set-key RET key cmd RET.
To bind a key just in the current major mode, type M-x local-set-key RET key cmd RET.
See Key Bindings in The GNU Emacs Manual.
To make the process of binding keys interactively easier, use the following “trick”: First bind the key interactively, then immediately type C-x ESC ESC C-a C-k C-g. Now, the command needed to bind the key is in the kill ring, and can be yanked into your init file. If the key binding is global, no changes to the command are required. For example,
(global-set-key [f1] 'help-for-help)
can be placed directly into your init file. If the key binding is local, the command is used in conjunction with the ‘add-hook’ function. For example, in TeX mode, a local binding might be
(add-hook 'tex-mode-hook (lambda () (local-set-key [f1] 'help-for-help)))
(global-unset-key [?\e ?{]) ;; or (local-unset-key [?\e ?{])
(global-set-key [f10] [?\C-x?\e?\e?\C-a?\C-k?\C-g]) ;; or (global-set-key [f10] "\C-x\e\e\C-a\C-k\C-g")