ChatGPT解决这个技术问题 Extra ChatGPT

Choose folders to be ignored during search in VS Code

Right now when I use +O to search for files, the fuzzy matching appears to operate over all files in the current project. Unfortunately, this includes a number of files from build and vendor directories. So, for instance, if I want to search for all JavaScript files and do +O and type .js in, the file and symbol results include around 1500 hits and all of them except the two ones are complete noise.

Is there a way to specify certain directories to be ignored for purpose of search?

Hmm, wait a minute, in 2019 ⌘O means "Open File...", and I doubt VS Code has much control over whatever (OS-specific) search facilities you might see there. It might be able to filter the files to only certain extensions?
Is this similar to Command +T now? I see stuff from node_modules when I prefix m search with # in the global search.
Related settings I was looking for: search.useIgnoreFiles search.useGlobalIgnoreFiles (tells search whether to pay attention to what's in gitignore to filter out what gets searched - I prefer this off in general, if I want it to filter something out I'll explicitly let it know)
Press CTRL + , then search.exclude.

S
Shotty

Temporary Exclusions

From the search function, click the ellipsis to show the files to include and files to exclude text boxes. Enter any files and folder to exclude (separated by commas).

https://i.stack.imgur.com/48uW5.png

Persistent Exclusions

From menu choose File ➡️ Preferences ➡️ Settings ➡️ User/Workspace Settings and filter default settings to search.

User settings will apply to all workspaces

Workspace settings will apply only to this workspace

You can modify the search.exclude setting (copy from default setting to your user or workspace settings). That will apply only to searches. Note that settings from files.exclude will be automatically applied.

Toggling search exclusions

You can (sometimes accidentally) toggle if these exclusions are enabled or disabled when searching using the gear icon in the files to exclude text box. Click the ellipsis, then the gear icon to toggle.

https://i.stack.imgur.com/GJFax.png

Additional documentation on configuring settings in Visual Studio Code

If the settings don't work

