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

1 Keystrokes, Key Sequences, and Key Bindings


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

1.1 Keystrokes as Building Blocks of Key Sequences

Earlier versions of Emacs used only the ASCII character set, which defines 128 different character codes. Some of these codes are assigned graphic symbols like ‘a’ and ‘=’; the rest are control characters, such as Control-a (also called C-a). C-a means you hold down the <CTRL> key and then press a.

Keybindings in XEmacs are not restricted to the set of keystrokes that can be represented in ASCII. XEmacs can tell the difference between, for example, Control-h, Control-Shift-h, and Backspace.

A keystroke is like a piano chord: you get it by simultaneously striking several keys. To be more precise, a keystroke consists of a possibly empty set of modifiers followed by a single keysym. The set of modifiers is small; it consists of Control, Meta, Super, Hyper, and Shift.

The rest of the keys on your keyboard, along with the mouse buttons, make up the set of keysyms. A keysym is usually what is printed on the keys on your keyboard. Here is a table of some of the symbolic names for keysyms:

a,b,c...

alphabetic keys

f1,f2...

function keys

button1

left mouse button

button2

middle mouse button

button3

right mouse button

button1up

upstroke on the left mouse button

button2up

upstroke on the middle mouse button

button3up

upstroke on the right mouse button

return

Return key

Use the variable keyboard-translate-table only if you are on a dumb tty, as it cannot handle input that cannot be represented as ASCII. The value of this variable is a string used as a translate table for keyboard input or nil. Each character is looked up in this string and the contents used instead. If the string is of length n, character codes N and up are untranslated. If you are running Emacs under X, you should do the translations with the xmodmap program instead.


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

1.1.1 Representing Keystrokes

XEmacs represents keystrokes as lists. Each list consists of an arbitrary combination of modifiers followed by a single keysym at the end of the list. If the keysym corresponds to an ASCII character, you can use its character code. (A keystroke may also be represented by an event object, as returned by the read-key-sequence function; non-programmers need not worry about this.)

The following table gives some examples of how to list representations for keystrokes. Each list consists of sets of modifiers followed by keysyms:

(control a)

Pressing <CTRL> and a simultaneously.

(control ?a)

Another way of writing the keystroke C-a.

(control 65)

Yet another way of writing the keystroke C-a.

(break)

Pressing the <BREAK> key.

(control meta button2up)

Release the middle mouse button, while pressing <CTRL> and <META>.

Note: As you define keystrokes, you can use the shift key only as a modifier with characters that do not have a second keysym on the same key, such as backspace and tab. It is an error to define a keystroke using the <shift> modifier with keysyms such as a and =. The correct forms are A and +.


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

1.1.2 Representing Key Sequences

A complete key sequence is a sequence of keystrokes that Emacs understands as a unit. Key sequences are significant because you can bind them to commands. Note that not all sequences of keystrokes are possible key sequences. In particular, the initial keystrokes in a key sequence must make up a prefix key sequence.

Emacs represents a key sequence as a vector of keystrokes. Thus, the schematic representation of a complete key sequence is as follows:

  [(modifier .. modifer keysym) ... (modifier .. modifier keysym)]

Here are some examples of complete key sequences:

[(control c) (control a)]

Typing C-c followed by C-a

[(control c) (control 65)]

Typing C-c followed by C-a. (Using the ASCII code for the character ‘a’)

[(control c) (break)]

Typing C-c followed by the break character.

A prefix key sequence is the beginning of a series of longer sequences that are valid key sequences; adding any single keystroke to the end of a prefix results in a valid key sequence. For example, control-x is standardly defined as a prefix. Thus there is a two-character key sequence starting with C-x for each valid keystroke, giving numerous possibilities. Here are some samples:

Adding one character to a prefix key does not have to form a complete key. It could make another, longer prefix. For example, [(control x) (\4)] is itself a prefix that leads to any number of different three-character keys, including [(control x) (\4) (f)], [(control x) (\4) (b)] and so on. It would be possible to define one of those three-character sequences as a prefix, creating a series of four-character keys, but we did not define any of them this way.

