Searching

This section describes TextWrangler's powerful Find command, now enhanced with a flexible file filtering mechanism. It tells you how to search for text in the active window or within a set of files. TextWrangler can also do advanced pattern, or grep, searching. To learn about pattern searching, you should read this chapter first and then read Chapter 8, "Searching with Grep."

Basic Searching and Replacing

This section describes the basic steps for searching and replacing text in a document. Later sections in this chapter cover more advanced techniques. To search and replace text in the front document, follow these steps:

  1. Choose Find from the Search menu. TextWrangler opens the Find & Replace dialog box.

For now, disregard the bottom part of the dialog--the Multi-File Search checkbox and everything below it. The section "Multi-File Searching" later in this chapter discusses this part of the dialog box.

  1. Type the string you are looking for in the Search For text field.

You can use special characters in the Search For text field to search for tabs, line breaks, or page breaks. See "Special Characters" later in this section.

TextWrangler remembers the last 12 search terms you used since launching the application. If you are searching for something you recently searched for, you can choose it from the pop-up menu above the Search For field.

Click the § button to enter the document selection in the Search For field.

  1. Type the replace string (if any) in the Replace With text field.

TextWrangler also remembers the last 12 replace terms you used since launching the application. These appear in the pop-up menu above the Replace With field.

Click the § button to enter the document selection in the Replace With field.

  1. Mark any checkboxes in the middle part of the dialog box that you want to apply to your search.

For more info about these options, see "Search Settings" later in this section.

  1. Click one of the buttons along the right side of the dialog box.
Note

The size of both the search and replace strings is limited only by available memory. However, the memory requirements for searching multi-byte text files are generally at least double those for single-byte text.

The following table explains what each of the buttons does.

This button...

Does this...

Find

Finds the first occurrence of the text in the active window. Shortcut: Cmd-F

Find All

Finds all the occurrences of the search string and displays the results in a search results window.
Shortcut: Cmd-Opt-F

Replace

If there is a selection, replaces the current selection

with the replace string. Otherwise, finds the

first occurrence of the text in the active window after the current insertion point and replaces it with the replace string. Shortcut: Cmd-R

Replace All

Replaces every occurrence of the search string in the active window with the replace string.

Shortcut: Cmd-Opt-R

Don't Find

Saves the settings of the Find & Replace dialog without doing a search. Shortcut: Cmd-D

Cancel

Does not do the search and restores the settings of the dialog box to their previous state. Shortcut: Cmd-.

TextWrangler closes the Find & Replace dialog and then selects the search string in the active window if the search was successful. Once TextWrangler finds your text, you can use the commands in the Search menu (see "Search Menu Reference" later in this chapter). The table below summarizes the most common commands you can use at this point.

This command...

Does this...

Find Again

Finds the next occurrence of the search string without displaying the Find & Replace dialog again. To reverse the search direction, hold down Shift.

Replace

Replaces the selection with the replace text

Replace All

Replaces all occurrences of the search string with the replace string.

Replace & Find Again

Replaces the selection with the replace string and looks for the search string again.

Search Settings

The checkboxes in the Find & Replace dialog let you control how TextWrangler searches your document for the indicated text.

Note

You can set the defaults for many of these settings in the Text Search section of the Preferences window.

Use Grep

When this checkbox is selected, TextWrangler treats the search and replace strings as grep patterns. Otherwise, TextWrangler searches the document for text that matches the search string as it appears literally, and will replace any matched text with the replace string. To learn more about pattern searching see Searching with Grep.

Start at Top

When this checkbox is selected, TextWrangler always starts searches from the beginning of the document. Choosing this option will disable the Wrap Around and Search Backwards settings.

Wrap Around

When this checkbox is selected, TextWrangler continues searching from the beginning of the document if a match is not found (or from the end of the document if searching backwards). Otherwise, TextWrangler stops searching when it reaches the end (or the beginning if searching backwards) of the file. Choosing this option will automatically disable Selection Only. (Unavailable if Start at Top is checked.)

When performing a Replace All with Wrap Around selected, the replace is transformed into "Start at Top" to allow for performance optimizations.

Search Backwards

When this checkbox is selected, TextWrangler searches from the insertion point to the beginning of the document. Otherwise, TextWrangler searches from the insertion point to the end of the file. (Unavailable if Start at Top is checked.)

Search Selection Only

When this checkbox is selected, TextWrangler searches only the selected text. Otherwise, TextWrangler searches the entire document. (Unavailable if Wrap Around or Start at Top is checked.)

