Skip to content
Tutorials Center
My learning

Topics

Lesson 10 of 38 ยท Editing and Buffers

The Kill Ring and Yanking

Understand Emacs cut and paste: killing versus deleting, the kill ring, C-y and M-y, appending kills, the clipboard connection and the commands that kill text.

Emacs does not call it cut and paste. Text is killed (removed and saved) and yanked (inserted back). The saved text goes into the kill ring, a list of your recent kills. Because the ring remembers many entries, you can paste something you cut ten minutes and five edits ago. This is one of the pieces of Emacs that people miss when they leave it.

Kill versus delete

  • Killing removes text and saves it in the kill ring. It is what you use for cutting and moving text.
  • Deleting removes text without saving it. Single characters (C-d, DEL) are deleted; larger pieces are killed.

The rule of thumb: commands that remove more than a character kill.

The main kill commands

Keys Command Kills
C-k kill-line From point to the end of the line (a second C-k removes the newline)
C-S-backspace kill-whole-line The whole line including its newline
M-d kill-word The word after point
M-DEL backward-kill-word The word before point
M-k kill-sentence To the end of the sentence
C-M-k kill-sexp The next balanced expression
M-z zap-to-char Everything up to and including the next occurrence of a character you type
C-w kill-region The selected region
M-w kill-ring-save Copies the region without removing it

M-w is the equivalent of Copy: it saves the region in the kill ring and leaves the text.

Yanking

Keys Command Effect
C-y yank Insert the most recent kill at point
M-y yank-pop Immediately after C-y, replace the yanked text with the previous kill, and again for the next one
C-u C-y Yank and leave point before the text

Picture the ring as a list with a pointer. A new kill goes on the front. C-y pastes the front entry. If it is not the text you wanted, press M-y and the pasted text is replaced by the next older entry; keep pressing to walk through the ring. In recent Emacs versions, M-y without a preceding C-y shows a menu (yank-from-kill-ring) of the ring's contents to choose from.

JS Stand-in: a kill ring

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Consecutive kills append

If you kill twice in a row with no other command between, the two pieces join into one ring entry. Pressing C-k C-k or M-d M-d M-d and then one C-y gives back all the text at once. Killing backward prepends. That is why C-k pressed twice on a line removes both the text and the newline in a single entry. To append a kill to the previous one on purpose even when you did something else in between, press C-M-w first and then kill.

JS Stand-in: consecutive kills append

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Selecting text first: the region

Many kills act on the region, the text between the mark and point. Set the mark with C-SPC, move the cursor, then press C-w or M-w. The next lesson (mark and region) covers selection in detail. Note that with Transient Mark mode (the default) the region is highlighted.

The clipboard

Emacs has its own kill ring, separate from the system clipboard, but they are linked in graphical Emacs. Since Emacs 24, select-enable-clipboard is on by default: killing in Emacs puts the text on the system clipboard (C-w, M-w copy to it) and C-y yanks from it if something else was copied (the ring keeps the previous clipboard contents too). The mouse's middle button pastes the primary selection, a separate X11 convention. In a terminal-only Emacs, the link to the clipboard can be missing; there are small packages for that, such as xclip or clipetty.

Many Emacs users also like CUA mode (M-x cua-mode), which makes C-c, C-x, C-v act as copy, cut, paste when a region is active. Use it if you come from other editors, but know that it fights with the standard C-x and C-c prefixes unless you keep its prefix-delay behavior.

Practical patterns

  • Move a line: C-a C-k C-k (kills text and newline), go to the new spot and C-y.
  • Duplicate a line: C-a C-k C-k C-y C-y (kill, then yank twice). Emacs 29 added a command duplicate-line (and duplicate-dwim); check C-h f duplicate-line.
  • Swap two things: C-t transposes characters, M-t words, C-x C-t lines, C-M-t expressions.
  • Delete a word and keep the previous kill: use C-d for single characters. Delete a larger text without touching the ring by selecting it and running M-x delete-region.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the difference between kill and delete in Emacs?

    Killed text can be yanked back; deleted text is gone, aside from undo.
  2. 2. Which key copies the region without removing it?

    M-w runs kill-ring-save, the equivalent of Copy.
  3. 3. You pressed C-y and the wrong text appeared. What next?

    M-y immediately after a yank cycles through older kill ring entries.
  4. 4. What happens when you press M-d three times in a row, then C-y?

    Consecutive kills append to one ring entry.

Summary

  • Kill saves text to the kill ring; yank (C-y) inserts the newest entry, M-y cycles through older ones.
  • C-k, M-d, M-DEL, C-w kill; M-w copies.
  • Consecutive kills combine into one entry; C-M-w forces appending.
  • In graphical Emacs the kill ring is connected to the system clipboard.

Next: The Mark, the Region and Selection