editing environment, Part 4: Options, registers, and bookmarks Get going with this famous open source editor

Skill Level: Intermediate

Michael Stutz ([email protected]) Author Consultant

17 Jul 2007

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.

Section 1. Before you start

Learn what to expect from this tutorial, and how to get the most out of it.

About this series

The Emacs editing environment is a favorite of UNIX® developers. It's known around the world as the king of editors, but many users find it has a bit of a learning curve. The Emacs environment doesn't seem intuitive at first glance, and it doesn't work like other editors and word processors. But learning Emacs doesn't have to be difficult. Once you get going, you'll see how intuitive it is and become more comfortable with it after each use. This tutorial series (see Resources) shows you the way, taking you from the basics of Emacs, such as its features, philosophy, key-command layout, and methods for editing text, through many of its powerful editing features.

After completing this series (see Resources), you'll be able to comfortably use

Options, registers, and bookmarks © Copyright IBM Corporation 1994, 2008. All rights reserved. Page 1 of 26 developerWorks® ibm.com/developerWorks

Emacs for everyday editing, be well on your way to Emacs proficiency, and have a good feel for many of the advanced capabilities of Emacs.

About this tutorial

This is the fourth in a series of tutorials on learning Emacs (see Resources). Previous tutorials in this series on using the Emacs editor took you through some of the basics; this installment rounds out what you've learned so far with the special topics of options, registers, and bookmarks. Like many applications, Emacs has a number of command-line options that alter its functionality; in this tutorial, you learn how and when to use them—and you also learn the special Emacs facilities of registers and bookmarks, which are powerful ways of storing locations and other values for current and future editing sessions.

Objectives

The primary objective of this tutorial is to show you how to use the Emacs command-line options, register, and bookmark facilities. It assumes that you've already taken the three previous tutorials in this series (see Resources) and have a basic understanding of text-editing techniques.

After working through this tutorial, you'll have learned a number of tricks for controlling your Emacs session: You'll have gained an understanding for how your Emacs editing environment can be modified and customized using various command-line options—including those for running Emacs non-interactively—and how you can use registers and bookmarks to save positions and data for later retrieval.

Prerequisites