Extend Selection

When this checkbox is selected, TextWrangler extends the selection from the current insertion point to the end of the matched search string.

Case Sensitive

When this checkbox is selected, TextWrangler treats upper- and lowercase letters as different letters. Otherwise, TextWrangler treats upper- and lowercase letters as if they were the same.

Match Entire Words

When this checkbox is selected, TextWrangler matches the search string only if it is surrounded in the document text by word-break characters (white space or punctuation). Otherwise, TextWrangler matches the search string anywhere in the text.

Multi-File Search

When this checkbox is selected, TextWrangler searches a set of files for the search string.

 

Special Characters

You can use the following special characters to search for line breaks and other non-printing characters, as well as hexadecimal escapes to search for any desired 8-bit character.

Character

Matches...

\r

line break (carriage return)

\n

Unix line break (line feed)

\t

tab

\f

page break (form feed)

\xNN

hexadecimal character code NN (for example, \x0D for CR)

\\

backslash (\)

The form of a hex escape is "\xNN", where "N" is any single hex digit [0-9,A-F]. The "x" may be upper or lower case. (You can use the ASCII Table in the Window menu to find the hex value for any 8-bit Macintosh character.) You can perform a literal search for any character, including a null, using this option. Malformed escapes are treated as literal strings.

Note

In older versions of TextWrangler, you could not perform a grep search for a null character (ASCII 0), even if it was escaped. This limitation is no longer present; you can find nulls as part of a grep search.

Multi-File Searching

The only difference between single-file searching and multi-file searching is that to perform a multi-file search, you must specify the files to be searched. TextWrangler gives you a great deal of flexibility in how to do this. You can search all the files in a given folder, in open editing windows, or in an existing search results browser. For even great control, you can choose a diverse sets of search sources, or apply TextWrangler's advanced multi-criteria file filtering options.

IMPORTANT

Multi-file searching is no longer a modal operation. When you start a search, TextWrangler will display a search progress window and return control, so that you can continue working. You can perform more than one multi-file searches at a time; each search will have its own progress window. Closing a search's progress window or clicking Cancel in the progress window will stop the operation, and TextWrangler will display a search results browser containing any matches found up to that point.

Starting a Search

To search for a string in multiple files, do the following steps:

  1. Choose Find from the Search menu to open the Find & Replace dialog box (if it is not already open).
  2. Mark the Multi-File Search checkbox.
  1. Type the string you are looking for in the Search For text field.
  2. Type the replace string (if any) in the Replace With text field.

Be sure to read the section "Multi-File Replacing" later in this chapter if you use the replacement features.

  1. Mark any checkboxes in the middle part of the dialog box that you want to apply to your search.

To learn more about these options, see Search Settings. earlier in this section.

  1. If you want TextWrangler to find only files that do not contain the search string, select the Exclude Matches checkbox.
  2. Drag a folder to the search target area to search its contents, or open the Sources drawer specify the set of files to search.

See See Specifying the Search Set. later in this chapter for more information.

  1. Click one of the buttons along the right side of the dialog box to begin the search.

The table below tells you what each of the buttons does.

This button...

Does this...

Find

Finds the first occurrence of the search string in the frontmost document. (Not available when the multi-file search option is selected.)

Find All

Finds all occurrences of the search string in all the files in the search sources set. TextWrangler displays the results in a search results browser.

Replace

Replaces the current selection in the frontmost document with the replace string. (Not available when the multi-file search option is selected.)

Replace All

Finds all occurrences of the search string in all the files in the search set and replaces them with the replace string.

Don't Find

Preserves the current search and replace strings and settings of the Find & Replace dialog without performing a search.

Cancel

Does not do the search and restores the search and replace strings and settings to their state prior to display of the Find & Replace dialog

Multi-File Search Results

When you perform a multi-file search, TextWrangler displays every occurrence of the search string in the set of files searched using a search results browser.

The items at the top of the window tells you how many matches TextWrangler found in the set of files you specified, as well as specifying whether there were any error conditions or warnings generated during the search. You can display or hide any combination of errors, warnings, and matches, by checking the appropriate options.

