VSCode无默认搜索过滤,所有过滤需显式配置:search.exclude设永久规则(须在settings.json中、值为对象、glob格式正确),files to include/exclude仅临时生效;.gitignore需search.useIgnoreFiles=true才参与过滤,files.exclude对搜索无效。

VSCode 没有“默认搜索过滤条件”这个概念——所有过滤行为都必须显式配置,且分永久和临时两套机制,混用容易失效。
search.exclude 是唯一能设“默认”的地方
它决定每次 Ctrl+Shift+F 打开后自动跳过的路径,但必须满足三个硬性条件才生效:
- 写在
settings.json里(用户级或工作区级均可),不能放在tsconfig.json或其他配置文件中 - 值必须是对象:
"search.exclude": { "**/dist": true, "**/node_modules": true },不能是数组或字符串 - 每个键必须是合法 glob:以
**/开头、用正斜杠/(Windows 下写**\dist会被完全忽略)、值必须是布尔true(不是"true")
常见错误示例:"node_modules": true 只排除根目录下的 node_modules;"**/dist/**": true 虽然有效,但冗余,推荐统一用 "**/dist": true。
files to include/exclude 比 search.exclude 更灵活但不“默认”
这两个输入框在搜索面板底部,只对当前搜索生效,优先级高于 search.exclude,适合快速收窄范围:
-
files to include是白名单:填**/src/**.ts就只搜 TypeScript 源码,其余全过滤 -
files to exclude是黑名单:填**/tests/**,**/mocks/**(逗号无空格)可临时跳过测试和模拟代码 - 它们不保存、不继承、不跨搜索会话——关掉面板就清空,所以不能当“默认”用
注意:files to include 填错会静默失效(比如漏了 **/ 前缀),结果不变,容易误判为功能异常。
为什么 .gitignore 不自动生效?查 search.useIgnoreFiles
很多人以为把 dist、node_modules 加进 .gitignore 后 VSCode 就会跳过,其实不会——是否读 .gitignore 由 search.useIgnoreFiles 控制,默认为 true,但容易被覆盖:
- 打开命令面板(
Ctrl+Shift+P),搜Preferences: Open Settings (JSON) - 搜索
search.useIgnoreFiles,如果值是false或根本没这行(说明被设为false过),就删掉该行或手动设为true - 该设置只补充规则,不替代
search.exclude:.gitignore 里写的dist和search.exclude里的"**/dist": true效果一致,但后者优先级更高
如果你的项目里 .gitignore 规则很全,又不想重复维护,可以只开 search.useIgnoreFiles,但要注意它不支持 !unignored-file 这类取反逻辑,遇到例外还得靠 search.exclude 补。
别把 files.exclude 当成搜索过滤
files.exclude 只控制左侧资源管理器显示和 Ctrl+P 文件跳转,对 Ctrl+Shift+F 全局内容搜索完全无效:
- 你在
files.exclude里写了"**/dist": true,但搜索仍会扫dist/里的文件——这不是 bug,是设计如此 - 想让搜索也跳过,必须单独配
search.exclude - 两者可以共存:一个管“看不看得到”,一个管“搜不搜得到”,互不干扰
最常被忽略的一点:工作区设置(.vscode/settings.json)优先级高于用户设置,如果团队共用一套排除规则,务必写在项目级配置里,否则本地用户设置可能被覆盖而不自知。











