Search code examples
emacselispcircular-buffer

what is called ring in emacs?


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?


Solution

  • 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