This tutorial builds on several concepts introduced in the first three tutorials in this series, so you should take them before attempting this tutorial (see Resources). The practice file you made in the first tutorial and used in the third tutorial is used again in the examples (see Downloads if you don't have it).

The special Emacs notation for representing keystrokes, which is used in this tutorial and throughout the series, is described in the introduction of the first tutorial of the series, "Emacs editing environment, Part 1: Learn the basics of Emacs" (see Resources).

Although this tutorial is written for all levels of UNIX expertise, it's helpful if you have at least a rudimentary understanding of the UNIX file system:

• Files • Directories

Options, registers, and bookmarks Page 2 of 26 © Copyright IBM Corporation 1994, 2008. All rights reserved. ibm.com/developerWorks developerWorks®

• Permissions • File system hierarchy

System requirements

This tutorial requires a user account on any UNIX-based system that has a recent copy of Emacs installed.

There are several varieties of Emacs; the original and most popular is GNU Emacs, which is published online by the GNU Project (see Resources).

You should have a recent copy of GNU Emacs—one that is at version 20 or greater. Versions 20 and 21 are the most commonly available, and development snapshots of version 22 are also available. This tutorial works with any of these versions for Emacs. If your system is running something older, it's time to upgrade.

To know what version of Emacs you have running, use the GNU-style --version flag:

$ emacs --version GNU Emacs 22.0.91.1 Copyright (C) 2006 Free Software Foundation, Inc. GNU Emacs comes with ABSOLUTELY NO WARRANTY. You may redistribute copies of Emacs under the terms of the GNU General Public License. For more information about these matters, see the file named COPYING. $

Additionally, some parts of the tutorial showing graphical elements of Emacs operation deal specifically with the X Window System. To go through these sections, you should have an X server up and running.

Section 2. Understand the Emacs runtime and display options

Emacs has a number of command-line options that affect its behavior. You usually start Emacs with no options at all—maybe a filename argument or two—but the options let you perform a number of advanced operations, including running code on files in batch mode. There are many times when it's convenient and helpful to use these options, and they're good for a number of specific situations, which are described in this section. (One particular instance where calling Emacs from the command line with command-line options is useful and popular is in shell scripts or startup files.)

Options, registers, and bookmarks © Copyright IBM Corporation 1994, 2008. All rights reserved. Page 3 of 26 developerWorks® ibm.com/developerWorks

Emacs uses the GNU-style "long" options, which begin with two dashes and take either a traditional space character or an equals sign between the option and its argument. These long options are intended to be easy to understand and remember—the long-style option to start Emacs in inverse video, for example, is --inverse-video —but as a consequence, they're longer to type than options on most other tools and applications. However, most Emacs options have a traditional single-hyphen equivalent. A complete list of options, including both styles, is given in Table 2, later in this tutorial.

Modify file operations

Some of the most popular and useful Emacs options are the ones for modifying or specifying files. You can perform a number of handy tricks with these options.

Automatically open certain files

The only argument Emacs takes is a filespec. When you give a file, a list of files, or another filespec as arguments, Emacs starts and opens the specified files into individual buffers of their own, just as if you'd specified each of them in turn with calls to the find-file function, C-x C-f:

$ emacs /usr/local/share/newdev/inputs/*.txt

This command starts Emacs, opens all the files in the /usr/local/share/newdev/inputs/ directory having a .txt file name extension, and puts them into new buffers of their own. Each buffer's name is identical to the corresponding filename.

If there are certain files that you always edit, such as a task list, day planner, or to-do file, it makes sense to add them to the emacs command line that you use in your startup file. You can have Emacs automatically open them into buffers as soon as it begins by putting them there:

emacs plans tasks

Emacs opens files in the order they're given, so the last file you specify is always the current buffer. In the previous example, the file named tasks is the current buffer when Emacs starts. If you specify more than one file, Emacs splits the window in half across the middle, showing one file's buffer on the top and one on the bottom. Type C-x 1 to delete the second window so that the entire Emacs window is filled with the one buffer.

If you specify three or more files, Emacs displays the last file in the top window as the current buffer; it displays a buffer list in the second window beneath it, showing the names of all the open buffers, their sizes, their major modes, and the names of the files they correspond to. Again, you type C-x 1 to remove this window.

Options, registers, and bookmarks Page 4 of 26 © Copyright IBM Corporation 1994, 2008. All rights reserved. ibm.com/developerWorks developerWorks®

You can also use the long --visit and --file options to precede the filespec. The command lines in Listing 1, for example, are equivalent.

Listing 1. Equivalent command lines for specifying Emacs filenames

emacs myfile emacs --visit myfile emacs --visit=myfile emacs --file myfile emacs --file=myfile

These long options are sometimes useful when you're constructing long Emacs command lines for batch operations (see the Perform batch operations section).

Start working on a new file

If a file you give doesn't exist on the file system, Emacs opens a new buffer and indicates on the mode line that it's a new file. For example, if the current directory is an empty directory named /usr/local/src/projx/inputs/ and you type emacs README, you open a new buffer named README. If you save the buffer to disk, you write a file named /usr/local/src/projx/inputs/README.

Try making a new file in your home directory:

$ emacs myfile

Notice how Emacs tells you (in the minibuffer) that myfile is a new file.

Type some text in the buffer:

Enough! or Too much!

Now, save the buffer to disk: Type C-x C-s C-x C-c to save the buffer to your new file and then exit Emacs. Notice that you don't have to give a filename, which you normally do if you visit a new file, as described in previous tutorials—that's because what you specify as a command-line argument is the name of a file, not a buffer. When the file doesn't exist, Emacs makes it. (If you exit without saving the file, you're asked to confirm—and although the file won't exist, an autosave file with your edits has been written.)

Open files at certain positions

If you precede a filename argument with +number , Emacs opens the file and moves point to the line corresponding to number. This is useful for when you start Emacs with the purpose of editing a file in a particular position. Say, for example, you're debugging a C program, myprog.c, and the compiler has warned you that an error exists on line 315; you can type emacs +315 myprog.c to open the source file into a buffer for editing at the exact line where the compiler found the error.

Options, registers, and bookmarks © Copyright IBM Corporation 1994, 2008. All rights reserved. Page 5 of 26 developerWorks® ibm.com/developerWorks

If you've been reading the tutorials in this series and typing the examples given, you should have a sample file named practice.b containing William Blake's "The Tiger;" in case you haven't saved it or your copy's been mangled by the text operations in previous tutorials, a clean copy is available in the Downloads section of this tutorial.

Try opening practice.b with the cursor at the fifteenth line:

$ emacs +15 practice.b

(Exit with C-x C-c.)

This option is also good for opening a buffer at the end of a file. To do that, give a number that's very high, relative to the length of the file.

Try opening practice.b with the cursor at the end of the file:

$ emacs +999 practice.b

(Exit with C-x C-c.)

You can also specify the horizontal position in the file. If you think of vertical lines as rows, then horizontal positions across each line are columns, and they are given by following the line number (row position) with a colon character (:) and the numeric value for the column.

Try opening practice.b with the cursor on the twelfth character across (twelfth column) on the third line (row):

$ emacs +3:12 practice.b

The cursor should be on the a of immortal when you do this. (Then, exit with C-x C-c.)

If the specified line has fewer columns than you specify, the cursor doesn't wrap to the next line; it stays at the end of the line.

If you specify multiple files, you can precede each file with its own option for the row and column at which to place the cursor. The default behavior for each file is to open it into a buffer with point at the first row and first column—this is the equivalent of preceding every filename with +1:1.

Insert files

Use the --insert option to insert the contents of the file you give as an argument. Give this option after the file argument you want to insert to; with no preceding file argument, this option inserts the given file into a scratch buffer.

Options, registers, and bookmarks Page 6 of 26 © Copyright IBM Corporation 1994, 2008. All rights reserved. ibm.com/developerWorks developerWorks®

Try it:

$ emacs --insert myfile

This starts Emacs with a scratch buffer that should look like Figure 1, where your new file, myfile, is inserted and point is at the beginning of the buffer.

Figure 1. Insert a file into an Emacs buffer from the command line.

(Exit with C-x C-c.)

Now, try inserting your new file into practice.b:

$ emacs practice.b --insert myfile

This starts Emacs with the practice.b file open in a new buffer and the contents of myfile inserted at the beginning of it.

Options, registers, and bookmarks © Copyright IBM Corporation 1994, 2008. All rights reserved. Page 7 of 26 developerWorks® ibm.com/developerWorks

(Exit with C-x C-c, type n, and then yes to confirm an exit without saving.)

You can combine a positional option with an insert, and you can give additional arguments at the same time. Try it:

$ emacs +10:5 practice.b --insert myfile anotherfile

This starts Emacs with the contents of practice.b in a new buffer, inserting myfile at the fifth column of the tenth line, and then opens a new file named anotherfile in a new buffer and makes it the current buffer.

(Exit with C-x C-c, type n, and then yes to confirm an exit without saving.)

Change the display properties

Perhaps second in popularity to the Emacs options for specifying files are the options pertaining to the X Window System. You can change the way Emacs appears in X using these options. Most of these options are standard X Window System options that work on all X clients.

Run Emacs in an xterm

One of the most useful X-related Emacs command-line options is -nw, which you saw demonstrated in the first tutorial in this series (see Resources). This option opens Emacs in the current terminal instead of creating a new X client window of its own. It's useful in many situations when you're running a remote terminal window over a slow connection and don't want to start a local X client window, or when you're doing a number of operations in a terminal and want to do a quick edit to a file without launching a new window for Emacs to open in.

If you have X running, try it in a terminal window:

$ emacs -nw

When you type this, Emacs starts in terminal mode and appears in that terminal instead of making a new X client window. (Type C-x C-c to exit.)

Specify the server

Use the --display option to specify where the Emacs X client window should be displayed. It takes as an argument the hostname of the X server followed by a colon and the display number, which indicates the number of the particular X server on that host (it's usually 0, but it differs on systems running multiple X servers).

For example, this command displays the client on the default server on the local host and is equivalent to not giving any option:

Options, registers, and bookmarks Page 8 of 26 © Copyright IBM Corporation 1994, 2008. All rights reserved. ibm.com/developerWorks developerWorks®

emacs --display :0

To open the Emacs window on the default server of a remote host named rs6000, type:

emacs --display rs6000:0

Specify the window geometry

The --geometry option is another standard X option. Give the width, height, and optional X and Y offsets of the client window in this format:

WIDTHxHEIGHT[{+-}XOFFSET{+-}YOFFSET]

The offsets are counted from the top-left corner of the screen and can be either positive or negative values.

If you're running this tutorial in an X server, you can try it now. To open a long window 25 characters wide and 250 characters high in the upper-left corner of your desktop, type:

$ emacs --geometry 25x250+0+0

(Exit with C-x C-c.)

Set the Emacs fonts and colors

You can use other standard X options to start Emacs with a specified font, colors for the text (the foreground), and blank space behind it (the background).

To specify a font, give an X font name as a quoted argument to the --font option. Colors are specified with a number of options (see Table 1).

Table 1. Emacs command-line options for colors Option Description --foreground-color color Set the foreground color to color. -fg color --background-color color Set the background color to color. -bg color --border-color color Set the border color to color. -bd color --cursor-color color Set the cursor color to color. -cr color --mouse-style color Set the mouse pointer color to color. -ms color

Options, registers, and bookmarks © Copyright IBM Corporation 1994, 2008. All rights reserved. Page 9 of 26 developerWorks® ibm.com/developerWorks

You can combine font and color options, too. Try setting them all at once:

$ emacs -fn "-misc-fixed-medium-r-normal-ja-13-120-75-75-c-120-iso10646-1" \ -bg "sea green" -fg "white" -cr "brown1" -ms "royalblue1" practice.b

This command starts Emacs in an X client window that looks like Figure 2.

Figure 2. Specifying Emacs colors and fonts

The particular fonts and colors that are available to you depend on your X server and its configuration.

Control the startup files

Emacs lets you run a startup file, also called an init file, which can contain Emacs Lisp code for evaluation, and it's normally used to set various properties and variables—you use it to customize your editing environment. It's a hidden file named .emacs, and it's in your home directory. (Customizing your .emacs file is the subject of a future tutorial in this series.)

You can specify that Emacs doesn't run any existing startup file by giving the -q option. This option is useful for debugging new additions to your startup file, when you want to run an Emacs session without any customizations you've made, when you're using a new Emacs function, when something is behaving oddly, or you're not sure if one of your own customizations is the culprit:

emacs -q

Because everyone's .emacs file is different, you might want to try someone else's

Options, registers, and bookmarks Page 10 of 26 © Copyright IBM Corporation 1994, 2008. All rights reserved. ibm.com/developerWorks developerWorks®

startup file for a change of pace and to see how others customize their editing environment. You can do that with the -u option, giving the name of the user as an argument to the option. This is useful for trying out the customizations of a local Emacs wizard, such as (for example) user joe:

emacs -u joe

This command starts Emacs with the .emacs startup file in user joe's home directory, as long as that file is readable by you. If user joe doesn't exist, doesn't have a .emacs file, or the file isn't readable, Emacs starts with no init file.

Emacs also has a global startup file, site-start.el, which is often kept in the /etc/emacs/ directory tree. The --no-site-file option starts Emacs without evaluating the Emacs Lisp in this file.

Finally, --debug-init enables the Lisp debugger to check your startup file, which is useful when you're adding things to it and something isn't working right. You can even use it to debug another user's startup file:

emacs -u joe --debug-init

Use options for Emacs Lisp execution

Emacs has command-line options for the execution of Lisp code; these options are particularly useful when you're running Emacs in batch mode, as described below.

Use -l to load a Lisp file; give the name of the file as an argument. Emacs looks for the file first in the current directory and second in any of the directories in the EMACSLOADPATH environment variable, if set. To load a Lisp file from elsewhere on the file system, give the full path name as the argument.

To run an Emacs Lisp function from the command line, use the -f option and give the name of the Lisp function to execute as an option. If the function takes any arguments, be sure to give them.

For example, try starting Emacs and running the phases-of-moon function:

$ emacs -f phases-of-moon

When you do this, your Emacs session should look like Figure 3, with two windows in the Emacs X client: a scratch buffer and a window containing dates for the current phases of the moon. (Exit with C-x C-c.)

Figure 3. Starting Emacs with the phases-of-moon function

Options, registers, and bookmarks © Copyright IBM Corporation 1994, 2008. All rights reserved. Page 11 of 26 developerWorks® ibm.com/developerWorks

To start Emacs and evaluate a Lisp expression, use the --eval option; follow it with a quoted Lisp expression.

For example, try starting Emacs with your new file, moving to the end of the buffer, and evaluating the Lisp code (insert (current-time-string)), which inserts the current time and date into the current buffer at point:

$ emacs +999 myfile --eval "(insert (current-time-string))"

(Exit with C-x C-c, type n, and then yes to confirm an exit without saving.)

Perform batch operations

You can run Emacs in batch mode for non-interactive processing. When you give the --batch option, Emacs doesn't write to standard output, and it takes input from the standard input instead of the minibuffer. (It still writes to standard error.)

Options, registers, and bookmarks Page 12 of 26 © Copyright IBM Corporation 1994, 2008. All rights reserved. ibm.com/developerWorks developerWorks®

Normally, you'd use either the -l or -f options, as just described, with --batch to evaluate Lisp code or run functions non-interactively. If you call Emacs with --batch and no other options, Emacs exits immediately.

Otherwise, Emacs performs the given commands until a command causes it to exit or until it reaches a --kill option, which immediately exits Emacs—put it at the end of the command line to exit Emacs when the batch operations are completed.

Try running Emacs in batch mode to insert the contents of myfile into practice.b at the thirty-fifth line, saving it with the save-buffer function, and then exiting:

$ emacs --batch +35 practice.b --insert myfile -f save-buffer --kill

Now, look at your practice.b file—it should contain an extra line.

You can just as easily take out that line with another Emacs one-liner. Try it:

$ emacs --batch +35 practice.b -f kill-line -f save-buffer --kill

Don't forget to look at practice.b and make sure the extra line is gone.

Table of command-line options

Table 2 summarizes the many Emacs command-line options described so far, giving both their long and short option names (if applicable) and describing their functions.

Table 2. Emacs command-line options Option Description --visit=filespec Open filespec into individual buffers for editing. --file=filespec filespec +row[:column] Move point to line number row and (optional) horizontal position column in the file (default is +1:1). --insert file Insert file at the beginning of the buffer. --debug-init Use the Lisp debugger on the .emacs startup file. --no-init-file Don't run any .emacs startup file. -q --no-site-file Don't run the global site-start.el file. -u user Use the .emacs startup file of user. --user user --funcall function Execute the Emacs Lisp function. -f function --eval expression Evaluate the Emacs Lisp expression.

Options, registers, and bookmarks © Copyright IBM Corporation 1994, 2008. All rights reserved. Page 13 of 26 developerWorks® ibm.com/developerWorks

--execute expression --load file Execute the Emacs Lisp instructions in file. -l file -batch Use batch (non-interactive) mode. --batch -kill Exit Emacs when in batch mode. --kill --name name Use name as the name for the Emacs X client window (default is "emacs"). -T title Use title as the title for the Emacs X client --title title window (default is name@FQDN, where FQDN is the host's fully qualified domain name). --reverse-video Use reverse video, swapping the foreground and -r background colors. --iconic Start Emacs as an icon instead of an active -iconic window. --icon-type When iconifying the Emacs window, use the -i Emacs icon (usually qat /usr/share/emacs/version/etc/.xpm) instead of any window manager defaults. -fn name Use name for the Emacs window font. -font name --border-width width Set the window border to width pixels. -bw width --internal-border width Set the window's internal border to width pixels. -ib width --g dimensions Set the window's width, height, and position --geometry dimensions according to the given X window dimensions (the default is to make the window 80x40 characters). --foreground-color color Set the foreground color to color. -fg color --background-color color Set the background color to color. -bg color --border-color color Set the border color to color. -bd color --cursor-color color Set the cursor color to color. -cr color --mouse-color color Set the mouse-pointer color to color. -ms color -d name Open the Emacs window on the X display --display name corresponding to name. -nw In X, don't use an X client window, but open in --no-windows the current terminal window instead. This option doesn't affect console sessions. -t file Redirect standard I/O to file instead of terminal. --terminal file

Options, registers, and bookmarks Page 14 of 26 © Copyright IBM Corporation 1994, 2008. All rights reserved. ibm.com/developerWorks developerWorks®

Section 3. 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

Options, registers, and bookmarks © Copyright IBM Corporation 1994, 2008. All rights reserved. Page 15 of 26 developerWorks® ibm.com/developerWorks

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:

Options, registers, and bookmarks Page 16 of 26 © Copyright IBM Corporation 1994, 2008. All rights reserved. ibm.com/developerWorks developerWorks®

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

Options, registers, and bookmarks © Copyright IBM Corporation 1994, 2008. All rights reserved. Page 17 of 26 developerWorks® ibm.com/developerWorks

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 Key Function Description 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-registerSave the selected rectangle to

Options, registers, and bookmarks Page 18 of 26 © Copyright IBM Corporation 1994, 2008. All rights reserved. ibm.com/developerWorks developerWorks®

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.

Section 4. Use Emacs bookmarks

Emacs has another facility for saving positions in buffers. These Emacs bookmarks work the same as registers, but their labels can be longer than a single character, and they're more permanent: If you save them, you can use them between sessions. They persist until you remove them. As their name implies, bookmarks are handy for saving your position in a buffer so that you can return to it at a later time, most often during a later Emacs session.

This section takes you through using, setting, listing, saving, and deleting Emacs bookmarks.

Set a bookmark

Bookmarks are like registers for saving point, except they last beyond your current session. Bookmarks are convenient ways to mark your position in a file and return to it later—you can have bookmarks for many files, and you can have many bookmarks in a single file.

To set a bookmark for the current point in the file that you're visiting in the current buffer, run the bookmark-set function, which is bound to C-x r m.

Follow this command with a name for your bookmark; by default, it's the name of the current buffer.

Try saving some bookmarks now:

1. Start Emacs with the cursor 20 lines into your practice file:

$ emacs +20 practice.b

2. Type C-x r m to save this point as a bookmark.

Options, registers, and bookmarks © Copyright IBM Corporation 1994, 2008. All rights reserved. Page 19 of 26 developerWorks® ibm.com/developerWorks

3. When Emacs asks for a name to use for this bookmark, press Enter to use the name of the buffer (practice.b).

4. Type M-< to move to the top of the buffer.

5. Save a new bookmark named top: Type C-x r m top.

List your bookmarks

The bookmarks-bmenu-list function lists a menu of all the bookmarks you've set. Run it by typing C-x r l so that your session looks like Figure 5.

Figure 5. The Emacs bookmark menu

You can select a bookmark from the list by pressing Enter. Emacs visits that file in a

Options, registers, and bookmarks Page 20 of 26 © Copyright IBM Corporation 1994, 2008. All rights reserved. ibm.com/developerWorks developerWorks®

new buffer (if it's not already open in a buffer), makes that the current buffer, and moves point to the position of the bookmark.

Try running this function now, and use the arrow keys to select your "top" bookmark.

Jump to a bookmark

You can visit a bookmark by directly jumping to it, without selecting it from your master list of bookmarks. To jump to a particular bookmark, use the bookmark-jump function, C-x r b. This command moves to the given position in the particular file; if the bookmarked file isn't yet open in a buffer, this command opens it.

You're prompted in the minibuffer for a bookmark to jump to. The prompt uses completion, so you only have to type enough of the bookmark name to make it unique from the other bookmarks and then press Tab to select that bookmark.

Try jumping to your practice.b bookmark: Type C-x r b prac, press the Tab key, and then press Enter.

Delete a bookmark

When you set a bookmark (see Set a bookmark section) using the label of an existing bookmark, you replace the old with the new. That's one way to "delete" a bookmark—to replace its value with the new value. This is common if you keep a single bookmark for marking your place as you read through a large file, because every time you set it you replace its previous value.

But you can delete a bookmark altogether, so the label no longer refers to any position in any file. To do this, run the bookmark-delete function and give the name of the bookmark to delete when asked.

Try deleting your "top" bookmark:

1. Type C-x bookmark-delete and press Enter.

2. Give the name of the bookmark when asked: Type top and press Enter.

You can verify that the bookmark has been deleted by calling up the bookmark list with C-x r l.

Save your bookmarks

In new versions of Emacs, any bookmarks you set last for the current Emacs

Options, registers, and bookmarks © Copyright IBM Corporation 1994, 2008. All rights reserved. Page 21 of 26 developerWorks® ibm.com/developerWorks

session are automatically saved to your permanent bookmark file as soon as you make them. Your bookmark file is a hidden file named .emacs.bmk in your home directory.

If your Emacs isn't configured to automatically save your bookmarks, you can save them to your .emacs.bmk file by running the bookmark-save function. If any new bookmarks haven't been saved when you exit, Emacs asks you whether or not you'd like to save them.

Table of Emacs bookmark commands

Table 4 lists the various commands and keys for using Emacs bookmarks, giving their function names and describing their meanings.

Table 4. Emacs commands for using bookmarks Key Function Description C-x r m Bookmark bookmark-set Set a bookmark named Bookmark. C-x r l bookmarks-bmenu-list List all saved bookmarks. bookmark-delete Delete a bookmark. C-x r b Bookmark bookmark-jump Jump to the location set in the bookmark named Bookmark. undefined bookmark-save Save all bookmarks to the bookmark file, ~/.emacs.bmk.

Section 5. Summary

Wrap-up

In this tutorial, the fourth in a series on using the Emacs editing environment, you've learned how to use three parts of Emacs that control aspects of the editing session: the many command-line options available when starting Emacs, the powerful Emacs register, and bookmarking facilities for setting and saving positions and data. You've also learned a number of power editing techniques that make you a more productive Emacs user.

Future tutorials in this series build on what you've learned here, bringing you further along in your mastery of this versatile editing application.

Options, registers, and bookmarks Page 22 of 26 © Copyright IBM Corporation 1994, 2008. All rights reserved. ibm.com/developerWorks developerWorks®

Downloads

Description Name Size Download method au-practice.b.txt au-practice.b.zip 1KB HTTP

Information about download methods

Options, registers, and bookmarks © Copyright IBM Corporation 1994, 2008. All rights reserved. Page 23 of 26 developerWorks® ibm.com/developerWorks

Resources

Learn •"Emacs editing environment": Check out other parts in this series. •"Use free software within commercial UNIX" (David Dougall, developerWorks, February 2006): Learn how to install GNU Emacs on the IBM AIX® operating system or another commercial UNIX. • Emacs commands: The IBM DB2® Universal Database™ (UDB) Information Center has a quick summary of basic Emacs commands. • New to AIX and UNIX?: Visit the "New to AIX and UNIX" page to learn more about AIX and UNIX. • GNU Project and the Free Software Foundation: Learn more about the GNU Project and the Free Software Foundation from its Web site. • Check out other articles and tutorials written by Michael Stutz: • AIX and UNIX zone • Across developerWorks and IBM

• Search the AIX and UNIX library by topic: • System administration • Application development • Performance • Porting • Security • Tips • Tools and utilities • Java™ technology • Linux® • Open source

• AIX and UNIX: The AIX and UNIX developerWorks zone provides a wealth of information relating to all aspects of AIX systems administration and expanding your UNIX skills. • AIX 5L™ Wiki: A collaborative environment for technical information related to AIX. • Safari bookstore: Visit this e-reference library to find specific technical resources.

Options, registers, and bookmarks Page 24 of 26 © Copyright IBM Corporation 1994, 2008. All rights reserved. ibm.com/developerWorks developerWorks®

• developerWorks technical events and webcasts: Stay current with developerWorks technical events and webcasts. • Podcasts: Tune in and catch up with IBM technical experts. Get products and technologies • GNU Project Web site: Download a free copy of the latest GNU Emacs for your OS and architecture free of charge. • IBM trial software: Build your next development project with software for download directly from developerWorks. Discuss • Participate in the developerWorks blogs and get involved in the developerWorks community. • Participate in the AIX and UNIX forums: • AIX 5L—technical forum • AIX for Developers Forum • Cluster Systems Management • IBM Support Assistant • Performance Tools—technical • Virtualization—technical • More AIX and UNIX forums

About the author

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. You can reach Michael at [email protected]. (Be sure to include "developerWorks" in the subject line.)

Trademarks

IBM, AIX, AIX 5L, and DB2 are registered trademarks of International Business Machines Corporation in the United States, other countries, or both. Java and all Java-based trademarks are trademarks of Sun Microsystems, Inc. in the United States, other countries, or both.

Options, registers, and bookmarks © Copyright IBM Corporation 1994, 2008. All rights reserved. Page 25 of 26 developerWorks® ibm.com/developerWorks

Linux is a registered trademark of Linus Torvalds in the United States, other countries, or both. UNIX is a registered trademark of The Open Group in the United States and other countries.

Options, registers, and bookmarks Page 26 of 26 © Copyright IBM Corporation 1994, 2008. All rights reserved.