To What Is C-h Bound?

In order to find the symbol that names the Help command, we can use C-h b, which invokes another command called describe-bindings. This is one of the Help system's many subcommands. It presents a window listing all the keybindings in effect. Looking through it for the C-h binding, we find this line:

C-h          help-command

This tells us that help-command is the symbol that names the function that invokes Help.

Our Lisp example is almost complete, but we can't just write

(global-set-key "\M-?" help-command)       ;almost right!

This is wrong because of the way symbols are interpreted when they appear in Lisp expressions. If a symbol appears in the first position of a list, it's the name of a function to execute. If it appears elsewhere, it's a variable whose value needs to be retrieved. But when we run global-set-key as shown, we don't want the value contained in help-command, whatever that may be. The value we want is the symbol help-command itself. In short, we wish to prevent the symbol from being evaluated before it's passed to global-set-key. After all, as far as we know, help-command doesn't have a value as a variable.

The way to prevent a symbol (or any Lisp expression) from being evaluated is to quote it by preceding it with a single quote ('). It looks like this:

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

Our Lisp example is now complete. If you place this line in your .emacs file, then M-? will invoke help-command the next time you run Emacs, and in all future Emacs sessions. (Soon we'll learn how to make Lisp expressions take effect immediately.) M-? b will invoke describe-bindings the way C-h b did before (and still does—at this point, both M-? and C-h are bound to help-command).

Incidentally, to illustrate the difference between quoting and not quoting, the same effect could be achieved with

(setq x 'help-command)             ;setq  assigns a variable
(global-set-key "\M-?" x)          ;use x's value

The first line sets the variable x to hold the symbol help-command. The second uses x's value—the symbol help-command—as the binding for M-?. The only difference between this and the first example is that now you end up with a leftover variable named x that didn't exist before.

Symbols aren't the only things that may follow a ' character; any Lisp expression can be quoted, including lists, numbers, strings, and other kinds of expressions we'll learn about later. Writing 'expr is shorthand for

(quote  expr)

which, when evaluated, yields expr. You might have noticed that a quote is required before the symbol help-command but not before the string argument, "\M-?". This is because in Lisp, strings are self-evaluating, which means that when the string is evaluated, the result is the string itself. So quoting it, while harmless, is redundant. Numbers, characters, and vectors are other types of self-evaluating Lisp expressions.

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.