You might also need to Clear Editor History (See: https://github.com/Microsoft/vscode/issues/6502).

Example

I am developing an EmberJS application which saves thousands of files under the tmp directory.

If you select WORKSPACE SETTINGS on the right side of the search field, the search exclusion will only be applied to this particular project. And a corresponding .vscode folder will be added to the root folder containing settings.json.

This is my example settings:

{
    // ...
    "search.exclude": {
        "**/.git": true,
        "**/node_modules": true,
        "**/bower_components": true,
        "**/tmp": true
    },
    // ...
}

Note: Include a ** at the beginning of any search exclusion to cover the search term over any folders and sub-folders.

Picture of search before updating settings:

Before updating the settings the search results are a mess.

https://i.stack.imgur.com/FrgRO.png

Picture of search after updating settings:

After updating the settings the search results are exactly what I want.

https://i.stack.imgur.com/PRsxF.png


this has stopped working for me in the recent version... any idea why?
Is there a way to make this apply to the "Quick Open" feature? Ctrl+P on PC and Cmd+P on Mac. When i search for files using this feature, they still show up.
Make sure to clear your editor history for this to work. github.com/Microsoft/vscode/issues/6502
I had to run the command "clear editor history" (see here: github.com/Microsoft/vscode/issues/6502#issuecomment-273697138) and restart VS Code for this to work for me.
Note that it is possible to turn the search exclusion off when searching files. In the search area, expand the "files to exclude" input box and make sure that the gear icon is selected. This will ensure your search exclusion settings are honored.
a
atreeon

https://i.stack.imgur.com/5ep3L.png


How would one exclude multiple folders?
@WouterVanherck you would just add a comma to separate each file or folder you want to exclude. Example: ./src/public/,src/components/
Thank you! I never would have guessed that having that icon clicked was turning on and off my workspace settings. I spent hours thinking I was doing my settings incorrectly.
C
Christopher Grigg

Update April 2018

You can do this in the search section of vscode by pre-fixing an exclamation mark to each folder or file you want to exclude.

https://i.stack.imgur.com/3cL6e.png


Exactly! perfect
P
Pants

I'm an idiot so it took me a while to realize this, but make sure that the Gear icon is clicked on the global search so your settings can be applied.

https://i.stack.imgur.com/xl8dI.png


you're not an idiot because this is the answer that fixed it for me, I must have accidentally clicked that icon in the past, it's so not obvious that this little icon controls this and it's barely noticeable when it's enabled or disabled
You're certainly not an idiot for missing that wonderful piece of UX, but can you tell me how yours is different than this earlier answer, if it is -- well, beyond this answer being in dark mode? ;^D
@ruffin did not notice the earlier answer ha! I guess mine has better UX (the arrow) ;)
Ha! 22 people and counting agree, so who am I to argue? Least I didn't miss something obvious; thanks.
Why is this button even a thing? In what universe do I set global settings and then want to turn off those global settings via a confusing button?
L
Liam

If these are folders you want to ignore in a certain workspace, you can go to:

AppMenu > Preferences > Workspace Settings

Otherwise, if you want these folders to be ignored in all your workspaces, go to:

AppMenu > Preferences > User Settings

and add the following to your configuration:

//-------- Search configuration --------

// The folders to exclude when doing a full text search in the workspace.
"search.excludeFolders": [
    ".git",
    "node_modules",
    "bower_components",
    "path/to/other/folder/to/exclude"
],

The difference between workspace and user settings is explained in the customization docs


If it's outdated, please either update it, or delete it to avoid confusion and possible downvotes!
seroiusly, make an edit. the above accepted answer WORKS in the latest VS Code on macOS. so ditch your BS for upvotes and make a useful contribution to the actual answer.
The reason why this answer should be accepted is because it saved the configuration file.
As of 2022-07-20 this is the correct answer stackoverflow.com/a/54646625/634586
N
Nahush Farkande

If I understand correctly you want to exclude files from the vscode fuzzy finder. If that is the case, I am guessing the above answers are for older versions of vscode. What worked for me is adding:

"files.exclude": {
    "**/directory-you-want-to-exclude": true,
    "**/.git": true,
    "**/.svn": true,
    "**/.hg": true,
    "**/CVS": true,
    "**/.DS_Store": true
}

to my settings.json. This file can be opened through File>Preferences>Settings


FYI, this also excludes those files from the explorer window, not just search
I
Imad Eddin

Hi you need to find settings and add a new exclude pattern for history files

https://i.stack.imgur.com/TLPd2.png


d
david_adler

Create this file in your vscode project

.vscode/settings.json

{
  "search.exclude": {
    "**/Pods": true,
    "**/node_modules": true,
    "**/*.code-search": true
  }
}

d
domih

The short answer is to comma-separate the folders you want to ignore in "files to exclude".

Start workspace wide search: CTRL+SHIFT+f Expand the global search with the three-dot button Enter your search term As an example, in the files to exclude-input field write babel,concat to exclude the folder "babel" and the folder "concat" in the search (make sure the exclude button is enabled). Press enter to get the results.


L
LStarky

I wanted to exclude 1 of the Workspace folders completely, but found this to be difficult, since regardless of the exclusion patterns, it always runs the search on each of the Workspace folders.

In the end, the solution was to add ** to the Folder Settings search exclusion patterns.

https://i.stack.imgur.com/QBzCP.png


B
Bruno Gimenes

After you setup the search.exclude and file.exclude mentioned on the previous answers, run the command "Clear Editor History" (Use the Command Palette to do that - CTRL + SHIFT + P).

Only after that the excluded files will not appear on your quick open menu.

Update: You can run the command "Clear Command History" too. I forgot about that.


H
Hamit YILDIRIM

Forget aboves for vscode exclude search pattern, try to below pattern it is working for any folder in vscode last version!

!../../../locales/*

for example i have searched like below vscode example clude settings

files to include: *.js

files to exclude: **/node_modules,!../../../locales/,!../../../theme/,!../../admin/client/*


B
Bhaskara Arani

If you have multiple folders in your workspace, set up the search.exclude on each folder. There's a drop-down next to WORKSPACE SETTINGS.

Folder Settings Tab


T
Touhid Rahman

Extending the most voted answer, now there is an extension to achieve what is described there to toggle quickly in a GUI way. It's called Explorer Exclude. You can install this with this command:

ext install RedVanWorkshop.explorer-exclude-vscode-extension

Demo:

https://i.stack.imgur.com/efg5X.gif


A
Ambuj Khanna

I am sure it must be resolved now but I just wanted to contribute my solution as well which may be easy and useful for someone.

simply type below line in exclude field of the search

*/node_modules/*

No need to set anything in the setting files of your workspace/VS Code.


W
Walter A. Jablonowski

Exclude all from subfolders works like this (version 2019)

include

./db

exclude

./db/*

w
wp78de

Since it was not mentioned before and some users wanted to know how to exclude multiple files or folders (temporarily) in an ad-hoc search:

You can exclude multiple files and folders from the search with a comma-separated list of simplified globbing patterns (as described in VS Code's glob.ts source:

/**
 * Simplified glob matching. Supports a subset of glob patterns:
 * * `*` to match one or more characters in a path segment
 * * `?` to match on one character in a path segment
 * * `**` to match any number of path segments, including none
 * * `{}` to group conditions (e.g. *.{ts,js} matches all TypeScript and JavaScript files)
 * * `[]` to declare a range of characters to match in a path segment (e.g., `example.[0-9]` to match on `example.0`, `example.1`, …)
 * * `[!...]` to negate a range of characters to match in a path segment (e.g., `example.[!0-9]` to match on `example.a`, `example.b`, but not `example.0`)
 */

And don't forget to activate the (⚙-) Use Exclude Settings and Ignored Files button on the Search Details panel (...) (following Shotty's explanations).

https://i.stack.imgur.com/v4dCP.png


A
Adam

I wanted to search for the term "Stripe" in all files except those within a plugin ("plugin" folder) or within the files ending in ".bak", ".bak2" or ".log" (this is within the wp-contents folder structure of a wordpress install).

I just wanted to do this search one time and very quickly, so I didn't want to alter the search settings of my environment. Here's how I did it (note the double asteriks for the folder):

Search Term: Stripe

Include Files: {blank}

Exclude Files: *.bak*, *.log, **/plugins/**

Here's what it looked like


r
reza.cse08

Create a file with .gitignore & put the folder or file name which one you want to ignore.

to ignore everything below node_modules folder

echo node_modules/ > .gitignore 

the question is not about git