Before wrapping up this first example, let's discuss Emacs's most important on-line help facility, apropos
. Suppose you're one of those who have both BS and DEL keys and think it's a good idea for BS to erase the character preceding the cursor and DEL to erase the character following the cursor. You know that delete-backward-char
is the command that accomplishes the former, but you don't know which command achieves the latter. You strongly suspect that Emacs must have such a command. How do you find it?
The answer is to use the apropos
command, which allows you to search all known variables and functions for a pattern you specify. Try this:[7]
M-x apropos RET delete RET
The result is a buffer listing all the matches for "delete" among Emacs's variables and functions. If we search that buffer for occurrences of the word "character," we narrow the field down to
backward-delete-char Command: Delete the previous N characters (following if N is negative). backward-delete-char-untabify Command: Delete characters backward, changing tabs into spaces. delete-backward-char Command: Delete the previous N characters (following if N is negative). delete-char Command: Delete the following N characters (previous if N is negative).
The function delete-char
is the one we want.
(global-set-key "\C-?" 'delete-char)
(For historical reasons, the way to write the DEL character is CONTROL-question-mark.)
You may invoke apropos
with a prefix argument. In Emacs, pressing C-u before executing a command is a way to pass extra information to the command. Frequently, C-u is followed by a number; for instance, C-u 5 C-b means "move the cursor left 5 characters." Sometimes the extra information is just the fact that you pressed C-u.
When apropos
is invoked with a prefix argument, it not only reports Emacs functions and variables that match the search pattern, it also reports any existing keybindings for each command in the list. (This isn't the default because finding the keybindings can be slow.) Using C-u M-x apropos RET delete RET and picking out occurrences of "character" as before, we come up with:
backward-delete-char (not bound to any keys) Command: Delete the previous N characters (following if N is negative). backward-delete-char-untabify (not bound to any keys) Command: Delete characters backward, changing tabs into spaces. delete-backward-char C-h, DEL Command: Delete the previous N characters (following if N is negative). delete-char C-d Command: Delete the following N characters (previous if N is negative).
This confirms that both C-h and DEL now invoke delete-backward-char
, and also informs us that delete-char
already has a binding: C-d. After we execute
(global-set-key "\C-?" 'delete-char)
if we run apropos
again, we find
backward-delete-char (not bound to any keys) Command: Delete the previous N characters (following if N is negative). backward-delete-char-untabify (not bound to any keys) Command: Delete characters backward, changing tabs into spaces. delete-backward-char C-h Command: Delete the previous N characters (following if N is negative). delete-char C-d, DEL Command: Delete the following N characters (previous if N is negative).
When we know that the target of our search is an Emacs command, as opposed to a variable or function, we can further limit the scope of the search by using command-apropos
(M-? a) instead of apropos
. The difference between a command and other Lisp functions is that commands have been written specially to be invoked interactively, i.e., from a keybinding or with M-x. Functions that aren't commands can only be invoked as function calls from other Lisp code or by such commands as eval-expression
and eval-last-sexp
. We'll look at the roles of functions and commands more in the next chapter.
[7] All Emacs commands, regardless of which keys (if any) they're bound to, can be invoked with M-x command-name RET. Naturally, M-x is itself just a keybinding for a command, execute-extended-command
, which prompts for the name of a command to execute.
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.