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.
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.
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.
In VS Code...
The search query will be prefilled with the path under "files to include".
Press Ctrl + Shift + F
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
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"
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.
This action is not bound to a key by default, to bind it do this:
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
# | 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 |
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.
filesExplorer.findInFolder
command.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:
That's why I suggest changing the shortcut to CTRL+SHIFT+F with some When Expression magic:
workbench.action.findInFiles
and set its When Expression to !filesExplorerFocus
filesExplorer.findInFolder
and change its keybinding to CTRL+SHIFT+F{
"key": "ctrl+shift+f",
"command": "workbench.action.findInFiles",
"when": "!filesExplorerFocus"
},
{
"key": "ctrl+shift+f",
"command": "filesExplorer.findInFolder",
"when": "explorerViewletVisible && filesExplorerFocus && !inputFocus"
}
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.
If you have a directory open in VSCode, and want to search a subdirectory, then either:
files to include
field enter the path with a leading ./
,or
Find in Folder...
option.Enter Search Keyword in search (CTRL + SHIFT + F)
Exclude unwanted folder's/files by using exclude option (!)
ex: !Folder/File*
Hit Enter
Search results gives you desired result
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
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.
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.
Select your folder, Press ⌥ + ⇧ + F Don't know about windows but this works for mac :)
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
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
}
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.