Other examples of key rebindings include binding C-x ? to help-command and C-h to 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.
10.4.1 Special Keys
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-line command 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-3 lists 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-key and define-key commands. For example, the following lines will both remove the goto-line command 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-interval in Chapter 2. To set the value of a variable, use the setq function in your .emacs, as in:
(setq auto-save-interval 800)
Although auto-save-interval takes an integer (number) value, many Emacs variables take true or false values, called Boolean in computer parlance. In Emacs Lisp, t is the true value, and nil is the false value, although in most cases, anything other than nil is 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, ?x and ?\C-c are character values x and C-c, respectively.
• Symbols are given by a single quote followed by a symbol name—for example, 'never (see the variable version-control in 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-variable command described in Chapter 14 to look for variables and their descriptions.
Several Emacs variables can have different values for each buffer (local values, in Emacs parlance) as well as a default value. Such variables assume their default values in buffers where the local values are not specified. A common example is starting a new text document. The local value for the left-margin variable has not been set, so Emacs uses the default value for left-margin. You can change the local value in this buffer if you like. But start a new document in a new buffer and you'll find that left-margin is back to the default value—because the second buffer's local value has not been set.