views:

190

answers:

5

Unlike windows style self explanatory copy/cut/paste commands, I could not understand ring concept in emacs.

Since I don't program very often in emacs, I could have not realized the value of ring feature. Can you tell me what is called ring in emacs and how to use it?

+4  A: 

A ring is a circular buffer. Think of a rolodex.

If you are killing/yanking to a ring, then you can walk forward and backwards in your history.

Paul Nathan
+2  A: 

From the Emacs manual: "A ring is a fixed-size data structure that supports insertion, deletion, rotation, and modulo-indexed reference and traversal." In other words, it's a circular queue.

larsmans
+5  A: 

Every time you copy or cut something to the clipboard in windows, you lose whatever was on your clipboard before. (Though some programs will store previous clipboard contents for you) The emacs "ring" will store old clipboard contents even after you copy/yank/cut/kill things. It can be handy if you get used to it because it lets you store more than one thing in the clipboard at once, and reduces the chances of accidentally overwriting something that you cut to the clipboard like you could in Windows.

Peter Recore
+11  A: 

Well, let me try in simple words. Each time you copy (M-w) or cut (C-w), the selection is inserted into a top of so called ring (which is just like a closed ended list).

The interesting part comes after. Now, if you paste with C-y then the most recently pasted region (which is now the front element of the ring) is inserted in the buffer. If you continue pressing M-y, then the text is replaced successively with older and older elements from the ring, so to say, the ring is rotated and the older text is designated as front element each time. This gives you an access to older cut/copied text.

The useful part does not end here. If you cut/copy some other text, it will be inserted at the top of the ring, and the ring will be rotated again such that the top is now the front. And you can start the C-y M-y sequence again, with the newly inserted text designated as front.

So, to conclude:

  • the top of the ring is the place where the newly copied/cut (M-w/C-w) text is inserted. When that happens, the top element becomes the front element.
  • the front element of the ring is the place on which paste (aka yank) commands C-y and M-y operate, by inserting it into the buffer. M-y also rotates the ring, such that older text in the ring becomes the front.

You can visualize it by imagining the circle with the front fixed at 12 hours position, but not part of the ring. The top is part of the ring instead, so when some command rotates the ring the top is also rotated.

alt text

VitoshKa
+2  A: 

On Ubuntu, with the emacs-goodies-el package installed:

Press C-( M-x browse-kill-ring RET C-)

This defines a temporary keyboard macro which calls browse-kill-ring when you press C-x e.

Now you don't have to just read about the kill ring, you can actually see what's in it.

Experiment with C-w (cut), M-w (copy), C-y (yank) and C-y M-y (yank next in ring), and press C-x e to see the effect on the kill ring.

unutbu
Is `browse-kill-ring` a standard library function? I cannot find it.
Török Gábor
@Török Gábor: Oops, I didn't realize it is not a standard library function. It is provided by the emacs-goodies-el package on Ubuntu. The code can be found here: http://www.fan.gr.jp/~ring/doc/misc/browse-kill-ring.el
unutbu