Now for some personal key bindings:
;;; Compare windows (global-set-key "\C-cw" 'compare-windows)
compare-windows
is a nifty command that compares the text in
your current window with text in the next window. It makes the
comparison by starting at point in each window, moving over text in
each window as far as they match. I use this command all the time.
This also shows how to set a key globally, for all modes.
The command is global-set-key
. It is followed by the
key binding. In a .emacs file, the keybinding is written as
shown: \C-c
stands for Control-C, which means to press the
control key and the c key at the same time. The w
means
to press the w key. The key binding is surrounded by double
quotation marks. In documentation, you would write this as
C-c w. (If you were binding a META key, such as
M-c, rather than a CTRL key, you would write
\M-c
in your .emacs file. See Rebinding Keys in Your Init File in The GNU Emacs Manual, for
details.)
The command invoked by the keys is compare-windows
. Note that
compare-windows
is preceded by a single-quote; otherwise, Emacs
would first try to evaluate the symbol to determine its value.
These three things, the double quotation marks, the backslash before the ‘C’, and the single-quote are necessary parts of key binding that I tend to forget. Fortunately, I have come to remember that I should look at my existing .emacs file, and adapt what is there.
As for the key binding itself: C-c w. This combines the prefix key, C-c, with a single character, in this case, w. This set of keys, C-c followed by a single character, is strictly reserved for individuals’ own use. (I call these own keys, since these are for my own use.) You should always be able to create such a key binding for your own use without stomping on someone else’s key binding. If you ever write an extension to Emacs, please avoid taking any of these keys for public use. Create a key like C-c C-w instead. Otherwise, we will run out of own keys.
Here is another key binding, with a comment:
;;; Key binding for 'occur' ; I use occur a lot, so let's bind it to a key: (global-set-key "\C-co" 'occur)
The occur
command shows all the lines in the current buffer
that contain a match for a regular expression. When the region is
active, occur
restricts matches to such region. Otherwise it
uses the entire buffer.
Matching lines are shown in a buffer called *Occur*.
That buffer serves as a menu to jump to occurrences.
Here is how to unbind a key, so it does not work:
;;; Unbind 'C-x f' (global-unset-key "\C-xf")
There is a reason for this unbinding: I found I inadvertently typed C-x f when I meant to type C-x C-f. Rather than find a file, as I intended, I accidentally set the width for filled text, almost always to a width I did not want. Since I hardly ever reset my default width, I simply unbound the key.
The following rebinds an existing key:
;;; Rebind 'C-x C-b' for 'buffer-menu' (global-set-key "\C-x\C-b" 'buffer-menu)
By default, C-x C-b runs the
list-buffers
command. This command lists
your buffers in another window. Since I
almost always want to do something in that
window, I prefer the buffer-menu
command, which not only lists the buffers,
but moves point into that window.