Special character |
Definition |
\C-x |
C-x(where x is any letter) |
\C-[ or \e |
Esc |
\M |
Meta |
\C-j or \n |
Newline |
\C-m or \r |
Enter |
\C-i or \t |
Tab |
Thus, the string abc\C-a\ndefis equal to abc, C-a, newline, and def, all concatenated into one string. Note that control characters are case-insensitive—that is, \C-Ais the same thing as \C-a. However, the characters that follow control characters may be case-sensitive; \C-aecould be different from \C-aE, for example.
The function define-keyis the most general because it can be used to bind keys in any keymap. global-set-keybinds keys in the global map only; since there is only one global-map, (global-set-key ...)is the same as (define-key global-map ...). The function local-set-keybinds keys in the local map of the current buffer; it is useful only for specifying temporary key bindings during an Emacs session.
Here is an example of a simple keyboard customization. Let's say you are writing code in a programming language. You compile it and get error messages that contain the line number of the error, and you want to go to that line in the source file to correct the error. [71]You would want to use the goto-linecommand, which is not bound by default to any keystroke. Say you want to bind it to C-x l. The command to put into your .emacs file is
(global-set-key "\C-xl" 'goto-line)
This binds the lslot in ctl-x-mapto the function goto-lineglobally—that is, in all modes. Alternatively, you can use either of the following:
(define-key global-map "\C-xl" 'goto-line)
(define-key ctl-x-map "l" 'goto-line)
These commands have the same effect but aren't really any more efficient or better. And really, you shouldn't have to know that the keymap for C-xis called ctl-x-map. We'll stick to showing the global-set-keyapproach for the remaining examples, but remember that you have define-keyavailable for situations where setting the global key is not appropriate, such as when adding a mode-specific keystroke.
Other examples of key rebindings include binding C-x ?to help-commandand C-hto backward-char. These key rebindings are shown below:
(global-set-key "\C-x?" 'help-command)
(global-set-key "\C-h" 'backward-char)
Notice that these could also be done as
(define-key ctl-x-map "?" 'help-command)
(define-key global-map "\C-h" 'backward-char)
After you put a key binding (or any other code) in your .emacs file, you need to "run" (or evaluate) the file for the change to take effect. The command for this is M-x eval-current-buffer Enter. Even better, you could press C-x C-e, which (as we will see in the next chapter) causes only the single line of Lisp code that your cursor is on to run. If you don't do either of these, the changes won't take effect until the next time you invoke Emacs.
A more complicated keyboard customization task is binding commands to special keys, such as arrow, numeric keypad, or function keys, on your keyboard. This level of customization takes some work, but if you like using special keys, it is well worth the effort.
Most of the special keys have reasonable names, but using them with the set key functions discussed above requires using a slightly different syntax. The name of the key appears inside square brackets rather than inside double quotes. For example, you could bind the goto-linecommand to the function key F5 like this:
(global-set-key [f5] 'goto-line)
And you can certainly use modifiers with your special keys. Control-Alt-F5 can be bound like this:
(global-set-key [C-A-f5] 'goto-line)
Table 10-3lists the names of some common special keys.
Table 10-3. Special key ELisp names
ELisp Name |
Key |
DEL or backspace |
Backspace |
delete |
Delete key |
down |
Down arrow key |
end |
End key |
f1 .. f35 |
Function keys F1 through F35 |
home |
Home key |
help |
Help key |
kp-0 .. kp-9 |
Keypad numbers 0 through 9 |
kp-enter |
Enter key on the number pad |
left |
Left arrow key |
next |
Page Down |
prior |
Page Up |
right |
Right arrow key |
up |
Up arrow key |
10.4.2 Unsetting Key Bindings
You can also remove a particular key binding with the global-unset-keyand define-keycommands. For example, the following lines will both remove the goto-linecommand bindings from our previous examples:
(global-unset-key [f5])
(define-key ctl-x-map "l" nil)
Of course, you don't need to unset any bindings if you plan to replace them with something else. But this can be useful if you have a common "typo" key that you don't want firing off when you type it by mistake.
10.5 Setting Emacs Variables
Now we will get into ways to affect Emacs' behavior—not just its user interface. The easiest way to do so is by setting variables that control various things. We already saw examples of this like auto-save-intervalin Chapter 2. To set the value of a variable, use the setqfunction in your .emacs , as in:
(setq auto-save-interval 800)
Although auto-save-intervaltakes an integer (number) value, many Emacs variables take true or false values, called Boolean in computer parlance. In Emacs Lisp, tis the true value, and nilis the false value, although in most cases, anything other than nilis taken to mean true. Emacs variables can take other types of values, and here is how to specify them:
• Strings of characters are surrounded by double quotes. We saw examples of strings in the arguments to key binding commands earlier in this chapter.
• Characters are specified like strings but with a ?preceding them, and they are not surrounded by double quotes. Thus, ?xand ?\C-care character values xand C-c, respectively.
• Symbols are given by a single quote followed by a symbol name—for example, ' never(see the variable version-controlin Appendix A).
A list of useful Emacs variables, grouped by category, appears in Appendix A, with descriptions and default values. Emacs has more than 2,500 variables—many more than are covered in Appendix A. If there is something about Emacs that you want to customize, a variable probably controls the feature (especially if what you want to change involves a number or a true-or-false condition). To find out whether any variables relate to what you want to do, you can use the apropos-variablecommand described in Chapter 14 Chapter 14. The Help System Emacs has the most comprehensive help facility of any text editor—and one of the best such facilities of any program at all. In fact, the Emacs help facilities probably cut down the time it took for us to write this book by an order of magnitude, and they can help you immeasurably in your ongoing quest to learn more about Emacs. In this chapter, we describe Emacs help in the following areas: • The tutorial. • The help key ( C-h ) and Help menu, which allow you to get help on a wide variety of topics. • The help facilities of complex commands like query-replace and dired . • Navigating Emacs manuals and using the info documentation reader. • Completion , in which Emacs helps you finish typing names of functions, variables, filenames, and more. Completion not only saves you time and helps you complete names of functions you know about but can help you discover new commands and variables.
to look for variables and their descriptions.
Читать дальше