Keys and Strings

The goal of this chapter is to make any BS-generating key work the same as any DEL-generating key. Unfortunately, C-h will no longer invoke the Help command. You'll need to choose some other key to invoke Help; my own choice for the new Help key is META-question-mark.

The META Key

The META key works like the CONTROL key and the SHIFT key, which means that you hold it down while pressing some other key. Such keys are called modifiers. Not all keyboards have a META key, though. Sometimes the ALT key will serve the same purpose, but not all keyboards have an ALT key, either.

In any case, you don't need to use the META key or the ALT key. The single keystroke META-x can always be replaced with the two-key sequence ESC x. (Note that ESC is not a modifier—you press it and release it like a normal key before pressing x.)

Binding Keystrokes to Commands

In Emacs, every keystroke invokes a command or is part of a multiple-key sequence that invokes a command. Commands are special kinds of Lisp functions, as we will see. Making a keystroke invoke a command such as Help is known as binding the keysequence to the command. We'll need to execute some Lisp code to bind keys to commands. One Lisp function for doing this is global-set-key.

Here's what a call to global-set-key looks like. Remember that a function call in Lisp is simply a parenthesized list. The first element of the list is the name of the function, and any remaining elements are the arguments. The function global-set-key takes two arguments: the keysequence to bind, and the command to bind it to.

(global-set-key  keysequence  command)

One important thing to note about Emacs Lisp is that it is case-sensitive.

The keysequence we've chosen is META-question-mark. How is this denoted in Emacs Lisp?

Denoting Keys in Strings

There are a few different ways to write a keysequence in Emacs Lisp notation. The simplest is to write the keys as a string. In Lisp, a string is a sequence of characters surrounded with double quotes.

"xyz"      ;three-character string

To get a double quote in the string itself, precede it with a backslash (\):

"I said, \"Look out!\""

This represents the string containing these characters:

I said, "Look out!"

To include a backslash in the string, precede it with another backslash.

An ordinary key is denoted by writing the character in a string. For instance, the keystroke q is denoted in Lisp by the string "q". The keystroke \ would be written as "\\".

Special characters such as META-question-mark are denoted in strings using a special syntax: "\M-?". Even though there are four characters inside the double quotes, Emacs reads this as a string containing the single character called META-question-mark.[3]

In Emacs jargon, M-x is shorthand for META-x, and "\M-x" is the string version. CONTROL-x is abbreviated C-x in Emacs documentation, and in strings is written as: "\C-x". You can combine the CONTROL and META keys, too. CONTROL-META-x is denoted C-M-x and is written as "\C-\M-x" in strings. "\C-\M-x", incidentally, is interchangeable with "\M-\C-x" (META-CONTROL-x).

(CONTROL-x is also sometimes abbreviated ^x in documentation, corresponding to this alternative string syntax: "\^x".)

Now we know how to fill in the first argument to our global-set-key example:

(global-set-key "\M-?"  command)

(One other way to write the keysequence "\M-?" is "\e?". The string "\e" denotes the escape character, and M-x is the same as ESC x.)

Next we must figure out what belongs in place of command. This argument should be the name of the Help function that we want M-? to invoke—i.e., the function that C-h now invokes. In Lisp, functions are named with symbols. Symbols are like function names or variable names in other languages, although Lisp allows a wider variety of characters in symbols than most languages allow in their variable names. For instance, legal Lisp symbols include let* and up&down-p.



[3] You can use the length function, which returns the length of a string, to confirm this. If you evaluate (length "\M-?"), the result is 1. How to "evaluate" is covered later in this chapter.

Get Writing GNU Emacs Extensions now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.