www.delorie.com/gnu/docs/emacs/emacs_497.html   search  
 
Buy the book!


GNU Emacs Manual

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

AD.4.6 Rebinding Keys in Your Init File

If you have a set of key bindings that you like to use all the time, you can specify them in your `.emacs' file by using their Lisp syntax. (See section AD.7 The Init File, `~/.emacs'.)

The simplest method for doing this works for ASCII characters and Meta-modified ASCII characters only. This method uses a string to represent the key sequence you want to rebind. For example, here's how to bind C-z to shell:

 
(global-set-key "\C-z" 'shell)

This example uses a string constant containing one character, C-z. The single-quote before the command name, shell, marks it as a constant symbol rather than a variable. If you omit the quote, Emacs would try to evaluate shell immediately as a variable. This probably causes an error; it certainly isn't what you want.

Here is another example that binds a key sequence two characters long:

 
(global-set-key "\C-xl" 'make-symbolic-link)

To put TAB, RET, ESC, or DEL in the string, you can use the Emacs Lisp escape sequences, `\t', `\r', `\e', and `\d'. Here is an example which binds C-x TAB:

 
(global-set-key "\C-x\t" 'indent-rigidly)

These examples show how to write some other special ASCII characters in strings for key bindings:

 
(global-set-key "\r" 'newline)               ;; RET
(global-set-key "\d" 'delete-backward-char)  ;; DEL
(global-set-key "\C-x\e\e" 'repeat-complex-command)  ;; ESC

When the key sequence includes function keys or mouse button events, or non-ASCII characters such as C-= or H-a, you must use the more general method of rebinding, which uses a vector to specify the key sequence.

The way to write a vector in Emacs Lisp is with square brackets around the vector elements. Use spaces to separate the elements. If an element is a symbol, simply write the symbol's name--no other delimiters or punctuation are needed. If a vector element is a character, write it as a Lisp character constant: `?' followed by the character as it would appear in a string.

Here are examples of using vectors to rebind C-= (a control character not in ASCII), C-M-= (not in ASCII because C-= is not), H-a (a Hyper character; ASCII doesn't have Hyper at all), F7 (a function key), and C-Mouse-1 (a keyboard-modified mouse button):

 
(global-set-key [?\C-=] 'make-symbolic-link)
(global-set-key [?\M-\C-=] 'make-symbolic-link)
(global-set-key [?\H-a] 'make-symbolic-link)
(global-set-key [f7] 'make-symbolic-link)
(global-set-key [C-mouse-1] 'make-symbolic-link)

You can use a vector for the simple cases too. Here's how to rewrite the first three examples above, using vectors to bind C-z, C-x l, and C-x TAB:

 
(global-set-key [?\C-z] 'shell)
(global-set-key [?\C-x ?l] 'make-symbolic-link)
(global-set-key [?\C-x ?\t] 'indent-rigidly)
(global-set-key [?\r] 'newline)
(global-set-key [?\d] 'delete-backward-char)
(global-set-key [?\C-x ?\e ?\e] 'repeat-complex-command)

As you see, you represent a multi-character key sequence with a vector by listing each of the characters within the square brackets that delimit the vector.


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

  webmaster     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003