Use Emacs registers
You know that you can move back to the mark, and you can move back to any position in the mark ring, using the techniques you learned in the last tutorial in this series (see Resources). But in addition, you can set arbitrary places in the buffer and move to them at any time through another Emacs facility: registers.
Emacs registers are general-purpose storage mechanisms that can store one of many things, including text, a rectangle, a position in a buffer, or some other value or setting. Every register has a label, which is a single character that you use to reference it. A register can be redefined, but it can contain only one thing at a time. Once you exit Emacs, all registers are cleared.
You can insert text that you've saved in a register. When you bring back a setting or configuration of a register, you're said to restore the register; if a register contains a position in a buffer that you wish to go back to, you're said to jump to the position saved in the register.
All Emacs register commands begin with
The following sections take you through the commands for setting, viewing, restoring, and jumping to registers.
To save the current point in a register, run the
point-to-register function, which is bound to
C-x r space, and give the register name, which can be
any alphanumeric character. Register names are case sensitive—x
and X refer to two different registers.
To copy the region to a register, use the
copy-to-register function, which is bound to
C-x r s.
To copy a rectangle to a register, use the
copy-rectangle-to-register function, bound to
C-x r r.
Try saving the current point to register X:
Start Emacs with the cursor on the ninth character of the second line of the practice file, which is the o in the word forests:
$ emacs +2:9 practice.b
C-x r space Xto save the point in register X.
view-register function to view the contents
of a register.
Try looking at the contents of register X:
M-x view-registerand press Enter.
When prompted in the minibuffer, type
Xfor the register to view. Register names are always single characters, so you don't have to press Enter.
When you run this function, a new window opens to show what register X
contains—in this case, a point position in your practice.b
C-x 1 to close the window.
To jump to a point you've saved in a register or to restore a window or frame
configuration, use the
which is bound to
C-x r j.
Try jumping to the point you've saved in register X:
M->to move to the end of the buffer—far away from the point you've saved.
C-x r j Xto jump to that saved point. You're back in the forests now.
Restoring text from a register, whether it's a region or a rectangle, is done
differently. To insert a region of text or a rectangle from a register, use the
insert-register function, which is bound to
C-x r i. It inserts the register's text at point and
keeps point before the insertion. If the register you give is a point position,
that literal position number (the number of characters into the buffer) is
inserted at point.
Click and drag
B1over the text and what art to mark these three words as the region.
Save the region to register q: Type
C-x r s q.
Move to the end of the buffer and insert a blank line: Type
M->and press Enter.
Insert the contents of the register you just saved: Type
C-x r i q. Notice that point is set to the beginning of the text you've inserted, not the end.
Insert the contents of register X that you saved earlier, which contained the location of point: Type
C-x r i X.
If you've been following all the examples in this tutorial, your Emacs session should look like Figure 4.
Figure 4. Restoring the contents of text and location registers
The number 38 that is restored indicates that the o in forests —the point location of register X—is the thirty-eighth character in the buffer. You can verify this by moving point 37 characters forward from the first position in the file:
M-< M-3 M-7 C-f
Table 3 lists the various commands and keys for using Emacs registers, giving their function names and describing their meanings.
Table 3. Emacs commands for using registers
||Save point to register X.|
||Save the region to register X.|
||Save the selected rectangle to register X.|
||View the contents of a given register.|
||Move point to the location given in register X.|
||Insert the contents of register X at point.|