Go to the previous, next section.

Characters, Keys and Commands

This chapter explains the character set used by Emacs for input commands and for the contents of files, and also explains the concepts of keys and commands which are necessary for understanding how your keyboard input is understood by Emacs.

Keyboard Input

GNU Emacs uses an extension of the ASCII character set for keyboard input. ASCII consists of 128 character codes. Some of these codes are assigned graphic symbols such as `a' and `='; the rest are control characters, such as Control-a (usually written C-a for short). C-a gets its name from the fact that you type it by holding down the CTRL key and then pressing a.

Some control characters have special names, and special keys you can type them with: for example, RET, TAB, LFD, DEL and ESC. The space character is usually referred to below as SPC, even though strictly speaking it is a graphic character whose graphic happens to be blank.

On ASCII terminals, the shift key is meaningless with control characters: C-a and C-A are the same character, and Emacs cannot distinguish them. Under X Windows, these are distinct characters, but the standard key bindings treat them the same in all contexts.

On ASCII terminals, there are only 32 possible control characters. These are the control variants of letters and `@[]\^_'. Under X Windows, every non-control character has a control variant. For example, C-+ and C-5 are meaningful under X.

Emacs extends the ASCII character code by adding an extra bit to each character. The additional bit is called Meta. Any character can be made Meta; examples of Meta characters include Meta-a (normally written M-a, for short), M-A (not the same character as M-a, but those two characters normally have the same meaning in Emacs), M-RET, and M-C-a. For traditional reasons, M-C-a is usually called C-M-a; logically speaking, the order in which the modifier keys CTRL and META are mentioned does not matter.

Some terminals have a META key, and allow you to type Meta characters by holding this key down. Thus, Meta-a is typed by holding down META and pressing a. The META key works much 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. Thus, 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.

X Windows provides several other modifier keys that can be applied to any keyboard input character. These are called SUPER, HYPER and ALT. With them, you can make characters that we denote with `s-', `H-' and `A-'. Thus, s-H-C-x is short for Super-Hyper-Control-x. Not all X terminals actually provide keys for these modifier flags, and the standard key bindings of Emacs do not include such characters. But you can assign them meanings of your own by customizing Emacs.

Keyboard input includes keyboard keys that are not characters at all: for example function keys and arrow keys. Mouse buttons are also outside the gamut of characters. These inputs do not have numeric character codes. Instead, Emacs represents them by their names (actually, Lisp objects called symbols).

Input characters and non-character inputs are collectively called input events.

ASCII terminals cannot really send anything to the computer except ASCII characters. These terminals use a sequence of characters to represent each function key. But that is invisible to the Emacs user, because the keyboard input routines recognize these special sequences and converts them to names before any other part of Emacs gets to see them.

Keys

A key sequence (key, for short) is a sequence of input events that combine as part of the invocation of a single command. Recall that input events include both keyboard characters and non-character inputs (function keys, arrow keys, mouse buttons, and so forth).

If the sequence is enough to invoke a command, it is a complete key. If it isn't long enough to be complete, we call it a prefix key. Examples of complete keys include C-a, X, RET, NEXT (a function key), DOWN (an arrow key), C-x C-f and C-x 4 C-f.

Most single characters constitute complete keys in the standard Emacs command bindings. A few of them are prefix keys. A prefix key can be followed by additional input characters (or other events) to make a longer key, which may itself be complete or a prefix.

For example, C-x is a prefix key, so C-x and the next input character combine to make a two-character key sequence. Most of these key sequences are complete keys, including C-x C-f and C-x b. A few, such as C-x 4 and C-x r, are themselves prefix keys that lead to three-character key sequences. There's no limit to the length of a key sequence, but any key sequence longer than one character must be reached through a chain of prefix keys.

By contrast, the two-character sequence C-f C-k is not a key, because the C-f is a complete key in itself. It's impossible to give C-f C-k an independent meaning as a command. C-f C-k is two key sequences, not one.

All told, the prefix keys in Emacs are C-c, C-x, C-h, C-x C-a, C-x n, C-x r, C-x v, C-x 4, C-x 5, and ESC. But this is not cast in concrete; it is just a matter of Emacs's standard key bindings. In customizing Emacs, you could make new prefix keys, or eliminate these. See section Customizing Key Bindings.

Whether a sequence is a key can be changed by customization. For example, if you redefine C-f as a prefix, C-f C-k automatically becomes a key (complete, unless you define it too as a prefix). Conversely, if you remove the prefix definition of C-x 4, then C-x 4 f (or C-x 4 anything) is no longer a key.

Typing the help character (C-h) after a prefix character usually displays a list of the commands starting with that prefix. There are a few prefix characters for which this doesn't work--for historical reasons, they have other meanings for C-h which are not easy to change.

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 named commands, and then gives keys their meanings by binding them to commands.

Every command has a name chosen by a programmer. The name is usually made of a few English words separated by dashes; for example, next-line or forward-word. A command also has a function definition which is a Lisp program; this is what makes the command do what it does. In Emacs Lisp, a command is actually a special kind of Lisp function; one which specifies how to read arguments for it and call it interactively. For more information on commands and functions, see section `What Is a Function' in The Emacs Lisp Reference Manual. (The definition we use in this manual is simplified slightly.)

The bindings between keys and commands are recorded in various tables called keymaps. See section Keymaps.

When we say that "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. It is the command next-line that is programmed to move down vertically. C-n has this effect because it is bound to that command. If you rebind C-n to the command forward-word then C-n will move forward by words instead. Rebinding keys is a common method of customization.

In the rest of this manual, we usually ignore this subtlety to keep things simple. To give the customizer the information he needs, we state the name of the command which 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 examines the variable and behaves differently accordingly. 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. See section Variables.

Character Set for Text

Emacs buffers use an 8-bit character set, because bytes have 8 bits. ASCII graphic characters in Emacs buffers are displayed with their graphics. The newline character (which has the same character code as LFD) is displayed by starting a new line. The tab character is displayed by moving to the next tab stop column (usually every 8 columns). 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) is displayed as `\243'. You can customize the display of these character codes (or ANSI characters) by creating a display table; this is useful for editing files that use 8-bit European character sets. See section `Display Tables' in The Emacs Lisp Reference Manual.

Go to the previous, next section.