Use Safari 3 to help with text searching tasks

There are lots of ways to find words and phrases within text files on your Mac. You can use Spotlight, of course, or open those files with a text editor such as TextEdit, BareBones’ TextWrangler, or any of the other hundreds of text editors out there. If you’re Terminally-inclined, you can open and search text files in vi, nano, or emacs. But here’s an alternative you may have never considered: Safari. In particular, Safari 3.0 or later.

In the latest versions of Safari, Apple introduced an enhanced find mode that does a few things when you search for words on a Web page: the background dims to a mid-level gray, matching words are highlighted against a white background, and the currently-selected matching words are shown against a bold yellow background. Taken as a whole, this feature set makes it really easy to visually spot all the matches on a page, focus on the currently-selected match, and yet still easily read the surrounding text for content.

But who says you have to use Safari’s nice find features on a Web page? Safari can open any plain text file on your hard drive, in one way or another. Most text files should open just fine using Safari’s File -> Open menu. Open the document in question, press Command-F to start a search, enter your search terms, and enjoy the result. As an example, here are the results of a Safari search on the word battery in a recent article I wrote:

While I’ve trimmed the screenshot, you can see just how easy it is to identify all the matches, along with the presently-selected match. (You can then select the next match by pressing Command-G, and the highlight will move forward; Shift-Command-G will move back to the previous match.)

While Safari can open and read most any plain text file, you may find that the File -> Open menu won’t work on files you know are plain text. As an example, if you ask Safari to look in the /var/log folder (where log files reside), you can see the various .log files in that folder, but you won’t be able to open them through the Open dialog. Instead, open the folder in the Finder, start dragging the text file you’d like to look at, press and hold Command and Option, and drop the text file on Safari’s Dock icon. The modifier keys force Safari to accept the dropped file, even if Safari doesn’t know what to do with that type of file. As long as the file really is plain text, it should open and display just fine.

There are some limits to this trick—Safari will not be very happy about opening multi-megabyte files, and if Safari finds more than 100 matches for your search term, it will not highlight all of your matches at once. (If you use Command-G to find the next match, each will briefly flash with a yellow background before dimming to the standard OS X highlight color background.)

I don’t think Safari is a replacement for any of the excellent search tools we have at our disposal. It might, however, be useful in particular situations, so file this away for possible future use.