653

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.

Alexander Abakumov
  • 13,617
  • 16
  • 88
  • 129
user2465134
  • 8,793
  • 5
  • 32
  • 46

20 Answers20

654

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.

Justin Rice
  • 1,111
  • 12
  • 13
DaveShaw
  • 52,123
  • 16
  • 112
  • 141
  • 4
    Once you have a match, you Ctrl (Mac: 'Cmd') click on a keyword in the Search Results to open that file for side-by-side editing, jumping to that location in the file. – bitsand Dec 13 '16 at 16:25
  • 138
    The problem is that when you have an directory open in Visual Studio Code, with dozens of subdirectories, you often want to search in a single directory. Find in files isn't at all about doing that. (This is a useful answer to a completely different question, of course.) . @JesperWilfing 's answer (right click on folder, find in folder) is better. – James Moore Sep 05 '17 at 16:25
  • Does this search subdirectories of the folder, too? – Adam Miller Sep 26 '17 at 15:15
  • @JamesMoore the answer to your question is below, by Jesper. It works. – klewis Mar 01 '18 at 21:01
  • 4
    Is there anything like in Notepad++, where you can just specify the directory to search in, without having to open it? – Do-do-new Apr 07 '18 at 11:24
  • 18
    This doesn’t answer the question. How do you search only within given a folder? – Zaqx Jun 14 '18 at 18:16
  • 18
    Based upon another answer, if you right click on a folder and choose "Find in folder... (Shift+Alt+F)", you'll see the format required for searching a particular directory. It looks like you start at the workspace root. e.g. my workspace is `WebInterface`, and my folder include was `./WebInterface/cli/src/[folder]` – Tyler StandishMan Mar 29 '19 at 20:32
  • 1
    Or even simpler, click on the folder in the files sidebar, then hit SHIFT-ALT-F (at least in my setup). – berliner Jun 19 '19 at 15:55
  • 1
    Downvoted, this answer doesn't say how to search within a specific folder, as mentioned by other comments. Select a folder -> Alt+Shift+F is the answer. – Yngvar Kristiansen Nov 25 '19 at 13:56
  • In addition to @TylerStandishMan's comment: If you do not get the correct context menu on you project root folder, try opening it's parent (the folder containing your project root folder) FIle-> open folder ... – Robin Nov 14 '20 at 17:46
  • 1
    @TylerStandishMan This answer should have been the main one. Thank you. – Yatix Feb 02 '21 at 10:20
  • For me the search wasn`t looking through in all folders in the opened directory within VSCode. Then I noticed a feature was turned on by mistake which I had to turn off and it worked. Its the 'Search Only in Open Editors' with a open book symbol, you can locate this button to the right of 'files to include' field. – techloris_109 May 12 '21 at 12:56
  • how do you make it go away after you've done searching? – mLstudent33 Jun 12 '21 at 21:55
  • This does not answer the question. This shouldn't have been the accepted answer. The answer below provides a better and easier solution. – gourabix Sep 07 '21 at 07:36
592

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".

Pat Myron
  • 4,437
  • 2
  • 20
  • 39
Jesper Wilfing
  • 11,157
  • 5
  • 29
  • 32
  • 7
    "Find in folder" doesn't work on automatically ignored folders like `node_modules` which is sometimes needed for JavaScript developers. "VS Code excludes some folders by default to reduce the number of search results that you are not interested in. Open settings to change these rules under the files.exclude and search.exclude section." from https://code.visualstudio.com/docs/editor/codebasics#_advanced-search-options – Șerban Ghiță Mar 21 '19 at 13:43
  • 1
    Also kinda tricky if repo root isn't the actual project root, in which case VSCode always defaults to searching entire repo, but there's no way to restrict it to the current project (the folder open in VSCode) – Svend Apr 15 '19 at 08:39
  • This does have the unfortunate side effect of clobbering whatever is already in the 'files to include' field, and no way I could see to restore my usual filter. – greg7gkb May 21 '20 at 04:39
  • Note: Does not work on top Folder! I had to open the Directory containing my project root folder to be able to right click it. – Robin Nov 14 '20 at 17:42
  • This does not work for me. I can't find the words "function" or "return" in all my node_modules. I treid everything I could imagine. Changed my .gitignore. Nothing. Changed my init path. Nothing. Not a single match on anything in the alphabeth. What is wrong here? – kiroshiro Jan 27 '22 at 14:26
  • @Svend Try "File" > "Open Folder", if I understand correctly your program. This works for me to only search the current project folder that isn't VSCode's default path. Do note that this will close all your opened files in the editors. – CreativiTimothy Jul 07 '23 at 18:12
82
  • Press Ctrl + Shift + F

    enter image description here

  • Click on 3 dots under search box.

  • Type your query in search box

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

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

Ivailo Bardarov
  • 3,775
  • 1
  • 28
  • 25
Sajeer Babu
  • 1,103
  • 1
  • 9
  • 13
44

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

Meryan
  • 1,285
  • 12
  • 25
  • 2
    `./src/**/*.html` will ignore `.src/child/grandchild/foo.html`. How could we update the regex to include grandchildren folders of any depth? – manymanymore Feb 12 '21 at 11:01
  • You seem to be missing the first forward slash if you are targeting your project rooted ./src/ folder I have folders/files like ./src/components/BMS/BookmarkComp.html and it finds the file if I was to search some html tag. – Meryan Feb 12 '21 at 17:30
  • The leading slash change nothing for me. – manymanymore Feb 15 '21 at 14:02
18

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

Zymawy
  • 1,511
  • 16
  • 15
16

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.

Sampath
  • 63,341
  • 64
  • 307
  • 441
Milan Leškanič
  • 1,229
  • 9
  • 11
12

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

user2465134
  • 8,793
  • 5
  • 32
  • 46
11

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
fotcorn
  • 311
  • 3
  • 8
11

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

Blueberry
  • 2,211
  • 3
  • 19
  • 33
10

VS Code Default Actions

# Command Shortcut Command ID
1. View: Show Explorer CTRL+SHIFT+E workbench.view.explorer
2. Focus current directory (Left arrow)
3. Right-click / Find in Folder... ALT+SHIFT+F filesExplorer.findInFolder

Suggestion

The default commands are too slow, even when using keyboard shortcuts (it takes seven key presses).

First, let's get rid of the left arrow press. By default, the filesExplorer.findInFolder command requires a folder to be focused. However, it works perfectly with a file - by searching its parent folder.

  1. Preferences: Open Keyboard Shortcuts (CTRL+K, CTRL+S)
  2. Search for the filesExplorer.findInFolder command.
  3. Right-click / Change When Expression (CTRL+K, CTRL+E)
  4. Remove the explorerResourceIsFolder requirement from When Expression. The result should be: explorerViewletVisible && filesExplorerFocus && !inputFocus

Now, the Find in Folder command works on files too, so the shortcut combo is down by one key. The next is ALT+SHIFT+F, which is annoying for two reasons:

  1. You have to remember when to press CTRL but when ALT, and it is easy to mess up.
  2. Some extensions use ALT+SHIFT+F for code auto-formatting - and I doubt that this a side effect you wish.

That's why I suggest changing the shortcut to CTRL+SHIFT+F with some When Expression magic:

  1. Preferences: Open Keyboard Shortcuts (CTRL+K, CTRL+S)
  2. Search for workbench.action.findInFiles and set its When Expression to !filesExplorerFocus
  3. Search again for filesExplorer.findInFolder and change its keybinding to CTRL+SHIFT+F

Result

  1. CTRL+SHIFT+F triggers "Find in Folder" when File Explorer is focused, and the default "Find in Files" elsewhere.
  2. You have to press either CTRL+SHIFT+F to "Find in Files", or CTRL+SHIFT+E, CTRL+SHIFT+F to "Find in Folder". Easy to remember. Moreover, the latter can be optimized by keeping CTRL+SHIFT pressed while pressing E, then F

User Config

{
  "key": "ctrl+shift+f",
  "command": "workbench.action.findInFiles",
  "when": "!filesExplorerFocus"
},
{
  "key": "ctrl+shift+f",
  "command": "filesExplorer.findInFolder",
  "when": "explorerViewletVisible && filesExplorerFocus && !inputFocus"
}

Warning

The filesExplorer.findInFolder command overwrites the "files to include" field. For example, you could have a file search pattern *.c,*.cpp,*.h,*.hpp, which is now overwritten by the folder path. But you can always press (UpArrow) in the field to cycle through the history and restore the previous pattern.

PooSH
  • 601
  • 4
  • 11
6

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/

BuZZ-dEE
  • 6,075
  • 12
  • 66
  • 96
Dany
  • 339
  • 1
  • 3
  • 15
5

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.
robocat
  • 5,293
  • 48
  • 65
3
  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

3

There is another option starting with VSCode version 1.73 (Oct. 2022)

Add "Find in Folder" to context menu in Search Tree

Implemented by PR 163597

https://user-images.githubusercontent.com/31675041/192396545-2bae6f64-7315-42d6-9385-61a44528d0a7.png

musicformellons
  • 12,283
  • 4
  • 51
  • 86
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
2

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.

2

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.

human3
  • 31
  • 3
  • Thanks. I thought the search in files feature was broken until I noticed the little book icon and hovered over it. – R.M. Buda Feb 17 '23 at 00:53
1

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

0

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

Dipan Mandal
  • 175
  • 2
  • 6
0

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
}
0

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.