Sams Teach Yourself Emacs in 24 Hours


Hour 8: Searching for Text in Multiple Files

Previous HourNext Hour

Sections in this Hour:


Using grep from Within Emacs

If you are used to UNIX, you will most certainly know the two commands called grep and find. grep lists the lines of a file that contain certain text, whereas find lists files that match a pattern. Together, these two commands can search for a given string in all files in a directory structure. Emacs has a command called grep (that is, M-x grep) which interfaces to the grep command. Now that you know about it, forget it please! I told you about it, in case you ever find yourself without igrep and really need to use grep instead. igrep has a much more user-friendly interface than grep. Furthermore, it combines find and grep, which makes it possible to search for files in a directory hierarchy. In the igrep library is a set of different functions:

Caution - agrep is not included in the usual grep packages, so it might not be installed at all at your system. It is, however, available from the WWW at

To tell agrep how many errors are allowed, press C-u before pressing M-x agrep. This makes Emacs ask for options to agrep. Here, you must type -2 to tell it to accept two errors, for example.

Each of the three functions described previously searches for text in files in one directory. There also exist three functions called fgrep-find, igrep-find, and agrep-find, which work the same as those without the extension -find, but they work recursively in a whole directory structure--that is, files in the current directory and all its subdirectories.

Igrep.el by Kevin Rodgers is part of the XEmacs distribution but not part of the GNU Emacs distribution, so if you use GNU Emacs, you should copy igrep to your lisp directory. (If you use both GNU Emacs and XEmacs you should still copy it to your lisp directory. There is no trouble in using this one instead of the one shipped with XEmacs.) In Appendix A, "Installing Functions and Packages from the CD," there is a description on what you should do to install igrep (including what to put in your .emacs file).

Windows Notes - If you are using Windows 95 or 98, you will have to install one of the UNIX tool kits available for Windows, such as the Cygwin tools. There are copies on the CD-ROM. Again, see Appendix A.

Windows NT supports regular expressions somewhat in its program findstr, but most UNIX greps are more powerful. findstr will do in a pinch if you put the following into your .emacs file:

(setq grep-command "findstr /n")

If you use Bash as your shell, insert the following into your .emacs:

(setq w32-quote-process-args ?\")

But then you won't need to substitute findstr for grep.

However, neither operating system has a substitute for find.

Searching for a Definition in Standard C include Files Using fgrep

This task will demonstrate the igrep.el package by showing how you can search through all the standard C include files on your system for a given function. Follow these steps:

1. First you have to locate yourself in the base directory for your include files. (If you want to try this example and do not have include files installed, you might try it with your home directory and a search string that you want to search for in your files.) To go to this directory, press C-x C-f (find-file) and type the name of the directory. This loads the content of the directory into a special buffer. The mode for this buffer is called dired-mode (for directory editor ) and is described in detail in Hour 16, "Interfacing with the System" (see Figure 8.4).

Figure 8.4
In Emacs you can open a directory as easily as you can open a file. This is discussed in detail in Hour 16.

Note - If you already are in the directory in which you want to search, you can skip this step.

2. Now press M-x (fgrep-find). Emacs will ask you for an expression to search for. This is simply the search string when you use fgrep (see Figure 8.5).

Figure 8.5
Emacs asks for the text to search for.

3. Finally Emacs wants to know which files to search for the string in. By default it suggests *, which means all files. This is simply a pattern, known from the shell. So here you can tell it to search only in files ending in .h by typing *.h (see Figure 8.6).

Figure 8.6
Emacs asks for a pattern describing the files in which it should search.

Now Emacs searches recursively for matches, splits the window in two, and, in the lower part, displays the matches, one line for each match (see Figure 8.7).

Figure 8.7
When Emacs has found some matches, it splits the window and shows the matches in one of the panes.

The first three lines are simply the command used to find the matches. The actual matches start on the third line, which tells you which file the match is located in and which line it is located on and shows you the line.

Note - I have resized the window for your eyes only. Emacs doesn't resize itself.

4. Now you can open the files with the matches and go to the line with the match, either by clicking the middle mouse button on the match or by moving point to the match and pressing Enter (see Figure 8.8).

Figure 8.8
Pressing Enter on one of the matches makes Emacs show this match in the other window and jump to the given line.

If you have the feeling of déjà vu now, it might simply be because the interface that igrep uses is the same interface used by occur, which is described in Hour 7. In fact, this interface is used a lot of places in Emacs.

If you press M-u once before you press M-x fgrep, Emacs will ask you for options to give to fgrep. You can consult the manual page for fgrep for information about these options. There are two useful options, namely -i, which makes the searches not case sensitive, and -# (where # should be replaced with a number), which tells grep to display a number of context lines.

Caution - Unfortunately, the options to agrep are not compatible with the options to, for example, fgrep. This means that -# does not tell how many lines of context to use but instead how many errors to accept. It does in fact seem that agrep doesn't support context lines.

In Figure 8.9, you can see an example of the output where two context lines are displayed.

Figure 8.9
Output from igrep, where two lines of context are shown.

Sams Teach Yourself Emacs in 24 Hours


Hour 8: Searching for Text in Multiple Files

Previous HourNext Hour

Sections in this Hour: