Searching Documents

The text editor window provides an extensive search-and-replace capability. Two primary methods of searching are supported: Quick Find (ideal for finding text fragments within the current document or set of open documents) and Search in Files (ideal for finding text in a file residing anywhere within a folder structure). All these search mechanisms are triggered through the Edit, Find and Replace menu (and more commonly, through their hotkeys).

Each search mode is also capable of doing replacement operations. That makes a total of four different functions:

Image Quick Find

Image Quick Replace

Image Find in Files

Image Replace in Files

Let’s take a closer look at each of the two search-and-replace modes individually.

Quick Find/Quick Replace

Figure 6.16 shows the Quick Find window in its native position to the top right of the text editor window. Its minimalist UI allows you to quickly start your search process by typing directly into the search box.

Image

FIGURE 6.16 The Quick Find tool.

The search box drop-down holds the last 20 strings used in a find operation, making it easy to reuse a previous search. Just select the string from the list.

Fine-Tuning Your Search

The Quick Find window also hosts options for fine-tuning your search via a small toolbar at the bottom edge of the Find window:

Image Match Case causes the search to be executed against the exact case you used in the Find What drop-down.

Image Match Whole Words forces the search to match on the entire string as entered in the Find What drop-down.

Image Use Regular Expression changes how the search engine performs matching on the string you have entered into the search box. A standard search does a character match for the target string. By selecting this option, however, you can instead use a full-blown regular expression to perform even more intricate searches. For instance, checking this box and then entering [0-9]{9} would return all matches for a nine-digit number.

Image Scope alters the area that the search operation functions over. Your selections in the drop-down here include the currently selected block of code, the current document, all open documents, all documents in the current project, and all documents in the entire solution.


Note

Although a complete discussion of regular expressions is beyond the scope of this book, you should note that the Replace With box is capable of supporting tagged expressions. For more information on how you might use this to your advantage during replace operations, consult a regular expression reference manual and look at the MSDN Regular Expressions help topic for Visual Studio.


Finding Search Results

After you have specified all the criteria for your search, the right-arrow button (or F3 as a shortcut) to the right of the search box will find the next match to the search or Shift+Ctrl F3 for the previous match. Any matches within the scope specified are highlighted for you within the document and will be scrolled into view. Subsequent clicks on the Find Next button move to the next match until no more matches are found.

The Find Next button also functions as a drop-down that lets you perform a Find Previous or a Find All action.

Replacing Text

The arrow to the left of the search box will expand the Quick Find window to show a Replace text box: Type in the replacement string here, and then use one of the two buttons to the right of the box to replace either the next matching string or all matching strings with the new text (see Figure 6.17).

Image

FIGURE 6.17 Expanding the Quick Find window to do a replacement.

Note that any replacements you make can always be undone via the Undo command under the Edit menu.

Find in Files/Replace in Files

Figure 6.18 shows the Find in Files tool. This tool is similar to Quick Find, with two minor additions. You still have to specify the “what” (search string) and the “where” (scope) components of the search. And you still can fine-tune your search using regular expressions and by matching on case or whole word. But you also have the option of creating a custom search scope. The way that the search results are displayed is via a separate window instead of within the code/text editor window.

Image

FIGURE 6.18 Find in Files.

Let’s look at these two differences in turn.

Building Search Folder Sets

Clicking the ellipses button to the right of the Look In drop-down launches a dialog box that allows you to build up a set of directories as the scope of the search. You can name this folder set and even set the search order for the directories. Figure 6.19 captures this dialog box as a search set called ClassLibCode is built. You can see that two directories have been added to the set. You can add more by simply browsing to the folder with the Available Folders control and adding them to the Selected Folders list.

Image

FIGURE 6.19 Building a Search Folder set.

The Find Results Window

With Quick Find, the search results are highlighted (or bookmarked) right within the text editor window. The Find in Files mode displays its search results in a separate, dedicated Find Results window (see Figure 6.20). You can redirect the output to one of two results windows by selecting either the Find Results 1 Window or Find Results 2 Window option at the bottom of the Find and Replace dialog box. The two windows are identical; two options provided here allow you to keep different search results separate and avoid the confusion that the commingling of matches would cause if you were constrained to just one output window.

Image

FIGURE 6.20 The Find Results window.

In Figure 6.20, you see the results of a simple search conducted across all the files in a solution. The contents of the Find Results window provide the following information:

Image A description of the search performed (for example, Find all “Geo,” Subfolders, Find Results 1, “Entire Solution”).

Image The matches returned from the search. Match information includes the file path and name, the line number within the file, and a verbatim repeat of the line of code containing the match.

Image A summary of the find results, including the number of matching lines of code, the number of files containing matches, and the total number of files searched.

Double-clicking one of the results lines in the window jogs the cursor location directly to the matching line within the editor. Note that this window has a toolbar. From left to right, the buttons on this toolbar allow you to do the following:

Image Jump to the matched line of code within the text editor. (First place your cursor on the match inside the Find Results window and then click the Go to the Location of the Current Line button.)

Image Move back and forth through the list of matches. Each matched item is highlighted in the Find Results window and in the Text Editor window.

Image Clear the Find Results window.

Image Clear the selected result window.

Image Cancel any ongoing searches.

Replacing in Files

Just as with Quick Find, there is a way to perform replacements using the Find in Files tool. This mode is entered by clicking the Replace in Files button at the top of the search window (see Figure 6.21).

Image

FIGURE 6.21 Replace in Files mode.

We’ve already covered the Replace and Replace All functions. Each file that matches the search phrase is opened in a separate text editor window, and the replacements are made directly in that window. If you’re performing a Replace All, the replacements are made and then saved directly into the containing file. You also have the option, via the Keep Modified Files Open After Replace All check box, to have Visual Studio keep any files touched open inside their respective text editors. This allows you to selectively save or discard the replacements as you see fit.

You can elect to skip files during the search-and-replace process by using the Skip File button. This button is available only if more than one file has been selected as part of the search scope. Clicking this button tells the search engine to skip the current file being processed and continue with the next in-scope file.

Incremental Search

Incremental Search is a special case function that works with the Quick Find window. With a text editor open, select Edit, Advanced, Incremental Search (or press Ctrl+I). While Incremental Search is active, you will see the Quick Find window and a special visual pointer cue composed of binoculars and a down arrow. If you start typing a search string, character by character, the first match found is highlighted within the text editor window. With each successive character, the search string is altered and the search itself is re-executed. The current search string and search scope is displayed on the Visual Studio status bar and brought to view in the Editor. Figure 6.22 illustrates an Incremental Search in progress; the characters MESS have been entered, and you can see the first match flagged within the text editor.

Image

FIGURE 6.22 Incremental Search.

By default, the search function works from the top of the document to the bottom and from left to right. You can reverse the direction of the search by using the Ctrl+Shift+I key combination.

To jump to the next match within the document, use the Ctrl+I key combination.

Clicking anywhere within the document or pressing the Esc key cancels the Incremental Search.


Note

Incremental Searches are always performed in a manner that is not case sensitive and will always match on substrings.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset