Next: Keymaps, Previous: Indent Tabs Mode, Up: Your .emacs File [Contents][Index]
Now for some personal key bindings:
;;; Compare windows (keymap-global-set "C-c w" '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 key setting command is keymap-global-set
. 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. (If you were binding a META key, 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: (keymap-global-set "C-c o" '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' (keymap-global-unset "C-x f")
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' (keymap-global-set "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.
Historically, keys are bound globally using a lower-level function,
global-set-key
, which is now considered legacy. While you are
encouraged to use keymap-global-set
, you likely would encounter
global-set-key
in various places. The first example in this
section can be rewritten using global-set-key
as:
(global-set-key "\C-cw" 'compare-windows)
It is very similar to keymap-global-set
, with the keybinding
following a slightly different format. Control-C is represented by
\C-c
, instead of C-c
. There is no space between key
strokes, like \C-c
and w
in this example. Despite the
difference, in documentation, this is still written as C-c w
for readability.
Historically, keys are unbound globally using a lower-function,
global-unset-key
, which is now considered legacy. Its key
binding format follows that of global-set-key
. The key unbinding
example in this section can be rewritten as:
;;; Unbind 'C-x f' (global-unset-key "\C-xf")
Next: Keymaps, Previous: Indent Tabs Mode, Up: Your .emacs File [Contents][Index]