2009-01-07

using registers

UPDATED Emacs is full of wonderful features, but sometimes it takes some time to find them. Today, let's discuss one such feature, registers. Registers are dicussed in the Emacs Manual, but it took me quite some time before I understood what they're good for. So let me discuss them here - maybe I am not the only one.

To explain the use of register, let's look at the normal cut-copy-pasting of text first. When you have cut or copied some text, it lives in a place we call the clipboard, from with you can then paste it. But in most programs, if you copy/cut text again, it replaces what was already on the clip board.

Now, what about registers? In emacs, we have a special clipboard with multiple places to store things, each named by a single number or letter. We call these places registers. Thus, you can save some text to register A, some other text to register B, and later paste the contents of register A or B. The key bindings (shortcuts) for this are good to remember:

C-x r s Rsave region (selection) into register R
C-x r i Rinsert the contents of register R
So, to save the current region/selection in register 2, you would type: C-x r s 2, and to insert the contents of that register later, you'd do C-x r i 2. It's a really useful thing to add to your emacs muscle memory.

(Note: the clipboard that emacs uses for 'normal' cut/copy/paste, the 'kill-ring', allows for multiple (but unnamed) entries as well - but we'll discuss the kill-ring in some other entry.)

viewing register contents

One obvious problem with registers is that for most people it's very hard to remember what went into which register, if you use more than two or three registers. There is M-x view-register, but that's only marginally useful. It would be much nicer if we could get a list of all registers in use and a preview of their contents. To do that, we can use the list-register.el package (see installing packages). The package adds a function list-registers (and some others). I use a key binding C-x r v for that, which somewhat logically follows the other ones:
C-x r vview registers
(require 'list-register)
(global-set-key (kbd "C-x r v") 'list-register)
An alternative would be to use C-x r l (for list registers), but that one has already been taken by bookmark-bmenu-list, which shows a list of your bookmarks -- to be discussed some other time).

I would vote for including the list-registers functionality in emacs. Having registers without a way to view them, makes them much less useful.

more than words

Personally, I seldomly use registers for anything but text; however, you can store other things in registers as well (see the Emacs Manual registers section for details):
objectstoreretrievenotes
rectangleC-x r r RC-x r i R save rectangle into register R (see working with rectangular selections, and insert it);
buffer/positionC-x r <SPC> RC-x r j Rsave buffer/position in register R, and jump back to it
windowC-x r w RC-x r j Rsave window configuration in register R, and jump back to it. Note that what emacs calls a window is called a window pane elsewhere, see emacs terminology)
frameC-x r f RC-x r j Rsave frame configuration in register R, and jump back to it. Note that what emacs calls a frame is called a window elsewhere, see emacs terminology
As you can see, some of the objects share the keybinding for retrieving them. In other words, what happens when you retrieve register R depends on the type of object you put in there before.

While registers are quite useful, I think they would be easier to use if they were integrated with the normal cut-copy-paste (the 'kill-ring'). Another issue is that you cannot access your registers from other programs. Actually, recent MS-Office versions do this in a bit nicer way...

8 comments:

Brad said...

"Another issue is that you cannot access your registers from other programs."

What do you mean "other programs"? :)

susja said...

hi djcb, I like your site very much, It's very helpful for me.
For some reason this function didn't work for me :
(global-set-key (kbd "C-x r v") 'list-registers)

I copied package list-register.el into ...xemacs-packages\lisp\list-registers
directory and now I'm able to list registers by M-x list-register but when I do "C-x r v" it failed with the error:
Debugger entered--Lisp error: (void-function list-registers)
Where could be the problem?
Thanks

djcb said...

@susja: ah; you should put
'list-register
instead of
'list-registers

susja said...

thanks, it works !!

Ganesh said...

hi,

how to insert content of say 'R' register into the clipboard ?

so that the 'other' programs can also make use of the register content outside emacs ( ex: a document writer etc.,)

vvoody said...

Hi, found a broken link:
http://emacs-fu.blogspot.com/2000/12/installing-packages.html

And I would like to read that post ;-)

djcb said...

@vvoody: fixed, thanks.

Evgeniy Dolzhenko said...

Same sentiment about wanting to view register contents, +1! The provided function doesn't handle newlines in register contents very nicely, but is still useful (as is your blog in general), thanks!