How to exclude Folders from Search in Visual Studio Code

avatar
Borislav Hadzhiev

Last updated: Apr 6, 2024
6 min

banner

# Table of Contents

  1. Excluding Files or Folders from the current search
  2. Excluding folders from all searches
  3. Excluding folders from all searches in the current project
  4. Toggling global search exclusions
  5. Troubleshooting issues with exclude patterns when searching
  6. Make sure the Exclude settings and Ignore Files button is enabled
  7. Use a comma to separate multiple folders to exclude
  8. Use the correct format when excluding files and folders from search

# Excluding Files or Folders from the current search

If you need to exclude files or folders from the current search:

  1. Press Ctrl + Shift + F (or Cmd + Shift + F on macOS) to show the search menu in the left sidebar.

  2. Click on the three dots (ellipsis) icon to show the files to exclude field.

show files to exclude field

  1. Specify the folders as comma-separated entries in the files to exclude field.

Here is a short clip that demonstrates the process.

exclude files and folders from search

You can use the following format to exclude files and folders from the search.

  • **/package-lock.json - exclude files named package-lock.json located in any directory.
  • *.ts - exclude all files with .ts extension.
  • **/node_modules - exclude all files in the node_modules directory.
  • **/.git - exclude all files in the .git directory.
  • wp-*.* - exclude all files that start with wp-.
  • wp-config.* - exclude all files that start with wp-config. followed by any extension.
When ** (two asterisks) are included at the beginning of the pattern, it covers the search term over all folders and subfolders (including the root directory).

If you need to exclude multiple files or folders, separate them by a comma.

For example, .gitignore,example.ts excludes the .gitignore and example.ts files from search.

exclude multiple files or folders from search

Similarly, **/node_modules,**/.git, excludes the node_modules and .git folders from search.

# Searching only in Open Editors

There is also an icon that allows you to search only in open editors.

search only in open editors

When the icon to the right of the files to include field is clicked, the search results only contain matches from the currently opened files.

You can click on the icon again to disable the search only in open editors functionality.

# Excluding folders from all searches

If you need to exclude folders globally, from all searches:

  1. Press Ctrl + Shift + P (or Command + Shift + P on macOS).
Note: you can also press F1 to open the Command Palette.
  1. Type user settings and select Preferences: Open User Settings.

open user settings

You can also open the settings screen by pressing Ctrl + , on Windows and Linux or Cmd + , on macOS.

  1. Type search exclude and scroll down to the Search: Exclude setting.

exclude folders from all searches

  1. Click on the Add Pattern button and type a pattern that you'd like to exclude from the search.

The Search: Exclude setting allows you to configure glob patterns for excluding files and folders in full-text searches.

For example, to exclude node_modules from the search, you would use the following pattern:

  • **/node_modules

To exclude Thumbs.db files located in any directory, you would use the following pattern:

  • **/Thumbs.db

To exclude files that end with *.code-search located in any directory, you would use the following pattern:

  • **/*.code-search

If you scroll up with the Settings tab opened, you will see the Files: Exclude setting.

Note that the patterns that are specified under Files: Exclude are automatically excluded from the search.

files and folders automatically excluded from search

# Excluding folders from all searches in the current project

If you only want to exclude files and folders from all searches but only applied to the current project (and not all projects of the logged-in user), configure the setting scoped to your Workspace.

  1. Press Ctrl + Shift + P (or Command + Shift + P on macOS).
Note: you can also press F1 to open the Command Palette.
  1. Type user settings and select Preferences: Open User Settings.

open user settings

You can also open the settings screen by pressing Ctrl + , on Windows and Linux or Cmd + , on macOS.

  1. Type search exclude and click on the Workspace tab.

select workspace tab

  1. Scroll down to the Search: Exclude setting and add the patterns you want to exclude from the search.

exclude folders from search in workspace

The workspace settings only apply to the current workspace (project) and override any global configuration.

Once you add patterns to the workspace Search: Exclude setting, you can view the added patterns in your .vscode/settings.json file.

view exclude patterns in vscode settings json

You can use the same syntax to manually add exclude patterns to your .vscode/settings.json file.

settings.json
{ "search.exclude": { "**/my-folder": true, "**/your-folder": true } }

# Toggling global search exclusions

Sometimes you might need to turn off the global search exclusions that you've previously set.

You can use a button to toggle global search exclusion patterns.

  1. Press Ctrl + Shift + F (or Cmd + Shift + F on macOS) to show the search menu in the left sidebar.

  2. Click on the three dots (ellipsis) icon to show the files to exclude field.

show files to exclude field

  1. Click on the Use Exclude Settings and Ignore Files cogwheel button in the files to exclude field.

use exclude settings and ignore files

By default, the Use Exclude Settings and Ignore Files functionality is enabled.

If you click on the button, the search results include the globally excluded files and folders.

Here is a short clip that demonstrates how to toggle global search exclusions.

toggle global search exclusions

Always make sure to leave the Use Exclude Settings and Ignore files cogwheel selected after you're done.

Otherwise, you will get confused the next time you search and see results from folders that you've already excluded.

# Troubleshooting issues with exclude patterns when searching

If the exclude patterns aren't applied correctly or you get incorrect search results, try to clear your editor history.

  1. Press:
  • Ctrl + Shift + P on Windows and Linux
  • Command + Shift + P on macOS
Note: you can also press F1 to open the Command Palette.
  1. Type Clear editor and select Clear editor History.

select clear editor history

# Make sure the Exclude settings and Ignore Files button is enabled

Another thing that often causes issues is forgetting to enable the Use Exclude Settings and Ignore Files cogwheel button.

make sure to enable exclude folders functionality

If the button is disabled, your Files: Exclude setting is completely ignored.

# Use a comma to separate multiple folders to exclude

If you need to exclude multiple files or folders, separate them by a comma.

For example, .gitignore,example.ts excludes the .gitignore and example.ts files from search.

exclude multiple files or folders from search

Similarly, **/node_modules,**/.git, excludes the node_modules and .git folders from search.

# Use the correct format when excluding files and folders from search

You can use the following format to exclude files and folders from the search.

  • **/package-lock.json - exclude files named package-lock.json located in any directory.
  • *.ts - exclude all files with .ts extension.
  • **/node_modules - exclude all files in the node_modules directory.
  • **/.git - exclude all files in the .git directory.
  • wp-*.* - exclude all files that start with wp-.
  • wp-config.* - exclude all files that start with wp-config. followed by any extension.

You can read more about the glob syntax in this section of the docs.

The most commonly used glob characters are:

  • * - matches zero or more characters in a path segment.
  • ? - matches exactly one character in a path segment.
  • ** - matches any number of characters in a path segment or no characters at all. For example, **/node_modules matches a node_modules folder in the root directory or a nested directory.
  • {} - is used to group conditions. For example, {**/*.js,**/*.ts} matches all JS and TS files.
  • [] - is used to specify a range of characters or numbers to match. For example, foo_[0-9] matches foo_0, foo_1, foo_2, etc.
  • [!...] - is used to negate a range of characters to match. For example, foo_[!0-9] matches foo_a and foo_b, but not foo_0 and foo_1.

I've also written a detailed guide on how to search all files in VS Code.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.