navigating the kill-ring

The kill-ring is emacs' implementation of a copy-paste-clipboard. As expected, it's more powerful than what most other editors offer - but at the same time, it may be a bit hard to use. We already discussed the kill-ring in the Emacs-Fu prehistory.

One of the more powerful features of the emacs kill-ring is that is allows for multiple entries to be saved there. You can then retrieve those older entries by using prefix arguments, that is: C-y will retrieve the last item from the kill-ring, while M- n C-y will retrieve the n th last stretch of 'killed' (cut/copied) text. For example, M-2 C-y will retrieve the second last one.

Unfortunately, for most people it's quite hard to remember what was 'killed' and when and in what order… Those people can of course use the menu (Edit/Paste from kill menu), but that is not always so convenient, requires mousing around etc.

Edit: As Anynomous mentions in the comments, one can of course use M-y to circle through the candidates. This is quite useful, esp. when you have only a few items in the ring. Note, this command only works just after a 'yank' (C-y).


Instead, using the handy browse-kill-ring extension, you can open a buffer which lists the contents of the kill ring, and you can move your cursor to the desired item and insert it.

Installation in simple; first get the browse-kill-ring package from EmacsWiki, or, alternatively, Debian/Ubuntu users can install the emacs-goodies-el-package.

Then, add to your .emacs something like:

(when (require 'browse-kill-ring nil 'noerror)

Now, the M-y key binding will activate browse-kill-ring iff the normal behavior (see above) is not available, i.e., when the last command was not a 'yank'. You can also edit the kill-ring (press C-h m when in the browse-kill-ring-buffer to see the available bindings).

a little pop-up menu

While browsing EmacsWiki, I found another trick:

(global-set-key "\C-cy" '(lambda ()
   (popup-menu 'yank-menu)))

After which C-c y will show a little pop-up menu with the your kill-menu entries. It does not seem to fully synchronize with the (possibly edited) entries you get from browse-kill-ring, but it's a pretty neat way to navigate through your kill-ring-buffers – if you don't have too many of them (if so, you could customize the kill-ring-max variable).


Anonymous said...

M-x anything-show-kill-ring after installing anything.el via M-x auto-install-batch anything via http://www.emacswiki.org/cgi-bin/wiki/download/auto-install.el

Anonymous said...

I find it easier to use the combo C-y/M-y to navigate the kill ring...

djcb said...

@Anonymous(ii): yes, that is useful as well, forgot to mention that one. Added it now, thanks.

Avdi Grimm said...

I've used browse-kill-ring.el for years, but this article prompted me to revisit my one annoyance with the extension: the fact that it destroys the existing window configuration when used. It didn't take much investigation to turn up the customization setting browse-kill-ring-quit-action. It turns out that setting this to 'save-and-restore does exactly what I want, which is to reinstate the previous window configuration after selecting a kill ring entry.

djcb said...

@Avdi Grimm: great! I always try to write down all such small annoyances, and
when I have time, do something about them. Emacs usually offers some way to
make things just the way I like them...

ozan said...

Kill ring search is pretty useful too:

Drew said...

browse-kill-ring+.el extends browse-kill-ring.el. It lets you use the `kill-ring' but also an alternative ring, which by default is a ring of your secondary selections.


Icicles offers an alternative: completion vs browsing (of selections from either ring).