By contrast, the two-character sequence [(control f) (control k)] is not a key, because the (control f) is a complete key sequence in itself. You cannot give [(control f (control k)] an independent meaning as a command while (control f) is a complete sequence, because Emacs would understand <C-f C-k> as two commands.

The predefined prefix key sequences in Emacs are (control c), (control x), (control h), [(control x) (\4)], and escape. You can customize Emacs and could make new prefix keys or eliminate the default key sequences. @xref{Key Bindings}. For example, if you redefine (control f) as a prefix, [(control f) (control k)] automatically becomes a valid key sequence (complete, unless you define it as a prefix as well). Conversely, if you remove the prefix definition of [(control x) (\4)], [(control x) (\4) (f)] (or [(control x) (\4) anything]) is no longer a valid key sequence.

Note that the above paragraphs uses \4 instead of simply 4, because \4 is the symbol whose name is "4", and plain 4 is the integer 4, which would have been interpreted as the ASCII value. Another way of representing the symbol whose name is "4" is to write ?4, which would be interpreted as the number 52, which is the ASCII code for the character "4". We could therefore actually have written 52 directly, but that is far less clear.


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

1.1.3 String Key Sequences

For backward compatibility, you may also represent a key sequence using strings. For example, we have the following equivalent representations:

"\C-c\C-c"

[(control c) (control c)]

"\e\C-c"

[(meta control c)]


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

1.1.4 Assignment of the <META> Key

Not all terminals have the complete set of modifiers. Terminals that have a <Meta> key allow you to type Meta characters by just holding that key down. To type Meta-a, hold down <META> and press a. On those terminals, the <META> key works like the <SHIFT> key. Such a key is not always labeled <META>, however, as this function is often a special option for a key with some other primary purpose.

If there is no <META> key, you can still type Meta characters using two-character sequences starting with <ESC>. To enter M-a, you could type <ESC> a. To enter C-M-a, you would type ESC C-a. <ESC> is allowed on terminals with Meta keys, too, in case you have formed a habit of using it.

If you are running under X and do not have a <META> key, it is possible to reconfigure some other key to be a <META> key. See section Assignment of the <SUPER> and <HYPER> Keys.

Emacs believes the terminal has a <META> key if the variable meta-flag is non-nil. Normally this is set automatically according to the termcap entry for your terminal type. However, sometimes the termcap entry is wrong, and then it is useful to set this variable yourself. @xref{Variables}, for how to do this.

Note: If you are running under the X window system, the setting of the meta-flag variable is irrelevant.


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

1.1.5 Assignment of the <SUPER> and <HYPER> Keys

Most keyboards do not, by default, have <SUPER> or <HYPER> modifier keys. Under X, you can simulate the <SUPER> or <HYPER> key if you want to bind keys to sequences using super and hyper. You can use the xmodmap program to do this.

For example, to turn your <CAPS-LOCK> key into a <SUPER> key, do the following:

Create a file called ~/.xmodmap. In this file, place the lines

	remove Lock = Caps_Lock
	keysym Caps_Lock = Super_L
	add Mod2 = Super_L

The first line says that the key that is currently called Caps_Lock should no longer behave as a “lock” key. The second line says that this should now be called Super_L instead. The third line says that the key called Super_L should be a modifier key, which produces the Mod2 modifier.

To create a <META> or <HYPER> key instead of a <SUPER> key, replace the word Super above with Meta or Hyper.

Just after you start up X, execute the command xmodmap /.xmodmap. You can add this command to the appropriate initialization file to have the command executed automatically.

If you have problems, see the documentation for the xmodmap program. The X keyboard model is quite complicated, and explaining it is beyond the scope of this manual. However, we reprint the following description from the X Protocol document for your convenience:

A list of keysyms is associated with each keycode. If that list (ignoring trailing NoSymbol entries) is a single keysym ‘K’, then the list is treated as if it were the list ``K NoSymbol K NoSymbol''. If the list (ignoring trailing NoSymbol entries) is a pair of keysyms ‘K1 K2’, then the list is treated as if it were the list ``K1 K2 K1 K2''. If the list (ignoring trailing NoSymbol entries) is a triple of keysyms ‘K1 K2 K3’, then the list is treated as if it were the list ``K1 K2 K3 NoSymbol''.

The first four elements of the list are split into two groups of keysyms. Group 1 contains the first and second keysyms; Group 2 contains third and fourth keysyms. Within each group, if the second element of the group is NoSymbol, then the group should be treated as if the second element were the same as the first element, except when the first element is an alphabetic keysym ‘K’ for which both lowercase and uppercase forms are defined. In that case, the group should be treated as if the first element were the lowercase form of ‘K’ and the second element were the uppercase form of ‘K’.

The standard rules for obtaining a keysym from a KeyPress event make use of only the Group 1 and Group 2 keysyms; no interpretation of other keysyms in the list is given here. (That is, the last four keysyms are unused.)

Which group to use is determined by modifier state. Switching between groups is controlled by the keysym named Mode_switch. Attach that keysym to some keycode and attach that keycode to any one of the modifiers Mod1 through Mod5. This modifier is called the group modifier. For any keycode, Group 1 is used when the group modifier is off, and Group 2 is used when the group modifier is on.

Within a group, which keysym to use is also determined by modifier state. The first keysym is used when the Shift and Lock modifiers are off. The second keysym is used when the Shift modifier is on, or when the Lock modifier is on and the second keysym is uppercase alphabetic, or when the Lock modifier is on and is interpreted as ShiftLock. Otherwise, when the Lock modifier is on and is interpreted as CapsLock, the state of the Shift modifier is applied first to select a keysym, but if that keysym is lower-case alphabetic, then the corresponding upper-case keysym is used instead.

In addition to the above information on keysyms, we also provide the following description of modifier mapping from the InterClient Communications Conventions Manual:

X11 supports 8 modifier bits, of which 3 are pre-assigned to Shift, Lock, and Control. Each modifier bit is controlled by the state of a set of keys, and these sets are specified in a table accessed by GetModifierMapping() and SetModifierMapping().

A client needing to use one of the pre-assigned modifiers should assume that the modifier table has been set up correctly to control these modifiers. The Lock modifier should be interpreted as Caps Lock or Shift Lock according to whether the keycodes in its controlling set include XK_Caps_Lock or XK_Shift_Lock.

Clients should determine the meaning of a modifier bit from the keysyms being used to control it.

A client needing to use an extra modifier, for example Meta, should:

  1. Scan the existing modifier mappings.
    1. If it finds a modifier that contains a keycode whose set of keysyms includes XK_Meta_L or XK_Meta_R, it should use that modifier bit.
    2. If there is no existing modifier controlled by XK_Meta_L or XK_Meta_R, it should select an unused modifier bit (one with an empty controlling set) and:
  2. If there is a keycode with XL_Meta_L in its set of keysyms, add that keycode to the set for the chosen modifier, and then:
    1. If there is a keycode with XL_Meta_R in its set of keysyms, add that keycode to the set for the chosen modifier, and then:
    2. If the controlling set is still empty, interact with the user to select one or more keys to be Meta.
  3. If there are no unused modifier bits, ask the user to take corrective action.

This means that the Mod1 modifier does not necessarily mean Meta, although some applications (such as twm and emacs 18) assume that. Any of the five unassigned modifier bits could mean Meta; what matters is that a modifier bit is generated by a keycode which is bound to the keysym Meta_L or Meta_R.

Therefore, if you want to make a <META> key, the right way is to make the keycode in question generate both a Meta keysym and some previously-unassigned modifier bit.


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

1.2 Representation of Characters

This section briefly discusses how characters are represented in Emacs buffers. See section Representing Key Sequences for information on representing key sequences to create key bindings.

ASCII graphic characters in Emacs buffers are displayed with their graphics. <LFD> is the same as a newline character; it is displayed by starting a new line. <TAB> is displayed by moving to the next tab stop column (usually every 8 spaces). Other control characters are displayed as a caret (‘^’) followed by the non-control version of the character; thus, C-a is displayed as ‘^A’. Non-ASCII characters 128 and up are displayed with octal escape sequences; thus, character code 243 (octal), also called M-# when used as an input character, is displayed as ‘\243’.

The variable ctl-arrow may be used to alter this behavior. @xref{Display Vars}.


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

1.3 Keys and Commands

This manual is full of passages that tell you what particular keys do. But Emacs does not assign meanings to keys directly. Instead, Emacs assigns meanings to functions, and then gives keys their meanings by binding them to functions.

A function is a Lisp object that can be executed as a program. Usually it is a Lisp symbol that has been given a function definition; every symbol has a name, usually made of a few English words separated by dashes, such as next-line or forward-word. It also has a definition, which is a Lisp program. Only some functions can be the bindings of keys; these are functions whose definitions use interactive to specify how to call them interactively. Such functions are called commands, and their names are command names. More information on this subject will appear in the GNU Emacs Lisp Manual.

The bindings between keys and functions are recorded in various tables called keymaps. @xref{Key Bindings} for more information on key sequences you can bind commands to. @xref{Keymaps} for information on creating keymaps.

When we say “C-n moves down vertically one line” we are glossing over a distinction that is irrelevant in ordinary use but is vital in understanding how to customize Emacs. The function next-line is programmed to move down vertically. C-n has this effect because it is bound to that function. If you rebind C-n to the function forward-word then C-n will move forward by words instead. Rebinding keys is a common method of customization.

The rest of this manual usually ignores this subtlety to keep things simple. To give the customizer the information needed, we often state the name of the command that really does the work in parentheses after mentioning the key that runs it. For example, we will say that “The command C-n (next-line) moves point vertically down,” meaning that next-line is a command that moves vertically down and C-n is a key that is standardly bound to it.

While we are on the subject of information for customization only, it’s a good time to tell you about variables. Often the description of a command will say, “To change this, set the variable mumble-foo.” A variable is a name used to remember a value. Most of the variables documented in this manual exist just to facilitate customization: some command or other part of Emacs uses the variable and behaves differently depending on its setting. Until you are interested in customizing, you can ignore the information about variables. When you are ready to be interested, read the basic information on variables, and then the information on individual variables will make sense. @xref{Variables}.


[Top] [Contents] [Index] [ ? ]

About This Document

This document was generated on December 6, 2024 using texi2html 5.0.

The buttons in the navigation panels have the following meaning:

Button Name Go to From 1.2.3 go to
[ << ] FastBack Beginning of this chapter or previous chapter 1
[ < ] Back Previous section in reading order 1.2.2
[ Up ] Up Up section 1.2
[ > ] Forward Next section in reading order 1.2.4
[ >> ] FastForward Next chapter 2
[Top] Top Cover (top) of document  
[Contents] Contents Table of contents  
[Index] Index Index  
[ ? ] About About (help)  

where the Example assumes that the current position is at Subsubsection One-Two-Three of a document of the following structure:


This document was generated on December 6, 2024 using texi2html 5.0.