Sams Teach Yourself Emacs in 24 Hours

ContentsIndex

Hour 8: Searching for Text in Multiple Files

Previous HourNext Hour

Sections in this Hour:

 

Keeping Points in Buffers for Several Sessions (Bookmarks)


You might have several locations you are looking at often in your computer!

Examples of these might include

To help you easily access these locations, Emacs has a bookmarks feature that is similar to bookmarks in World Wide Web browsers. The two basic commands used with bookmarks are setting a bookmark that is bound to C-x r m (bookmark-set) and jumping to a bookmark which is bound to C-x r b (bookmark-jump). You might think that these bookmark commands aren't very useful when they are bound to such difficult-to-remember keys, but they are, and I strongly suggest that you bind these commands to functions keys.

Setting the bookmark is simple: Go to the location where you want to set the bookmark and press C-x r m (bookmark-set). Emacs will then ask you for a name for the bookmark. This name is for your use when you later want to return to the bookmark. Besides recording the filename of the bookmark, Emacs also records its location in the file. This recording includes text before and after the bookmark, which makes Emacs capable of finding the location even though the buffer might have changed. When you want to jump to a bookmark, press C-x r b (bookmark-jump). Emacs then, in the minibuffer, asks you for the name of the bookmark you want to jump to. In the minibuffer you can use the tabulator key to complete the bookmark name.

Tip - If you have many bookmarks, you might be interested in organizing these in a menu structure. This is, unfortunately, not possible, but you can use the fact that completion is available when naming the bookmark to jump to. Thus you might name your bookmark book/Chapter 1/search to indicate a menu structure consisting of book at the outer level, Chapter 1 as a subentry to book, and search as a subentry to Chapter 1.

Managing Bookmarks

In addition to creating and jumping to bookmarks, you might also want to rename, list, annotate, and delete bookmarks. All these actions can be handled in a special buffer, which contains a list of all the bookmarks. To get to this list press M-x (list-bookmarks). This will bring up a buffer similar to the one shown in Figure 8.10.

Figure 8.10
The list of bookmarks is obtained by pressing M-x (list-bookmarks).

This is not an ordinary buffer for editing but a buffer for managing your bookmarks; thus, the bookmark managing functions are bound to single keys. This is like the buffer for managing the list of buffers described in Hour 6, "Editing Several Files at Once." If you press ?, the window splits in two and in one of the windows, the available commands are listed. There are many commands regarding jumping to bookmarks.

By pressing the number 1, you jump to the bookmark on the current line. To delete some bookmarks, move to each of the lines describing the bookmarks and press d (for delete). A capital D will show in the left-most column indicating that the bookmarks have been marked for deletion. To actually delete the marked bookmarks, press x (for execute). If you change your mind before you have pressed x, you can unmark the bookmark by pressing u (for undelete). To rename a bookmark, press r on the line with the bookmark (for rename). Emacs will ask, in the minibuffer, for the new name of the bookmark. By pressing M-p you can access the original name for the bookmark.

Saving Bookmarks

Bookmarks are, by default, saved to the file $HOME/.emacs.bmk. The default name can, of course, be changed. (See the info pages on Emacs for a description on how to do this, if you really need to.) This should however not be necessary. The bookmarks are saved to the file when Emacs exits. This might not be good enough for you if you have the habit of exiting your window manager without exiting Emacs first. To make Emacs save your bookmarks to a file every time you change, delete, or add a bookmark, insert the following line into your .emacs file:


(setq bookmark-save-flag 1)

You should really do that, because this spares you a lot of trouble. Saving the bookmarks takes no time unless you have hundreds of them.

Sams Teach Yourself Emacs in 24 Hours

ContentsIndex

Hour 8: Searching for Text in Multiple Files

Previous HourNext Hour

Sections in this Hour: