[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10. Key bindings

10.1 How do I bind keys (including function keys) to commands?  
10.2 Why does Emacs say "Key sequence XXX uses invalid prefix characters"?  
10.3 Why doesn't this [terminal or window-system setup] code work in my `.emacs' file, but it works just fine after Emacs starts up?  
10.4 How do I use function keys under X?  
10.5 How do I tell what characters or symbols my function or arrow keys emit?  
10.6 How do I set the X key "translations" for Emacs?  
10.7 How do I handle C-s and C-q being used for flow control?  
10.8 How do I bind C-s and C-q (or any key) if these keys are filtered out?  
10.9 Why does the Backspace key invoke help?  
10.10 Why doesn't Emacs look at the `stty' settings for Backspace vs. Delete?  
10.11 How do I swap two keys?  
10.12 How do I produce C-XXX with my keyboard?  
10.13 What if I don't have a Meta key?  
10.14 What if I don't have an Escape key?  
10.15 Can I make my Compose Character key behave like a Meta key?  
10.16 How do I bind a combination of modifier key and function key?  
10.17 Why doesn't my Meta key work in an xterm window?  
10.18 Why doesn't my ExtendChar key work as a Meta key under HP-UX 8.0 and 9.x?  


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1 How do I bind keys (including function keys) to commands?

Keys can be bound to commands either interactively or in your `.emacs' file. 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 Info file `emacs', node `Key Bindings', for further details.

To make the process of binding keys interactively eaiser, 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 `.emacs' file. If the key binding is global, no changes to the command are required. For example,

 
(global-set-key (quote [f1]) (quote help-for-help))

can be placed directly into the `.emacs' file. If the key binding is local, the command is used in conjunction with the "add-hook" command. For example, in tex-mode, a local binding might be

 
(add-hook 'tex-mode-hook
  (lambda ()
   (local-set-key (quote [f1]) (quote help-for-help))))


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.2 Why does Emacs say "Key sequence XXX uses invalid prefix characters"?

Usually, one of two things has happened. In one case, the control character in the key sequence has been misspecified (e.g. `C-f' used instead of `\C-f' within a Lisp expression). In the other case, a prefix key in the keystroke sequence you were trying to bind was already bound as a complete key. Historically, the `ESC [' prefix was usually the problem, in which case you should evaluate either of these forms before attempting to bind the key sequence:

 
(global-unset-key [?\e ?[])  ;;  or
(global-unset-key "\e[")


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.3 Why doesn't this [terminal or window-system setup] code work in my `.emacs' file, but it works just fine after Emacs starts up?

During startup, Emacs initializes itself according to a given code/file order. If some of the code executed in your `.emacs' file needs to be postponed until the initial terminal or window-system setup code has been executed but is not, then you will experience this problem (this code/file execution order is not enforced after startup).

To postpone the execution of Emacs Lisp code until after terminal or window-system setup, treat the code as a lambda list and set the value of either the term-setup-hook or window-setup-hook variable to this lambda function. For example,

 
(add-hook 'term-setup-hook
          (lambda ()
           (when (string-match "\\`vt220" (or (getenv "TERM") ""))
             ;; Make vt220's "Do" key behave like M-x:
             (global-set-key [do] 'execute-extended-command))))

For information on what Emacs does every time it is started, see the `lisp/startup.el' file.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.4 How do I use function keys under X?

With Emacs 19, functions keys under X are bound like any other key. See section 10.1 How do I bind keys (including function keys) to commands?, for details.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.5 How do I tell what characters or symbols my function or arrow keys emit?

Type C-h c then the function or arrow keys. The command will return either a function key symbol or character sequence (see the Emacs on-line documentation for an explanation). This works for other keys as well.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.6 How do I set the X key "translations" for Emacs?

Emacs is not written using the Xt library by default, so there are no "translations" to be set. (We aren't sure how to set such translations if you do build Emacs with Xt; please let us know if you've done this!)

The only way to affect the behavior of keys within Emacs is through xmodmap (outside Emacs) or define-key (inside Emacs). The define-key command should be used in conjunction with the function-key-map map. For instance,

 
(define-key function-key-map [M-TAB] [?\M-\t])

defines the M-TAB key sequence.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.7 How do I handle C-s and C-q being used for flow control?

C-s and C-q are used in the XON/XOFF flow control protocol. This messes things up when you're using Emacs over a serial line, because Emacs binds these keys to commands by default. Because Emacs won't honor them as flow control characters, too many of these characters are not passed on and overwhelm output buffers. Sometimes, intermediate software using XON/XOFF flow control will prevent Emacs from ever seeing C-s and C-q.

Possible solutions:

For further discussion of this issue, read the file `etc/PROBLEMS' (in the Emacs source directory when you unpack the Emacs distribution).


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.8 How do I bind C-s and C-q (or any key) if these keys are filtered out?

To bind C-s and C-q, use either enable-flow-control or enable-flow-control-on. See section 10.7 How do I handle C-s and C-q being used for flow control?, for usage and implementation details.

To bind other keys, use keyboard-translate. See section 10.11 How do I swap two keys?, for usage details. To do this for an entire site, you should swap the keys in `site-lisp/site-start.el'. See section 10.7 How do I handle C-s and C-q being used for flow control?, for an explanation of why `site-lisp/default.el' should not be used.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.9 Why does the Backspace key invoke help?

The Backspace key (on most keyboards) generates ASCII code 8. C-h sends the same code. In Emacs by default C-h invokes help-command. This is intended to be easy to remember since the first letter of `help' is `h'. The easiest solution to this problem is to use C-h (and Backspace) for help and DEL (the Delete key) for deleting the previous character.

For many people this solution may be problematic:

When Emacs 21 or later runs on a windowed display, it binds the Delete key to a command which deletes the character at point, to make Emacs more consistent with keyboard operation on these systems.

For more information about troubleshooting this problem, see section `If DEL Fails to Delete' in The GNU Emacs Manual.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.10 Why doesn't Emacs look at the `stty' settings for Backspace vs. Delete?

Good question!


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.11 How do I swap two keys?

In Emacs 19, you can swap two keys (or key sequences) by using the keyboard-translate function. For example, to turn C-h into DEL and DEL to C-h, use

 
(keyboard-translate ?\C-h ?\C-?)  ; translate `C-h' to DEL
(keyboard-translate ?\C-? ?\C-h)  ; translate DEL to `C-h'.

The first key sequence of the pair after the function identifies what is produced by the keyboard; the second, what is matched for in the keymaps.

Keyboard translations are not the same as key bindings in keymaps. Emacs contains numerous keymaps that apply in different situations, but there is only one set of keyboard translations, and it applies to every character that Emacs reads from the terminal. Keyboard translations take place at the lowest level of input processing; the keys that are looked up in keymaps contain the characters that result from keyboard translation.

See Info file `emacs', node `Keyboard Translations'.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.12 How do I produce C-XXX with my keyboard?

On terminals (but not under X), some common "aliases" are:

C-2 or C-SPC
C-@

C-6
C-^

C-7 or C-S--
C-_

C-4
C-\

C-5
C-]

C-/
C-?

Often other aliases exist; use the C-h c command and try CTRL with all of the digits on your keyboard to see what gets generated. You can also try the C-h w command if you know the name of the command.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.13 What if I don't have a Meta key?

On many keyboards, the Alt key acts as Meta, so try it.

Instead of typing M-a, you can type ESC a. In fact, Emacs converts M-a internally into ESC a anyway (depending on the value of meta-prefix-char). Note that you press Meta and a together, but with ESC, you press ESC, release it, and then press a.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.14 What if I don't have an Escape key?

Type C-[ instead. This should send ASCII code 27 just like an Escape key would. C-3 may also work on some terminal (but not under X). For many terminals (notably DEC terminals) F11 generates ESC. If not, the following form can be used to bind it:

 
;;; F11 is the documented ESC replacement on DEC terminals.
(define-key function-key-map [f11] [?\e])  


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.15 Can I make my Compose Character key behave like a Meta key?

On a dumb terminal such as a VT220, no. It is rumored that certain VT220 clones could have their Compose key configured this way. If you're using X, you might be able to do this with the xmodmap command.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.16 How do I bind a combination of modifier key and function key?

With Emacs 19 and later, you can represent modified function keys in vector format by adding prefixes to the function key symbol. For example (from the on-line documentation):

 
(global-set-key [?\C-x right] 'forward-page)

where `?\C-x' is the Lisp character constant for the character C-x.

You can use the modifier keys Control, Meta, Hyper, Super, Alt, and Shift with function keys. To represent these modifiers, prepend the strings `C-', `M-', `H-', `s-', `A-', and `S-' to the symbol name. Here is how to make H-M-RIGHT move forward a word:

 
(global-set-key [H-M-right] 'forward-word)

See section 10.1 How do I bind keys (including function keys) to commands?, for general key binding instructions.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.17 Why doesn't my Meta key work in an xterm window?

See Info file `emacs', node `Single-Byte Character Support'.

If the advice in the Emacs manual fails, try all of these methods before asking for further help:


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.18 Why doesn't my ExtendChar key work as a Meta key under HP-UX 8.0 and 9.x?

This is a result of an internationalization extension in X11R4 and the fact that HP is now using this extension. Emacs assumes that the XLookupString function returns the same result regardless of the Meta key state which is no longer necessarily true. Until Emacs is fixed, the temporary kludge is to run this command after each time the X server is started but preferably before any xterm clients are:

 
xmodmap -e 'remove mod1 = Mode_switch'

This will disable the use of the extra keysyms systemwide, which may be undesirable if you actually intend to use them.


[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated by (Blade) GNU s/w Owner on November, 2 2001 using texi2html