105

Often when I want to search through my code in Visual Studio, I know the thing I'm looking for is in some C# code. However, as I've used the same variable name in a JavaScript file, I have to wade through all those search results too. This gets even worse when the text I'm looking for is also used in a third-party JavaScript library that we've brought into the project: this can result in hundreds of search results.

To compound things, our designers include HTML mock-ups of the pages in the same project, so I often find I'm hitting loads of search results in there too.

I guess what I really want is to see results in my .cs, .aspx, and .ascx files, but not .js or .htm.

Is there any way to do any of the following:

  • Search only in files of a particular type (search only .cs files).
  • Search only in files of any of a given set of types (search only .cs, .aspx and .ascx files).
  • Search in all file types except a particular type or types (search everything except .js).

I suspect not, in which case is there any crafty way of working around this?

user247702
  • 23,641
  • 15
  • 110
  • 157
teedyay
  • 23,293
  • 19
  • 66
  • 73
  • 5
    The last option doesn't seem to be available- the file-type selector is only additive, you can specify what to include, but not what to exclude. – nicodemus13 Sep 06 '12 at 16:17
  • 1
    We can all vote for the feature in Visual Studio here: https://visualstudio.uservoice.com/forums/121579-visual-studio-2015/suggestions/2907413-add-exclude-files-to-the-look-in-options-under Doooooo it! – Polshgiant May 16 '16 at 00:34

6 Answers6

95

In the Find in Files dialog (Ctrl+Shift+F), there should be a field called Find Options. You should be able to enter the extensions of fields you want to search in a field in this dialog:

*.cs; *.aspx; *.ascx; 
juFo
  • 17,849
  • 10
  • 105
  • 142
Matthew Jones
  • 25,644
  • 17
  • 102
  • 155
  • 3
    +1 the Find-in-Files dialog saves your recent extension choices, so it is easy to choose what set of extensions you want to search. – Gabe Moothart Nov 17 '09 at 16:02
  • 1
    This also works in the replace in files dialog, and replace has a few extra buttons, notably "Skip File," which allows you save even more time. – Travis Jan 29 '14 at 14:57
31

Instead of Ctrl + F, I think it is Ctrl + Shift + F which gives you the choice to specify file types, you wish to look into.

shahkalpesh
  • 33,172
  • 3
  • 63
  • 88
  • 2
    Correct on the Ctrl + shift + F. It also does a find all, which is nicer because with the find, it stops once it finds something, so you have to babysit it to find everything you want. – Andrew Sep 06 '12 at 21:40
  • 1
    You can do find all with the regular find menu also, it's just hidden under the dropdown to the right of the "find" arrow – alexanderbird Jul 27 '16 at 15:09
9

enter image description here

You can choose file types from default or type your own. Regular expressions available for complex search.

Altaf Patel
  • 1,351
  • 1
  • 17
  • 28
7

Another way to limit file searches is by only choosing certain folder sets.

Setting folder sets for search

Even Mien
  • 44,393
  • 43
  • 115
  • 119
  • That's a great option! What might also help is to change "Look in:" to "All open documents" and then open just the documents you want to search. – Matt Oct 19 '17 at 13:51
  • 1
    Dude! Great option. Been using VS since the dawn of time and never saw that before. – Wade Hatler Aug 10 '18 at 00:14
6

I like to exclude js files by using the following search: *.a*;*.cs*;

Most of the times, I end up searching for stuff in aspx, cs, cshtml files so this is quite helpful. Notice how I use *.cs* instead of *.c* since the latter would select jquery custom files such as jquery.custom1234.js (which I usually use in most of my projects), of course if you don't you could just use *.c*.

arviman
  • 5,087
  • 41
  • 48
  • I found this one to be the best, although it does include .css files, which may not be desirable. I also like to add two more for Web.Config's and Site.Master (if that exists): – arserbin3 Aug 08 '13 at 19:22
  • 4
    to omit css files: `*.a*;*.cs;*.csh*;*.Config;*.Master;` – arserbin3 Aug 08 '13 at 19:24
1

In the Find dialog box, go to "find options->Look at these file types".

Type in you own string, eg, *.cs, *.aspx, *.ascx. The click the "find all" button.

Mike Atlas
  • 8,193
  • 4
  • 46
  • 62