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?
5 Answers
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-yit says previous command was not a yankVass– Vass2015-03-27 14:52:35 +00:00Commented Mar 27, 2015 at 14:52 - @Vass Yes, you'll have to run the command
yank(C-y), then pressM-yto cycle through.nanny– nanny2015-03-27 14:58:44 +00:00Commented Mar 27, 2015 at 14:58 - I select text and then press C-y and it gets pasted on the spotVass– Vass2015-03-27 15:02:58 +00:00Commented Mar 27, 2015 at 15:02
- 1@Vass I don't understand what you mean.
C-yruns 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-ringand inserts it into the buffer. Subsequent presses ofM-y(kill-ring-pop) will cycle through the elements saved in thekill-ring.nanny– nanny2015-03-27 15:08:01 +00:00Commented Mar 27, 2015 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– nanny2015-03-27 15:51:31 +00:00Commented Mar 27, 2015 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.
- so I don't use Alt-w to copy? I have to kill it?Vass– Vass2015-03-27 14:58:31 +00:00Commented Mar 27, 2015 at 14:58
- 2No, you can use
M-w(Alt-w, if you will) to copy. If you doC-h k M-wyou will see thatM-wis 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-ringare called "kills", whether they got there by killing text or copying text (e.g. usingM-w).Drew– Drew2015-03-27 15:04:31 +00:00Commented Mar 27, 2015 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-ringto 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-ringor thesecondary-selection-ring, depending on the last yank type (you need librarysecond-sel.elfor 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-ywith 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-deleteto remove a candidate entry from the selection ringC-S-returnto copy a candidate to the other selection ring
C-y M-y, repeatingM-yas many times as you have to.undo-treemode.