How to search for text in all files in a directory?

Visual Studio-Code

Visual Studio-Code Problem Overview


Is there a way to search for text in all files in a directory using VS Code?

I.e., if I type find this in my search, it will search through all the files in the current directory and return the files that matched.

Visual Studio-Code Solutions


Solution 1 - Visual Studio-Code

You can do Edit, Find in Files (or Ctrl+Shift+F - default key binding, Cmd+Shift+F on MacOS) to search the Currently open Folder.

There is an ellipsis on the dialog where you can include/exclude files, and options in the search box for matching case/word and using Regex.

Solution 2 - Visual Studio-Code

In VS Code...

  1. Go to Explorer (Ctrl + Shift + E)
  2. Right click on your favorite folder
  3. Select "Find in folder" (Alt + Shift + F)

The search query will be prefilled with the path under "files to include".

Solution 3 - Visual Studio-Code

  • Press Ctrl + Shift + F

    enter image description here

  • Click on 3 dots under search box.

  • Type your query in search box

  • Type ./FOLDERNAME in files to include box and click Enter

Alternative way to this is, Right click on folder and select Find in Folder

Solution 4 - Visual Studio-Code

What is NOT so obvious is that you can use the following pattern to recursively search

./src/**/*.html

so perhaps leave the following as the default for most of your typical searches to remind that there is such a thing

./src/**/

For example I was after an attribute for left-right justify/docking content, I could not remember except "start" so I did the following search which reveals to me "item-start"

enter image description here

This fixed my layout to enter image description here

Instead of enter image description here

Here is where "item-sart" goes in the template. enter image description here

Solution 5 - Visual Studio-Code

Ctrl + P (Win, Linux), Cmd + P (Mac) – Quick open, Go to file

Solution 6 - Visual Studio-Code

I think these official guide should work for your case.

> VS Code allows you to quickly search over all files in the > currently-opened folder. Press Ctrl+Shift+F and enter in your search > term. Search results are grouped into files containing the search > term, with an indication of the hits in each file and its location. > Expand a file to see a preview of all of the hits within that file. > Then single-click on one of the hits to view it in the editor.

Solution 7 - Visual Studio-Code

A simple answer is to click the magnifying glass on the left side bar

Solution 8 - Visual Studio-Code

This action is not bound to a key by default, to bind it do this:

  1. File > Preferences > Keyboard Shortcuts (Ctrl+K, Ctrl+S)
  2. Search for "find folder"
  3. Press the + icon on the left of "filesExplorer.findInFolder" search result
  4. Enter your desired key combination

Solution 9 - Visual Studio-Code

To add to the above, if you want to search within the selected folder, right click on the folder and click "Find in Folder" or default key binding:

Alt+Shift+F

As already mentioned, to search all folders in your project, click Edit > "Find in Files" or:

Ctrl+Shift+F

Solution 10 - Visual Studio-Code

And by the way for you fellow googlers for selecting multiple folders in the search input you separate your directories with a comma. Works both for exclude and include

Example: ./src/public/,src/components/

Solution 11 - Visual Studio-Code

If you have a directory open in VSCode, and want to search a subdirectory, then either:

  • ctrl-shift-F then in the files to include field enter the path with a leading ./,

or

  • ctrl-shift-E to open the Explorer, right click the directory you want to search, and select the Find in Folder... option.

Solution 12 - Visual Studio-Code

  1. Enter Search Keyword in search (CTRL + SHIFT + F)

  2. Exclude unwanted folder's/files by using exclude option (!)

    ex: !Folder/File*

  3. Hit Enter

Search results gives you desired result

Solution 13 - Visual Studio-Code

In order to search only in one folder, you have to click on it and press Alt + Shift + F.

When you use Ctrl, VS Code looks in all project.

Solution 14 - Visual Studio-Code

Select your folder, Press + + F Don't know about windows but this works for mac :)

Solution 15 - Visual Studio-Code

Search across files - Press Ctrl+Shift+F

Find - Press Ctrl+F

Find and Replace - Ctrl+H

For basic editing options follow this link - https://code.visualstudio.com/docs/editor/codebasics

>Note : For mac the Ctrl represents the command button

Solution 16 - Visual Studio-Code

Just in case you wanted to use PowerShell to search for and then open all text files in your current directory:

foreach($file in $(dir -recurse -include *.txt))
{
    code $file
}

You could also be a little more specific or even change the file type:

foreach($file in $(dir <specificDir> -recurse -include *.<anyExtension>))
{
    code $file
}

Solution 17 - Visual Studio-Code

Be very aware that if you click on the 'book' icon to the right of the 'files to include' field, that it will toggle between searching all files in the 'files to include' field and searching only files in opened editors (matching the 'files to include' field).

This may only be obvious if you know to read the text at the bottom of the search dialog, which will change between something like the following, as you toggle the book icon: > No results found in './project_dir/sub_dir' - Search again in all files - Learn More

and this: > No results found in open editors matching './project_dir/sub_dir' - Search again in all files - Learn More

This can really mess you up if you think that you have found all occurrences of something, but you are only looking at all occurrences in the opened files.

Solution 18 - Visual Studio-Code

If you want to search your current directory/project directory, but not a single directory, just type * in the "files to include" on the search tab. (* meaning all files)

Also it's worth to note when you have the "search.exclude" config having some directories this config is more prioritized than the search. Thus if I have node_modules in the "search.exclude", even * does not show files inside ./node_modules, so if you want to explicitly include search-excluded dirs create a local settings.json in ./vscode and overwrite the config.

Solution 19 - Visual Studio-Code

Just click on the magnifier and search the string....

enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2465134View Question on Stackoverflow
Solution 1 - Visual Studio-CodeDaveShawView Answer on Stackoverflow
Solution 2 - Visual Studio-CodeJesper WilfingView Answer on Stackoverflow
Solution 3 - Visual Studio-CodeSajeer BabuView Answer on Stackoverflow
Solution 4 - Visual Studio-CodeMeryanView Answer on Stackoverflow
Solution 5 - Visual Studio-CodeZymawyView Answer on Stackoverflow
Solution 6 - Visual Studio-CodeMilan LeškaničView Answer on Stackoverflow
Solution 7 - Visual Studio-Codeuser2465134View Answer on Stackoverflow
Solution 8 - Visual Studio-CodefotcornView Answer on Stackoverflow
Solution 9 - Visual Studio-CodeBlueberryView Answer on Stackoverflow
Solution 10 - Visual Studio-CodeDanyView Answer on Stackoverflow
Solution 11 - Visual Studio-CoderobocatView Answer on Stackoverflow
Solution 12 - Visual Studio-Codeuser6470139View Answer on Stackoverflow
Solution 13 - Visual Studio-CodeSardorkhuja TukhtakhodjayevView Answer on Stackoverflow
Solution 14 - Visual Studio-CodeAmin Mohamed AjaniView Answer on Stackoverflow
Solution 15 - Visual Studio-CodeDipan MandalView Answer on Stackoverflow
Solution 16 - Visual Studio-CodeJonathan BeldenView Answer on Stackoverflow
Solution 17 - Visual Studio-Codehuman3View Answer on Stackoverflow
Solution 18 - Visual Studio-CodeLimeView Answer on Stackoverflow
Solution 19 - Visual Studio-CodeJayView Answer on Stackoverflow