Registers
*********

Emacs registers are compartments where you can save text, rectangles, positions, and other things for later use.  Once you save text or a rectangle in a register, you can copy it into the buffer once, or many times; you can move point to a position saved in a register once, or many times.

Each register has a name, which consists of a single character.  A register can store a number, a piece of text, a rectangle, a position, a window configuration, or a file name, but only one thing at any given time.  Whatever you store in a register remains there until you store something else in that register.  To see what a register R contains, use M-x view-register.

M-x view-register RET R
     Display a description of what register R contains.

Bookmarks record files and positions in them, so you can return to those positions when you look at the file again. Bookmarks are similar enough in spirit to registers that they seem to belong in this chapter.

Saving Positions in Registers
=============================

Saving a position records a place in a buffer so that you can move back there later.  Moving to a saved position switches to that buffer and moves point to that place in it.

C-x r SPC R
     Save position of point in register R (point-to-register).

C-x r j R
     Jump to the position saved in register R (jump-to-register).

To save the current position of point in a register, choose a name R and type C-x r SPC R.  The register R retains the position thus saved until you store something else in that register.

The command C-x r j R moves point to the position recorded in register R.  The register is not affected; it continues to hold the same position.  You can jump to the saved position any number of times.

If you use C-x r j to go to a saved position, but the buffer it was saved from has been killed, C-x r j tries to create the buffer again by visiting the same file.  Of course, this works only for buffers that were visiting files.

Saving Text in Registers
========================

When you want to insert a copy of the same piece of text several times, it may be inconvenient to yank it from the kill ring, since each subsequent kill moves that entry further down the ring.  An alternative is to store the text in a register and later retrieve it.

C-x r s R
     Copy region into register R (copy-to-register).

C-x r i R
     Insert text from register R (insert-register).

M-x append-to-register RET R
     Append region to text in register R.

M-x prepend-to-register RET R
     Prepend region to text in register R.

C-x r s R stores a copy of the text of the region into the register named R.  C-u C-x r s R, the same command with a numeric argument, deletes the text from the buffer as well; you can think of this as ``moving'' the region text into the register.

M-x append-to-register RET R appends the copy of the text in the region to the text already stored in the register named R.  If invoked with a numeric argument, it deletes the region after appending it to the register.  The command prepend-to-register is similar, except that it prepends the region text to the text in the register, rather than appending it.

C-x r i R inserts in the buffer the text from register R.  Normally it leaves point before the text and places the mark after, but with a numeric argument (C-u) it puts point after the text and the mark before.

Saving Rectangles in Registers
==============================

A register can contain a rectangle instead of linear text.  The rectangle is represented as a list of strings.  See Rectangles, for basic information on how to specify a rectangle in the buffer.

C-x r r R
     Copy the region-rectangle into register R (copy-rectangle-to-register).  With numeric argument, delete it as well.

C-x r i R
     Insert the rectangle stored in register R (if it contains a rectangle) (insert-register).

The C-x r i R command inserts a text string if the register contains one, and inserts a rectangle if the register contains one.

See also the command sort-columns, which you can think of as sorting a rectangle.  See Sorting.

Saving Window Configurations in Registers
=========================================

You can save the window configuration of the selected frame in a register, or even the configuration of all windows in all frames, and restore the configuration later.

C-x r w R
     Save the state of the selected frame's windows in register R (window-configuration-to-register).

C-x r f R
     Save the state of all frames, including all their windows, in register R (frame-configuration-to-register).

Use C-x r j R to restore a window or frame configuration. This is the same command used to restore a cursor position.  When you restore a frame configuration, any existing frames not included in the configuration become invisible.  If you wish to delete these frames instead, use C-u C-x r j R.

Keeping Numbers in Registers
============================

There are commands to store a number in a register, to insert the number in the buffer in decimal, and to increment it.  These commands can be useful in keyboard macros (see Keyboard Macros).

C-u NUMBER C-x r n R
     Store NUMBER into register R (number-to-register).

C-u NUMBER C-x r + R
     Increment the number in register R by NUMBER (increment-register).

C-x r i R
     Insert the number from register R into the buffer.

C-x r i is the same command used to insert any other sort of register contents into the buffer.  C-x r + with no numeric argument increments the register value by 1; C-x r n with no numeric argument stores zero in the register.

Keeping File Names in Registers
===============================

If you visit certain file names frequently, you can visit them more conveniently if you put their names in registers.  Here's the Lisp code used to put a file name in a register:

     (set-register ?R '(file . NAME))

For example,

     (set-register ?z '(file . "/gd/gnu/emacs/19.0/src/ChangeLog"))

puts the file name shown in register z.

To visit the file whose name is in register R, type C-x r j R.  (This is the same command used to jump to a position or restore a frame configuration.)

Bookmarks
=========

Bookmarks are somewhat like registers in that they record positions you can jump to.  Unlike registers, they have long names, and they persist automatically from one Emacs session to the next.  The prototypical use of bookmarks is to record ``where you were reading'' in various files.

C-x r m RET
     Set the bookmark for the visited file, at point.

C-x r m BOOKMARK RET
     Set the bookmark named BOOKMARK at point (bookmark-set).

C-x r b BOOKMARK RET
     Jump to the bookmark named BOOKMARK (bookmark-jump).

C-x r l
     List all bookmarks (list-bookmarks).

M-x bookmark-save
     Save all the current bookmark values in the default bookmark file.

The prototypical use for bookmarks is to record one current position in each of several files.  So the command C-x r m, which sets a bookmark, uses the visited file name as the default for the bookmark name.  If you name each bookmark after the file it points to, then you can conveniently revisit any of those files with C-x r b, and move to the position of the bookmark at the same time.

To display a list of all your bookmarks in a separate buffer, type C-x r l (list-bookmarks).  If you switch to that buffer, you can use it to edit your bookmark definitions or annotate the bookmarks.  Type C-h m in the bookmark buffer for more information about its special editing commands.

When you kill Emacs, Emacs offers to save your bookmark values in your default bookmark file, ~/.emacs.bmk, if you have changed any bookmark values.  You can also save the bookmarks at any time with the M-x bookmark-save command.  The bookmark commands load your default bookmark file automatically.  This saving and loading is how bookmarks persist from one Emacs session to the next.

If you set the variable bookmark-save-flag to 1, then each command that sets a bookmark will also save your bookmarks; this way, you don't lose any bookmark values even if Emacs crashes.  (The value, if a number, says how many bookmark modifications should go by between saving.)

Bookmark position values are saved with surrounding context, so that bookmark-jump can find the proper position even if the file is modified slightly.  The variable bookmark-search-size says how many characters of context to record on each side of the bookmark's position.

Here are some additional commands for working with bookmarks:

M-x bookmark-load RET FILENAME RET
     Load a file named FILENAME that contains a list of bookmark values.  You can use this command, as well as bookmark-write, to work with other files of bookmark values in addition to your default bookmark file.

M-x bookmark-write RET FILENAME RET
     Save all the current bookmark values in the file FILENAME.

M-x bookmark-delete RET BOOKMARK RET
     Delete the bookmark named BOOKMARK.

M-x bookmark-insert-location RET BOOKMARK RET
     Insert in the buffer the name of the file that bookmark BOOKMARK points to.

M-x bookmark-insert RET BOOKMARK RET
     Insert in the buffer the contents of the file that bookmark BOOKMARK points to.