Search, Replace, and Regular Expressions


If you can't easily search for and replace text in your editor, you're probably not working with a very good editor. It's great to be able to type in code easily, but finding and fixing code problems are tasks you must do often as a programmer, and the search functions built into the editor are what make that work easy (or difficult). Visual Studio offers a number of ways to search in the Text Editor, and it offers a powerful regular expressions facility that allows you to perform extremely complex searches.

First let's take a look at the named commands and shortcuts associated with the Find and Replace operations in Visual Studio. These might be familiar to you because they're mapped to the Common User Accessibility (CUA) shortcuts that you might have used in Windows or in Office.

To bring up the Find and Replace dialog box, press Ctrl+F. You can click the plus button to expand the Find Options area in the dialog box, as shown in Figure 3-14. The options in this dialog box are fairly straightforward. You can specify case (Match Case), whole-word searches (Match Whole Word), and the direction of your search (Search Up). An interesting option on this dialog box is Search Hidden Text. When this check box is left cleared, text that is hidden in a collapsed node of an outline won't be searched.

image from book
Figure 3-14: The Find and Replace dialog box in Find mode

The Find and Replace dialog box in Visual Studio is actually a tool window, so you can dock it in the IDE or even toggle the Dockable option on the shortcut menu available from the title bar to make it a tabbed window in the center of the IDE.

You can dock the Find and Replace dialog box by dragging it to a side of the IDE. But making it a floating dialog box might be preferable because it's easy to accidentally dock the window when you're trying to get it out of your way. You can turn off docking by choosing Floating on the shortcut menu for the dialog box. (Right-click on the Close button on a dialog box to open its shortcut menu.) Doing so will let you drag the Find dialog box around the screen with impunity.

You can bring up the Find and Replace dialog box in Quick Replace mode by pressing Ctrl+H. This dialog box contains a combo box for the Replace With text. Using the Find and Replace dialog box is straightforward. Especially handy are the Search options, which let you choose between the current document, all open documents, the current project, and specific selections or blocks in the editor.

Using the Find In Files option in the Find and Replace dialog box (Ctrl+Shift+F for Find or Ctrl+Shift+H for Replace), shown in Figure 3-15, make it fairly easy to find text within a project or a directory structure. Setting up these file searches takes a little more work than performing a standard Find command. You can type the path you want to search in the Look In field or you can click the button, which opens the Look In dialog box. You can use this dialog box to narrow your search to your project or to a directory structure on your machine. The output from the Find In Files search is sent to the Find Results window by default.

image from book
Figure 3-15: Find In Files from the Find and Replace dialog box

The Visual Studio Find and Replace shortcuts are listed in Table 3-6.

Table 3-6: Common Search Shortcuts

Command

Keystroke

Named Command

Find

Ctrl+F

Edit.QuickFind

Replace

Ctrl+H

Edit.QuickReplace

Find In Files

Ctrl+Shift+F

Edit.FindinFiles

Replace In Files

Ctrl+Shift+H

File.ReplaceinFiles

Incremental Searching

Incremental searching is a feature of Visual Studio that's a real timesaver. An incremental search is performed one character at a time, matching each word in the search string from the position of the cursor. You can start an incremental search by pressing Ctrl+I. You'll see the mouse pointer transform into a down arrow like the one shown in Figure 3-16.

image from book
Figure 3-16: Starting an incremental search by pressing Ctrl+I

With the down arrow showing, start typing the term you want to search for. As you type, words that match the letters are matched starting from the top. When you've completed the pattern you want to search for, press Ctrl+I again to move to the next match. You can continue to press Ctrl+I until you reach the end of the document to match every instance of the term you're searching for. If you want to search upward, just press Ctrl+Shift+I. It works in the same way. You can exit the incremental search by pressing Enter or Esc.




Working with Microsoft Visual Studio 2005
Working with Microsoft Visual Studio 2005
ISBN: 0735623155
EAN: 2147483647
Year: 2006
Pages: 100

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net