In Visual Studio Code, you can exclude files and folders from the search in several ways, from temporary exclusions in the search panel to permanent settings that can be configured for a specific workspace or your entire user profile.
The most effective and detailed methods involve using the search.exclude and files.exclude settings.
Method 1: The search.exclude setting
This setting is the most precise way to control what is excluded from the full-text search feature. It offers control at both the user and workspace levels.
Setting up search.exclude
- Open Settings: Use the keyboard shortcut
Ctrl + ,(Windows/Linux) orCmd + ,(macOS). - Choose Scope: Decide if your changes should apply to all projects (User settings) or only the current project (Workspace settings). The Workspace setting will override the User setting.
- Find
search.exclude: Typesearch excludeinto the settings search bar. - Add a pattern: Click the "Add Pattern" button and enter a glob pattern to define which files or folders to exclude. For example,
**/node_moduleswill exclude all folders namednode_moduleswherever they appear in your project.
The anatomy of search.exclude
The search.exclude setting is an object in settings.json that maps glob patterns to boolean values.
"search.exclude": {
"**/node_modules": true,
"**/bower_components": true,
"**/*.log": true
}
Use code with caution.
**/node_modules: Excludes thenode_modulesfolder and all its contents from search, no matter where it is located in the project hierarchy.**/*.log: Excludes all files with a.logextension from the search.**/dist/**/*: Excludes all files and subfolders within anydistfolder.
Method 2: The files.exclude setting
This setting is broader and is primarily used to hide files and folders from the Explorer view, but it also applies to search by default. Any pattern added here will be respected by the search functionality.
Setting up files.exclude
- Open Settings: Press
Ctrl + ,(Windows/Linux) orCmd + ,(macOS). - Choose Scope: Select User or Workspace settings.
- Find
files.exclude: Search forfiles exclude. - Add a pattern: Use the "Add Pattern" button to specify files and folders to exclude. For example,
**/buildwill hide the build output folder from both the Explorer and searches.
The anatomy of files.exclude
The structure is similar to search.exclude.
"files.exclude": {
"**/.git": true,
"**/.DS_Store": true,
"**/node_modules": true
}
Use code with caution.
- You will notice that some patterns like
**/.gitand**/node_modulesare often included by default for common projects.
Method 3: Temporarily excluding from the search panel
For quick, one-off exclusions that don't require permanent settings, you can use the search panel directly. This is useful for temporarily focusing your search without changing your settings.
How to use the search panel
- Open Search: Press
Ctrl + Shift + F(Windows/Linux) orCmd + Shift + F(macOS). - Toggle Details: Click the ellipsis (
...) to reveal the "files to exclude" input field. - Enter pattern: Type your exclusion patterns, separated by commas. For example, to search for a term but ignore the
testdirectory, you would enter**/testin the "files to exclude" field. - Important: These exclusions are only active for the current search session.
Method 4: Using .gitignore files
If your project is under version control with Git, you can leverage your .gitignore file to manage search exclusions. This is especially helpful for keeping your search in sync with your version control strategy.
How to enable .gitignore for search
- Open Settings: Navigate to your settings (
Ctrl + ,orCmd + ,). - Search for
ignore: Look for theSearch: Use Ignore Filessetting. - Enable the setting: Ensure this setting is checked. With this enabled, VS Code will automatically use the patterns in your
.gitignorefile to exclude files and folders from search results.
Enabling Use Exclude Settings and Ignore Files
When using the search panel, there is a gear icon (cogwheel) next to the "files to exclude" field. Click this to toggle whether your global/workspace exclude settings and .gitignore files are honored. If you are not seeing expected exclusions, make sure this icon is selected.
A hierarchy of exclusion methods
Understanding the different methods in Visual Studio Code is key to managing your search scope effectively. Here is a quick summary of the hierarchy, from most temporary to most permanent:
- Search Panel: Quick, temporary exclusions for a single search.
.gitignore: Inherits exclusions from your version control setup. A powerful, project-standard approach.- Workspace
settings.json: Permanent exclusions that are specific to the current project and can be shared with your team. - User
settings.json: Global exclusions that apply to all projects opened by your user.