6

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?

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • 1
    I don't think this question is off-topic here. It is a computer technology question on how clipboard is implemented in Emacs. – viam0Zah Oct 16 '10 at 11:40
  • 4
    Whenever somebody says some piece of computer technology is "self explanatory", I think that really just means they've learned it so long ago they forgot about the time when they didn't understand it. :-) – Ken Oct 16 '10 at 14:25
  • 1
    the emacs ring is not a simple clipboard, and certainly not self explanatory. as usual, when emacs does something, it does it in a super featureful, complex to understand way that is super powerful to the few users for whom investing the time to learn it is worth it. – Peter Recore Oct 30 '10 at 15:07
  • Further ring functions, FYI: – Drew Aug 20 '11 at 21:37
  • My mom doesn't find cut/copy/paste self-explanatory. – Mirzhan Irkegulov Aug 03 '14 at 19:09

5 Answers5

12

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
  • 8,387
  • 3
  • 35
  • 59
4

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
  • 14,037
  • 4
  • 42
  • 62
3

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
  • 39,638
  • 28
  • 112
  • 212
2

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
  • 842,883
  • 184
  • 1,785
  • 1,677
  • Is `browse-kill-ring` a standard library function? I cannot find it. – viam0Zah Oct 30 '10 at 16:56
  • @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 Oct 30 '10 at 17:06
1

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.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836