VS2010: Find in Files

I really debated with myself about writing this post but after an exchange with a friend I decided to go ahead with it.  My team recently upgraded to new laptops with a fresh new image.  When one of my friends remarked that he was glad that the new image contained wingrep I asked why he didn’t just use Visual Studio’s Find in Files feature.  His reason?  He didn’t know about some of its features.  After talking about it a bit he said he’d give it another shot.  Now, several days later he’s using it almost exclusively.

This post is in no way a knock on wingrep.  Wingrep is a great tool and offers some nice features beyond what is available in Visual Studio such as searching ZIP files, searching binary files, a command line interface, and shell integration but generally speaking, I find wingrep to be overkill for most of my searches.  What Find in Files provides something I love – convenience.  Without leaving Visual Studio we can perform fairly powerful searches and have the results displayed in a dockable window within the IDE.

Basic Options

Find and Replace DialogAt first glance the Find and Replace dialog really doesn’t look all that powerful.  In fact, the only things that are immediately available are the Find what and Look in fields, the Expression Builder button is disabled.

Find what – Identifies what you’re searching for.

Look in – Identifies where you want to look.  Depending on what’s open in the IDE the predefined options will vary:

Nothing Open “Loose” Document Solution
Current Document No Yes Yes
All Open Documents No Yes Yes
Current Project No No Yes
Entire Solution No Yes Yes

What may not be apparent (and was the source of my friend’s hesitation about this feature) is that these aren’t the only options, they’re just the pre-defined options.  For quick searches we are free to specify any search path by typing it into the Look in field.  Visual Studio is even kind enough to help by refilling the list with possible matches.  If we want to search more than one location we simply enter additional paths separated by semi-colons.  Visual Studio maintains a history of manually entered search locations for easy selection in future searches but what if we have a common set of paths we want to search regularly and we want to identify that set with a descriptive name?  The Choose Search Folders dialog lets us do just that.

Clicking the Choose Search Folders button brings up the Choose Search Folders dialog.  In this dialog we can specify the folders to include in the set and optionally give the set a name.  We can also modify or delete existing sets.  To name a set simply select the included folders, enter a name, then click Apply.  Once a set is defined it can be selected by name from the Look in list right after the predefined options.

You may also notice that once you enter a search path the Include sub-folders options is enabled.  Most of the time I leave this option selected but if the folder structure is really wide or deep disabling it may give you results a bit faster.

Note: The Replace in Files options are nearly identical to Find in Files.  One additional field is Replace with.  When running Replace in Files Visual Studio will replace matches with the value of this field.  Replace with also has an expression builder but it is only enabled when Regular Expressions are used (see below).

Find Options

Find Options SectionThe first collapsible section is the Find options section.  In this section we can set a few options for how Find in files will behave:

Match case – Toggles whether Find in files will perform case-sensitive matching.  I generally turn this option off out of laziness :)

Match whole word – Toggles whether Find in files will perform whole or partial word matching.  For the vast majority of searches I use whole word matching.

Use – Allows using either wildcards or Regular Expressions in the Find what field.  Selecting either of these options will enable the Expression Builder button to facilitate building the criteria.  I generally don’t use either of these options because most of my searches are pretty simple.  One thing to keep in mind is that Regular Expressions are more powerful than wildcards but they’re also significantly more complex.

Look at these file types – This is where we can filter down the types of files to examine.  Visual Studio provides a few defaults that may vary depending on your project type.  To provide a custom filter simply enter a file wildcard pattern.  It is also possible to specify multiple filters by separating them with a semicolon.

Result Options

Result Options SectionThe second collapsible section is the Result options section and allows us to control how results are handled:

List results in – Indicates which of two search windows will show the search results.  Most of the time I leave Find results 1 window selected but occasionally it’s helpful to run two related searches side-by-side.

Note: VS2010 Express does not include the List results in option.

Display file names only – Normally the search results include both the file name and the line containing the match.  Disabling this option will exclude the match information from the results.  Note that this option only applies to Find in Files and will be disabled for Replace in Files.

Keep modified files open after Replace All – When Visual Studio finds a match with Replace in Files it normally opens the file to make the change then closes it.  This behavior can help keep the number of open files at a manageable level but it also removes the possibility of undoing the change (source control revert excluded, of course).  Enabling this option will make Visual Studio skip the close step.  Note that this option only applies to Replace in Files and will be disabled for Find in Files.

Find Results Windows

Once you’ve completed your search it would be nice to do something with the results.  The Find Results windows lists all matches according to the options entered in the Find and Replace dialog.  The window makes it easy to get to individual matches by either double clicking a match or selecting a match and pressing the Go to the location on the current line button or using the context menu option.  You can also navigate back and forth between matches.

If you find that your search is taking too long to complete you can stop it by clicking the Stop Search button, selecting the context menu option, or reopening the Find and Replace dialog and clicking the Stop Find button there. I’ve noticed that it can take some time for the search to complete writing results to the window but it will stop eventually.

The only thing I wish that the Find Results windows would provide is a direct way to save the search results.  Unfortunately saving the results requires selecting all of the results, copying them, and pasting them into a new file.  It seems like such an obvious thing to include but at least there’s a workaround.

Final Thoughts

Despite the couple of limitations I find Find in Files to be a capable tool for my file searching needs.  If you’re not using it like my friend give it a shot.

Advertisements