Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Emacs editing environment, Part 4: Options, registers, and bookmarks

Get going with this famous open source editor

Michael Stutz, Author, Consultant
Photo of Michael Stutz
Michael Stutz is author of The Linux Cookbook, which he also designed and typeset using only open source software. His research interests include digital publishing and the future of the book. He has used various UNIX operating systems for 20 years.

Summary:  Take control of your editing session within Emacs and use it to your advantage. This tutorial is the fourth in a series, and shows you three areas of Emacs that control some aspect of the editing session: various command-line options, the register, and bookmark facilities for setting and saving positions and data. Knowing how and when to use these features, and what tricks are possible with them, are important topics in power editing.

View more content in this series

Date:  17 Jul 2007
Level:  Intermediate PDF:  A4 and Letter (439 KB | 21 pages)Get Adobe® Reader®

Activity:  17170 views
Comments:  

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 C-x r.

The following sections take you through the commands for setting, viewing, restoring, and jumping to registers.

Save a register

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:

  1. 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
                                

  2. Type C-x r space X to save the point in register X.

View a register

Use the view-register function to view the contents of a register.

Try looking at the contents of register X:

  1. Type M-x view-register and press Enter.

  2. When prompted in the minibuffer, type X for 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 buffer. Type C-x 1 to close the window.

Jump to a register

To jump to a point you've saved in a register or to restore a window or frame configuration, use the jump-to-register function, which is bound to C-x r j.

Try jumping to the point you've saved in register X:

  1. Type M-> to move to the end of the buffer—far away from the point you've saved.

  2. Type C-x r j X to jump to that saved point. You're back in the forests now.

Restore a register

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.

Try it:

  1. Click and drag B1 over the text and what art to mark these three words as the region.

  2. Save the region to register q: Type C-x r s q.

  3. Move to the end of the buffer and insert a blank line: Type M-> and press Enter.

  4. 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.

  5. 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
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 of Emacs register commands

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
KeyFunctionDescription
C-x r space X point-to-register Save point to register X.
C-x r s X copy-to-register Save the region to register X.
C-x r r X copy-rectangle-to-register Save the selected rectangle to register X.
undefined view-register View the contents of a given register.
C-x r j X jump-to-register Move point to the location given in register X.
C-x r i X insert-register Insert the contents of register X at point.

3 of 8 | Previous | Next

Comments



static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=AIX and UNIX, Open source
ArticleID=240603
TutorialTitle=Emacs editing environment, Part 4: Options, registers, and bookmarks
publish-date=07172007
author1-email=stutz@dsl.org
author1-email-cc=mmccrary@us.ibm.com