EMACS: Marking and Kill Ring

Selecting a Region

Text is selected by setting a mark to start the area being selected, called a region by EMACS, then navigating to the other end of the selection.

Keystroke Function
CTRL-Space Sets the mark. This is the start of the region. Use the movement keystrokes (like arrows) to define the end of the region.
Meta-h Marks the paragraph
CTRL-x h Mark the buffer

Kill Ring

The kill ring would be known as a buffer or clipboard in other applications, or the OS system-wide clipboard. A key difference is it will hold up to 30 items.

Keystoke Function
CTRL-w Kill region–removes text from buffer and places it in the kill ring. A “cut” in contemporary language.
Meta-w Copy region, leaving the selected region in place. A “copy” in contemporary language.
CTRL-y Yank–inserts the most recent text put into the kill ring at the current location of the cursor.
Meta-y immediately after a yank Yank-Pop–Yanks further down the kill ring, replacing the text previously inserted.

Yanking Text from the Kill Ring

Suppose you copy three lines of text separately into the kill ring, in this order:

One
Two
Three

(This is to say you mark One and then copy it, then mark Two and copy it, and finally Three (as opposed to selecting all as one region).)

Three is now in the top (“most recent”) slot in the kill ring, followed by Two and One.

I want to insert from the kill ring into this sentence:

This is item number in the kill ring.

Note there are two spaces between “number” and “in.” If the cursor were placed on the second of those two spaces, any text inserted (typed or yanked) would go between the spaces. If the yank command were invoked (CTRL-y), you would get:

This is item number Three in the kill ring.

If I then invoke yank-pop (Meta-y), I would get:

This is item number Two in the kill ring.

(The sequence would be CTRL-y Meta-y)

yank-pop again, and I get:

This is item number One in the kill ring.

This can continue up to thirty times.

Note that each time yank-pop is invoked, the most recent thing yanked becomes the most top item kill ring–everything before that is removed from the kill ring. To get to One in the example above, I hit CTRL-y Meta-y Meta-y. If I type some more and do a yank (CTRL-y) command, I get “One” inserted. If I were to do CTRL-y Meta-y, it would provide whatever was put in the kill ring before “One.” This can be overridden by setting an argument before the yank command (so, Meta-0 CTRL-y would take you back to the top item in the kill-ring, "Three").

Confusing enough?

In practice, I usually am only pasting the most recent thing from the kill ring, similar to the system clipboard. Occasionally, going back one happens, but it doesn’t help alternating between text (saying I want to put <tag> and </tag> on either side of a bunch of things).

Interaction Between the System Clipboard and Kill Ring

Anything copied from another application to the system clipboard becomes the most recent thing in the kill ring.

The last thing copied to the kill ring also is copied to the system clipboard. Any changes due to yank-pop are not reflected. In the example above, even though the series of yank-pops made the top item in the kill ring “One”, the system clipboard will have “Three”, as that was the last thing copied to the kill ring.

Return to Reference Index

Created by I. Charles Barilleaux
Last Update: 2021-04-02