7

Once I have selected some text I use Alt-w to copy the region and then use ctrl-y to paste it. Is there a way to paste something I have selected and copied with Alt-w prior to my latest paste?

  • 1
    Try C-y M-y, repeating M-y as many times as you have to. – legoscia Mar 27 '15 at 14:50
  • See also undo-tree mode. – mankoff Mar 28 '15 at 19:38
5

After yanking with C-y, press M-y to run the command yank-pop. Repeated presses will cycle through the kill-ring.

For example, if you have:

Hello, world!

and you kill Hello and world (you have , ! left), then go to the next line and hit:

C-y world

M-y Hello

SPCC-y Hello Hello

C--M-y Hello world

  • when I press M-y it says previous command was not a yank – Vass Mar 27 '15 at 14:52
  • @Vass Yes, you'll have to run the command yank (C-y), then press M-y to cycle through. – nanny Mar 27 '15 at 14:58
  • I select text and then press C-y and it gets pasted on the spot – Vass Mar 27 '15 at 15:02
  • 1
    @Vass I don't understand what you mean. C-y runs the command yank, which is analogous to the concept of "pasting". Each time you kill text, or copy it with C-w (kill-ring-save), it gets pushed to the kill-ring. When you hit C-y, this pops the first element of the kill-ring and inserts it into the buffer. Subsequent presses of M-y (kill-ring-pop) will cycle through the elements saved in the kill-ring. – nanny Mar 27 '15 at 15:08
  • 1
    @Vass, oops, that is a typo on my part. It should have read M-w. The command is right, though. – nanny Mar 27 '15 at 15:51
3

@nanny has given you the answer. And if M-y says that the previous command was not a yank then you probably forgot to do C-y first. IOW, you first do C-y to yank the latest kill, which is not what you want. Then you do M-y to get the previous one instead. You can repeat M-y to cycle backward.

See the Emacs manual, node Yanking for general information about copying and pasting, and the kill-ring.


Anyway, I wanted to mention the secondary selection. In particular, it can be very handy in situations where you might otherwise want to use C-y M-y.

The secondary selection is just what it says: a second selection, unrelated to the selection that is the region in Emacs. It stays available regardless of whether the region is active, and its position is not affected by point or mark.

If you use library second-sel.el then you get many of the advantages of the kill-ring (which is for the region) for the secondary selection as well. It adds the secondary selection to a second selection ring, secondary-selection-ring, which is similar to, but separate from, the kill-ring. It also prevents the secondary selection from being added to the kill-ring, so you now have two separate selection-history rings.

M-y then cycles items from the appropriate ring, depending on whether it follows a yank of the primary (region) or pasting of the secondary selection.

  • so I don't use Alt-w to copy? I have to kill it? – Vass Mar 27 '15 at 14:58
  • 2
    No, you can use M-w (Alt-w, if you will) to copy. If you do C-h k M-w you will see that M-w is bound to kill-ring-save, and you will see what that command does - it copies the region to the kill-ring. The bits of text that are in the kill-ring are called "kills", whether they got there by killing text or copying text (e.g. using M-w). – Drew Mar 27 '15 at 15:04
2

The best solution I've found is helm-show-kill-ring. I used Helm for quite a while before I even realized this existed. I have it bound to M-y. It makes it very fast and easy to see existing entries, narrow them down, and choose the one you want.

1

After you paste, M-y will replace the pasted text with previously copied text.

If you press M-y multiple times then you can cycle through all your previously copied (or cut/killed) text

0

If you want to easily see previously pieces of copied and killed text, and choose one or more such pieces, these can help:

  • Browse Kill Ring - Browse the kill-ring to choose a kill to yank. That page shows several alternative libraries that offer this behavior in different ways. The main one is browse-kill-ring.el.

  • Icicles -

    • M-y: Yank from either the kill-ring or the secondary-selection-ring, depending on the last yank type (you need library second-sel.el for the second alternative). If the last command was not a yank then this is the same as C-- C-y (see next).
    • C-- C-y (C-y with a negative prefix arg): Lets you choose any number of kills to insert using completion. During completion you can use:

      • C-, to sort the candidates to yank in different ways (repeat)
      • S-delete to remove a candidate entry from the selection ring
      • C-S-return to copy a candidate to the other selection ring
  • Helm - See @bluejay's answer on this page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.