The middle panel lists each line that contains the matched text. (Depending on how you have configured TextWrangler, this list may be a Finder-style hierarchical list, where each match in a file is listed under the file's name, or a flat list where each occurrence is simply displayed in order.) Every match is identified by file name and line number.

To see the contents of a file, click a line in the list of matched searches. The part of the file that contains the search string appears in the lower pane. You cannot edit text in a search results browser. To edit a file, double-click the line that contains the match you want to edit, and the specified file will open at the point of the associated match.

After you have opened a file, you can use the Find Again, Replace, Replace All, and Replace & Find Again commands in the Search menu to continue searching it, as if you had chosen a File by File search. See the next section for information on File by File searching.

Note

You can use a search results window as the basis of another multi-file search. See "Specifying the Search Set" below.

Specifying the Search Set

When you turn on the Multi-File Search option, the Sources drawer of the Find & Replace dialog will open. You can also open or close the Sources drawer at any time by clicking the drawer toggle control at the lower left of the dialog. Use this drawer to specify which files and folders TextWrangler should examine when performing a multi-file search.

You can choose multiple sources for a multi-file search, and you can mix different types of sources. Available sources include:

To add a file, folder, or other suitable item to the Sources drawer, click Other in the Find & Replace dialog, and choose the item using the resulting selection sheet. (You can select multiple items to be added.)

To designate any item in the drawer as a search source, click on the box next to its name, or double-click on the name, to add a checkmark. To deselect a search source, click the box next to its name, or double-click its name, to turn off the checkmark. TextWrangler will display a summary of the selected sources in the search items box of the dialog. Here are some common scenarios.

Searching the files in a folder

To search the files in a folder, click on the box next to the folder's name, or double-click its name, in the Sources drawer. If the folder you want to search is not in the Sources drawer, click the Other button at the right of the dialog and pick the folder using the resulting selection sheet.

You can also drag a folder from the Finder directly into the search items box of the Find & Replace dialog to choose it as the source.

Searching all open documents

You can choose any or all open text documents as search sources. This option allows you to search documents that have not yet been saved to a file, or which contain unsaved changes. To choose all open documents, click the box next to the Open Documents item, or double-click on the item, in the Sources drawer.

Searching the files contained in a results browser

If a previous multi-file search found many files that contain your search string, you may want to narrow the search. To search the files listed in any results browser window, click the box next to that browser's name, or double-click on its name, in the Sources drawer. You can also click the box next to the Results Browsers item, or double-click on this item, to search the files listed in all results browsers.

Note

The Choose a Folder dialog will display any packages it encounters as folders (rather than just as single files, the way they appear in the Finder). This allows you to navigate their internal structure just as you would any other folder. Similarly, you can drag a package from the Finder into the path box in the Find & Replace dialog and it will be treated as a true folder rather than as a single file.

Multi-File Search Options

Click the Options button to display the search options sheet.

To search the contents of all subfolders within the folders you choose, select the Search Nested Folders option in the resulting sheet. You can also choose to skip any folders whose names are enclosed in parentheses here by selecting the Skip (...) Folders option, or whether to search the contents of invisible folders by selecting the Search Invisible Folders option.

You can also choose to search only text files or to search all file types. If you have image files or other non-text files in search source folders, it may be a good idea to restrict the search to only text files. This setting is applied in addition to any file filter (see next section) and in fact takes effect before the filter.

To find only files whose contents do not contain the search string, select the Exclude Matches option.

You can further restrict which files from the chosen sources will be searched by applying a file filter. See File Filters. (below) for more details.

File Filters

If you do not want to search every file in the set you selected, but want to include only those that meet certain criteria (such as those created on a certain date, or only those created by TextWrangler and not some other program, or those that are Perl documents), you can use a file filter. Mark the Use File Filter checkbox in the search options sheet and then choose the desired file filter from the Filters pop-up menu. If none of the filters you have already defined meets your needs, you can define a new one, or create a temporary filter.

New Filter

To define a new saved file filter, select New Filter from the pop-up menu. TextWrangler will ask you for a filter name; then the Edit File Filter dialog, below, appears. You can also define new file filters in the File Filters preferences panel.

Note

If you have the Preferences window open, you will not be able to add filters in the Find & Replace dialog. To work around this, close the Preferences window before using the Find & Replace dialog to add new filters.

The Edit File Filter dialog lets you specify multiple criteria that determine whether a given file is selected by the filter. You can choose whether these criteria are exclusive (that is, whether a file must meet every listed test to be selected) or inclusive (that is, whether a file that meets any of the tests is selected) using the Every (AND) and Any (OR) radio buttons at the top of the dialog.

To add a test, click More Criteria. A new row appears in the dialog, as shown below.

The leftmost column lets you specify which attribute of a file you wish to test. TextWrangler lets you test a file's name, the name of its enclosing folder, its creator or type, its creation and modification date (or both date and time), or its Finder label, visibility, or the programming or markup language it is written in. You can also test the content of a file, using the "Contents" criterion.

The middle column lets you choose the test to be applied to the selected attribute. The available options here change depending on what attribute you selected. If you choose Visibility in the first column, for instance, your only choices are whether the file is or is not visible, However, if you choose File Name in the first column, the middle column lets you test to see if the name does or does not exactly match, contain, begin with, or end with a particular string. You can also test file names to see if they match wildcard or Grep patterns.

Note

In wildcard patterns, the asterisk (*) and question mark (?) characters have special meanings. The asterisk matches any number of characters, such that "*.c" matches any file whose name ends with ".c". The question mark matches a single character, so that "foo?" matches "food", fool", "foot", and many other words. Both the asterisk and the question mark can be used anywhere in a wildcard pattern, and any number of either can be used in a single pattern.

Grep patterns, also known as regular expressions, are a powerful method of selecting file names based on classes of text or repeating text. They are covered in great detail in the next chapter.

The right column specifies the match criterion. For example, when filtering by File Name, you type the text you want the name to match, contain, begin with, or end with (or not). When filtering by Language, you choose a supported language from a pop-up menu.

Tip

To quickly set a match criterion, drag a file from the Finder to a field in the Define File Filter dialog. TextWrangler inserts the appropriate attribute from the dragged file, if possible.

Specifying Time and Date Criteria

When using a time or date criterion, you can use the special words below to specify dates and times relative to the current date and time.

Word

Means...

now

current date and time

today

midnight on the current date

yesterday

current date and time minus 24 hours

tomorrow

current date and time plus 24 hours

You can add any number of criteria using the More Criteria button. To delete the last criterion, click the Fewer Criteria button. To select any single criterion for deletion, press the Option key and click on the desired item. To select multiple continuous criteria, press Option-Shift and drag across the items, or to select discontinuous criteria, press Command-Option and click on the desired items.

Click Save to save the file filter and use it for this search. TextWrangler will ask you to name the filter, and it will then appear in the Filters pop-up menu in the Find & Replace dialog (and in the Define File Filter dialog). Click Revert to undo any changes you have made to the filter. (Hold the Option key when you click Revert to skip the confirmation alert.)

Filtering by Name

In order to provide the greatest possible flexibility, TextWrangler offers several different criteria for filtering based on file names

File Name: Tests the complete string corresponding to the file name.

File Name Root: Tests only the "root" portion of the file name. Given a name of the form "foo.txt", the root is the string which occurs before the period, in this case "foo".

File Name Suffix: Tests only the file name suffix. In the above example, the suffix is "txt". Note that the suffix does not include the period.

Temporary Filters

Choose "(current criteria)" from the pop-up menu in the Find & Replace dialog to reuse the last set of criteria applied (either from using a saved filter, or from using the Edit button to define criteria). Thus, you can use filter criteria on the fly, without the need to create and store a throwaway filter.

Editing and Deleting Filters

To edit a file filter you have already defined, choose it from the Filters pop-up menu, change it as desired, and click Save. Since each filter must have a unique name, saving it will replace the old version of the filter. To delete a filter entirely, visit the File Filters panel in the Preferences window. (You can also create or modify filters there.)

Multi-File Replacing

If you want to replace only some occurrences of text in multiple files, you can simply search those files, select the instances you want to change in the search results browser to open the files to those points, and perform the replacements individually. However, TextWrangler can also change all occurrences of a string in a group of files with one command.

Globally replacing text in more than one file works the same as replacing it in a single file. The only possible complication is that, if you make a mistake, it can have much wider consequences. If you are not sure what effect a replace operation will have, test it out on a few sample files, or a copy of your data, first!

To do a multi-file search and replace, replacing all occurrences:

  1. Set up the find and replace strings in the Find & Replace dialog as described in the section "Multi-File Search."
  2. Choose the files to be searched as described in See Specifying the Search Set..
  3. Click Replace All in the Find & Replace dialog, or use its key equivalent of Command-Option-R.

TextWrangler displays the Find & Replace All Matches dialog box:

This is what each of the options does:

This option...

Replaces all occurrences of the search string with the replace string and...

Leave Open

Leaves all the files open so that you can inspect the replacements.

 

If there are many files that contain the search string, TextWrangler may run out of memory.

Save to Disk

Saves each file with the changes.

 

When the Confirm Saves setting is active, you will have an opportunity to approve the changes before TextWrangler saves them to disk. You should not turn this off unless you are sure that the replace operation being done is what you want.

Show Results

Opens a results browser listing each of the files which was changed, and the number of changes in each file.

Quick Search

The Quick Search command performs an incremental search. In other words, it shows the matching text as you type the search string, so you only have to type until you find the text you want.

To use Quick Search:

Choose Quick Search from the Search menu.
  1. Type the string you are looking for in the Quick Search window.

As you type, TextWrangler selects the first occurrence of what you have typed so far.

  1. To find the next occurrence of the matching text, click Find Again, or press the Return or Enter keys.

You can use the Case Sensitive and Find Backwards options to change the way Quick Search looks for text. To clear the most recent word of the search string, you can press Option-Delete, or to clear the entire search string, you may use any of these shortcuts:

You can keep the Quick Search window open all the time and bring it to the front whenever you want to search. Quick Search always searches in the text view of the window immediately behind the Quick Search window.

Search Menu Reference

This section describes all of the commands in the Search menu.

Find

Opens the Find & Replace dialog. You can set the search and replace strings, choose searching options, and, for a multi-file search, specify the set of files to search.

Quick Search

Opens the Quick Search window. You can use this window to interactively search for text strings, as described in the previous section.

Find Again

Uses the previous settings of the Find & Replace dialog to search for the next occurrence of the search string.

Tip

Hold down the Shift key to search in the opposite direction from the current search direction. For example, if you have set the Backwards option in the Find & Replace dialog, holding down Shift while performing a Find Again will search forward in the document.

Find Selection

Uses the selected text as the search string and finds the next occurrence of the selected text. This command is the same as using the Enter Search String command followed by the Find Again command.

Tip

Hold down the Shift key to search in the opposite direction from the current search direction, just as for Find Again. Hold down the Option and Command keys as you double-click on a word to search for the next occurrence of that word.

Enter Search String

Choose the Enter Search String command to enter the currently selected text into the Find & Replace dialog as the search string (without opening the dialog). This command does not perform a search, but only sets the contents of the search string.

Enter Replace String

Choose the Enter Search String command to enter the currently selected text into the Find & Replace dialog as the replace string (without opening the dialog). This command does not perform a search, but only sets the contents of the replace string.

Replace

Replaces the selected text (usually an occurrence of the search string) with the replace string.

Replace All

Replaces every occurrence of the search string in a file with the replace string.

Replace & Find Again

Replaces the selected text with the replace string and searches for the next occurrence of the search string.

Go to Line

When you choose this command, TextWrangler opens the Go To Line dialog box. Type in a line number and the frontmost text window will jump to display that line.

Note

The Go To Line command honors the "Use `Hard' Line Numbering in Soft-Wrapped Text Views" option in the Text Editing preferences panel.

Go to Center Line

Will move the insertion point to the beginning of the middle or center line of the displayed text.

Find Differences

Finds the differences between two files, or all of the files contained in two folders. See the TextWrangler user manual for more details.

Compare Two Front Documents

Performs a Find Differences on the two frontmost text documents.

Compare Against Disk File

Performs a Find Differences between the contents of the front document and the disk file for that same document. This capability makes it easy to locate in-progress changes to a document.

Apply to New

Applies the currently selected difference to the "New" version of two files which are being compared.

Apply to Old

Applies the currently selected difference to the "Old" version of two files which are being compared. See the TextWrangler user manual for more details.

Compare Again

Find the differences between two files, using the same settings that were used in the last time you used the Find Differences command. See the TextWrangler user manual for more details.

Find in Reference

Performs a search using the selected symbol on the Apple Developer Connection web site. If there is no selection, TextWrangler will attempt to determine the symbol name by inspecting the text around the insertion point.

If you wish to modify the URL template which TextWrangler uses to perform the lookup, you may do so by issuing a `defaults write' command in the Terminal as follows:

defaults write com.barebones.textwrangler Services:ADCReferenceSearchTemplate <template>

In the template, use "%@" to indicate where the selected symbol name should be inserted in the lookup. For example, to make Find in Reference use Google, you could specify:

defaults write com.barebones.textwrangler Services:ADCReferenceSearchTemplate "http://www.google.com/search?q=%@&ie=UTF-8"