Emacs key sequence bindings
Emacs key (really key sequence) bindings are covered in the Customization section (section 50!) and there is a subsection called Customizing Key Bindings.
Some terminology:
- command
- "a Lisp function whose definition provides for interactive use"
- key bindings
- map key sequences to commands
- keymaps
- data structures that record key bindings; there are many
- key sequence
- (key for short) is a sequence of input events that have a meaning as a unit. The key sequence gets it meaning from its binding
There are a small number of key sequences reserved for user-defined bindings:
The reserved key sequences are those consisting of
C-c
followed by a letter (either upper or lower case), and function keys F5 through F9 without modifiers.
The global keymap is always in effect. To see what they are, type
C-h b
. (The buffer is 2056 lines long.) Just browsing I see that the
command goto-line
is bound to M-g M-g
. A command I have
accidentally been invoking is text-scale-adjust
via C-x C-+
and
C-x C--
.
(As an aside, I see that the "self-inserting" upside down exclamation
mark is bound to A-!
where A
stands for Alt
which is not the
same as the buttons on standard keyboards that say "Alt". From the
Modifier Keys section of the manual I learned that to invoke Alt
I
need to do C-x @ a !
. Here's what I get: ¡. I didn't see any key
bindings using the hyper key, H-
.
Major and minor modes have local keymaps.
[W]henever a minor mode is in effect, the definitions in its keymap override both the major mode’s local keymap and the global keymap. In addition, portions of text in the buffer can specify their own keymaps, which override all other keymaps.
Also this:
A local keymap can redefine a key [sequence] as a prefix key by defining it as a prefix keymap. If the key [sequence] is also defined globally as a prefix, its local and global definitions (both keymaps) effectively combine: both definitions are used to look up the event that follows the prefix key. For example, if a local keymap defines
C-c
as a prefix keymap, and that keymap definesC-z
as a command, this provides a local meaning forC-c C-z
. This does not affect other sequences that start withC-c
; if those sequences don’t have their own local bindings, their global bindings remain in effect.
And:
Another way to think of this is that Emacs handles a multi-event key sequence by looking in several keymaps, one by one, for a binding of the whole key sequence. First it checks the minor mode keymaps for minor modes that are enabled, then it checks the major mode’s keymap, and then it checks the global keymap. This is not precisely how key lookup works, but it’s good enough for understanding the results in ordinary circumstances.
If I want to bind a key sequence to a command globally, I would do this:
(keymap-global-set "C-c n" #'display-line-numbers-mode)
But I think keymap-global-set
is for newer Emacs than my (at the time) current version 27.1
So I will do this:
(global-set-key (kbd "C-c n") #'display-line-numbers-mode)
This is one that saves my wrist:
(global-set-key (kbd "S-SPC") 'delete-backward-char)
Here are a weird ones to consider:
(global-set-key (kbd "S-<home>") #'whitespace-mode) (global-set-key (kbd "S-<f1>") (lambda () (interactive) (message "hello")))
Here I use a capital "T".
(global-set-key (kbd "C-c T") (lambda () (interactive) (message "hello")))
Here I use the Microsoft Windows key which Emacs knows as Super:
(global-set-key (kbd "s-i") #'ibuffer)
So if I want a set of personal key sequences to work with, I have C-c
[A-z]
, F5 through F9, shifted versions of F1 though F12, and if I
have one, or can make one, Super plus anything.