Go to the first, previous, next, last section, table of contents.

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

  1. Find out what character sequence is generated by the keystroke sequence you wish to bind to a command. See section How do I tell what characters my function or arrow keys emit? for how to do this. Keep in mind that the character sequences generated by a keystroke sequence varies from one terminal to another. You may also get different results depending on what type of machine you are running on (see section How do I use function keys under X Windows?). For example, these keystrokes may generate these character sequences:
      F1        ---> ESC [ 2 2 4 z
      Shift-R10 ---> ESC O t
      L7        ---> ESC [ 3 1 ~
      Remove    ---> C-@
    
  2. Figure out what the Emacs Lisp syntax is for this character sequence. Inside an Emacs Lisp string, RET, LFD, DEL, ESC, SPC, and TAB are specified with `\r', `\n', `\C-?', `\e', ` ', and `\t'. C-x is specified by `\C-x'. M-x is specified the same was as "ESC x". (Control characters may also be specified as themselves, but I don't recommend it.) An Emacs Lisp string begins and ends with the double quote character, `"'. Here are some examples:
      ESC [ D       ---> "\e[D"
      ESC [ 2 2 7 z ---> "\e[227z"
      ESC [ 1 8 ~   ---> "\e[18~"
      C-M-r         ---> "\e\C-r"
    
  3. If some prefix of the character sequence is already bound, you must unbind it by binding it to `nil'. For example:
      (global-set-key "\e[" nil)
    
  4. Pick a command to bind your key sequence to.A command can be a "symbol" with a function definition, or a "lambda list", or a string (which is treated as a macro). For example:
      (global-set-key "\e[D" 'backward-char)
      (global-set-key "\e[227~" "\exgoto-line\r") ; macro
    

See `Key Bindings' and `Rebinding' in the online manual.

In Emacs 19 (including Lucid Emacs), you can bind function key F24 like this:


  (global-set-key 'f24 'some-command)

Go to the first, previous, next, last section, table of contents.