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?
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
-
-
@Vass Yes, you'll have to run the command
yank
(C-y
), then pressM-y
to cycle through. – nanny Mar 27 '15 at 14:58 -
-
1@Vass I don't understand what you mean.
C-y
runs the commandyank
, which is analogous to the concept of "pasting". Each time you kill text, or copy it withC-w
(kill-ring-save
), it gets pushed to thekill-ring
. When you hitC-y
, this pops the first element of thekill-ring
and inserts it into the buffer. Subsequent presses ofM-y
(kill-ring-pop
) will cycle through the elements saved in thekill-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
@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.
-
-
2No, you can use
M-w
(Alt-w, if you will) to copy. If you doC-h k M-w
you will see thatM-w
is bound tokill-ring-save
, and you will see what that command does - it copies the region to thekill-ring
. The bits of text that are in thekill-ring
are called "kills", whether they got there by killing text or copying text (e.g. usingM-w
). – Drew Mar 27 '15 at 15:04
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.
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
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 isbrowse-kill-ring.el
.Icicles -
M-y
: Yank from either thekill-ring
or thesecondary-selection-ring
, depending on the last yank type (you need librarysecond-sel.el
for the second alternative). If the last command was not a yank then this is the same asC-- 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 ringC-S-return
to copy a candidate to the other selection ring
C-y M-y
, repeatingM-y
as many times as you have to. – legoscia Mar 27 '15 at 14:50undo-tree
mode. – mankoff Mar 28 '15 at 19:38