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.
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:
Start Emacs with the cursor 20 lines into your practice file:
$ emacs +20 practice.b
C-x r mto save this point as a bookmark.
When Emacs asks for a name to use for this bookmark, press Enter to use the name of the buffer (practice.b).
M-<to move to the top of the buffer.
Save a new bookmark named top: Type
C-x r m top.
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. The Emacs bookmark menu
You can select a bookmark from the list by pressing Enter. Emacs visits that file in a 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.
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
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
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:
C-x bookmark-deleteand press Enter.
Give the name of the bookmark when asked: Type
topand press Enter.
You can verify that the bookmark has been deleted by calling up the bookmark
C-x r l.
In new versions of Emacs, any bookmarks you set last for the current Emacs 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 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
||Set a bookmark named Bookmark.|
||List all saved bookmarks.|
||Delete a bookmark.|
||Jump to the location set in the bookmark named Bookmark.|
||Save all bookmarks to the bookmark file, ~/.emacs.bmk.|