Emacs Delete vs. Kill
One thing that has always vexed me is the Emacs kill ring. It’s great that it makes a list of things you’ve killed, but often I just want delete things quickly without saving it. So I need to change the default settings to enable both killing and deleting words and lines.
Basic nomenclature (for this blog post at least);
- kill means removing text and putting it in the kill ring
- delete means removing text and not putting it in the kill ring
Understanding Key Bindings
The default behavior of Emacs can depend on the underlying operating system. This is because key presses are first translated by the OS, then passed (if they don’t trigger an OS function) to the application, e.g., Emacs. This can be further complicated by the keyboard setup and, further still, by an Emacs instance running inside of a Terminal.
To figure out the name that Emacs itself has for each key, use
C-h k KEY or
M-x describe-key KEY.
This internal name will come in handy when rebinding these keys.
For extended keyboards, there are often two “delete” keys, one which performs a backspace and one which does a forward delete.
The forward delete is often found on the “keypad” or number pad and is thus called
kp-delete by Emacs.
Mac keyboards have the following modifier keys: Shift, Control, Option (Alt), and Command.
While Shift and Control are already mapped for Emacs (known as
C respectively), the Option and Command keys are not mapped.
I use Option/Alt as a meta (
M) key and Command as a super (
By default, the
delete key removes a single letter (backwards).
In Emacs keybindings, this is sometimes denoted as
backspace (in addition to sometimes being called
ALT-delete on a Mac keyboard) will remove a whole word, but place it in the kill ring.
Killing a line is simple:
C-k, but this kills a line forwards.
Putting the most recently killed word or line back is
C-y, called yanking.
Yanking previous items in the ring is achieved with
M-y, which will cycle through the ring.
There is a 3 dimensional problem to solve here, with 16 possible combinations (14 unique operations):
- Removal direction (backwards or forwards)
- Removal type (kill or delete)
- Removal size (character, word, visual line, whole line)
I use the Mac internal for marking the direction as backwards or forwards.
Even on laptop keyboards, I can use
Fn-delete to get the forwards deletion.
The whole line removal does not have a direction, but must be bound to both
The un-modified delete key-press should trigger a character delete.
Using the meta (
M) modifier triggers a word removal.
Using the control (
C) modifier triggers a line removal.
Saving Items to the Kill Ring
I use the Shift (
S) modifier to explicitly trigger adding the removed text to the kill ring.
Binding the Keys
The following table tracks all the combinations which must be implemented. The implementation looks like:
The functions marked with a * must be implemented.
I am very happy with how cleanly these keybindings map to functionality. Let’s see if I can actually put these to use!
Comments are sent directly to me and may or may not be posted for display on this page.