673

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.

1

22 Answers 22

668

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.

14
  • 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, 2016 at 16:25
  • 143
    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. Sep 5, 2017 at 16:25
  • 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 7, 2018 at 11:24
  • 19
    This doesn’t answer the question. How do you search only within given a folder?
    – Zaqx
    Jun 14, 2018 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] Mar 29, 2019 at 20:32
604

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

6
  • 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 code.visualstudio.com/docs/editor/… Mar 21, 2019 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, 2019 at 8: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, 2020 at 4: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, 2020 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, 2022 at 14:26
83
  • 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

4
  • 2
    I have been looking for this feature forever in VSCode, and it has been right under my nose the whole time.
    – NOCARRIER
    Apr 26, 2019 at 20:17
  • thank you so much, I was looking for how to find in a particular folder. So far I was using sublime for finding in particular folder. Feb 6, 2020 at 4:13
  • Was searching for some extension or something to do this but this feature was already present. Thank you. Jan 13, 2021 at 18:16
  • 1
    I was missing the dot . before my folder, makes perfect sense! Sep 30, 2021 at 18:31
45

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

3
  • 2
    ./src/**/*.html will ignore .src/child/grandchild/foo.html. How could we update the regex to include grandchildren folders of any depth? Feb 12, 2021 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, 2021 at 17:30
  • The leading slash change nothing for me. Feb 15, 2021 at 14:02
18

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

1
  • 8
    This only matches on filenames, not text inside the files. Jun 26, 2018 at 7:41
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.

12

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

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

11

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.

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/

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

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

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

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.

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

1
  • 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 at 0:53
1

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

1
  • The same on Mac: Option + Shift + F Apr 14, 2021 at 9:24
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

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.

0

You need to uncheck button "Search only in Open Editors" enter image description here

0

The @PooSH solution has significantly addressed the issue for me. However, I'm also proposing an advanced idea here by utilizing the command alias extension. To maps find in folder to be executable via the command palette, which is immensely helpful for someone like me with limited brain memory capacity.

Not the answer you're looking for? Browse other questions tagged or ask your own question.