【问题标题】:How to exclude folder from "Explore" tab?如何从“探索”选项卡中排除文件夹?
【发布时间】:2016-01-20 09:42:57
【问题描述】:

我正在尝试排除 Visual Studio Code 中 Explore 选项卡上的几个文件夹。为此,我在项目的根目录中添加了以下 jsconfig.json

{
    "compilerOptions": {
        "target": "ES6"
    },
    "exclude": [
        "node_modules"
    ]
}

node_modules 文件夹在目录树中仍然可见。

我做错了什么?还有其他选择吗?

【问题讨论】:

  • 似乎大多数答案都建议在 Workspace Settings 中更改此配置。但是,我不需要 node_modules 出现在 any 项目中。所以我会在 User Settings 中全局设置它。

标签: visual-studio-code


【解决方案1】:

使用files.exclude:

  • 转到文件 -> 首选项 -> 设置(或在 Mac 上 代码 -> 首选项 -> 设置
  • 选择workspace settings 标签
  • 将此代码添加到右侧显示的settings.json文件中:
    // Place your settings in this file to overwrite default and user settings.

    {
        "settings": {
            "files.exclude": {
                "**/.git": true,         // this is a default value
                "**/.DS_Store": true,    // this is a default value
    
                "**/node_modules": true, // this excludes all folders 
                                        // named "node_modules" from 
                                        // the explore tree
    
                // alternative version
                "node_modules": true    // this excludes the folder 
                                        // only from the root of
                                        // your workspace 
            }
        }
    }

如果您选择文件 -> 首选项 -> 用户设置,那么您可以为当前用户全局配置排除文件夹。

【讨论】:

  • 如果有人想知道:尾部斜杠不会帮助(也不会损害)将排除仅限于文件夹。即"**/BACKUP/": true 与没有最后一个斜线一样好/坏。
  • 您在 RHS 上提供的值似乎与 LHS 上的值合并。据推测,必须从 LHS 复制值并将其设置为 false 以覆盖默认值。
  • 更正:应该是 "**/node_modules": true
  • 继承在这里是如何工作的?我必须列出所有排除项还是仅列出用户设置中未列出的项?
  • 值得注意的是,在当前版本的 Code (1.28.2) 中,files.exclude 键属于 内部 code-workspace 文件的 settings 键。
【解决方案2】:

在较新版本的 VS Code 中,导航到设置 (Ctrl+,),并确保在右上角。

然后添加一个files.exclude 选项来指定要排除的模式。

如果您只想从搜索结果中排除文件,而不是从文件夹资源管理器中排除,也可以添加 search.exclude

【讨论】:

  • 从搜索中排除,同时仍然能够在资源管理器中浏览文件 - 正是我所需要的,谢谢!
  • 感谢您指定Workspace Settings
  • 在将files.exclude 应用于工作区设置.code-workspace 文件)时,我必须将files.exclude 放在settings:{ ... } 中,否则它会抱怨属性未知的行.有一个第三个“文件夹设置”选项卡(.vscode/settings.json),它在最外面的大括号中起作用。
【解决方案3】:

我设法通过禁用验证来消除错误:

{
    "javascript.validate.enable": false,
    "html.validate.styles": false,
    "html.validate.scripts": false,
    "css.validate": false,
    "scss.validate": false
}

Obs:我的项目是一个使用 StyledComponents、React、Flow、Eslint 和 Prettier 的 PWA。

【讨论】:

  • 这根本没有回答 OP 的问题。
【解决方案4】:

在较新版本的 VSCode 中,这已移至特定于文件夹的配置块。

  • 转到File -> Preferences -> Settings(或在Mac上Code -> Preferences -> Settings
  • 选择文件夹设置标签

然后添加一个“files.exclude”块,列出您要排除的目录 glob:

{
    "files.exclude": {
        "**/bin": true,
        "**/obj": true
    },
}

【讨论】:

    【解决方案5】:

    在 Visual Studio Code 1.28 版中,"files.exclude" 必须放在 settings 节点内。

    生成的工作区文件如下所示:

    {
        "settings": {
            "files.exclude": {
                "**/node_modules": true
            }
        }
    }
    

    【讨论】:

      【解决方案6】:

      图形界面方式

      1. 转到“文件 -> 首选项 -> 设置”(或按 Ctrl + ,)然后:
      2. 在搜索栏中输入“排除”。
      3. 如果您希望此更改仅影响当前项目而不是每个项目,请选择“工作区”选项卡。
      4. 点击“添加模式”按钮。

      编码方式

      1. 打开settings.json 文件:

        • Ctrl + Shift + PCmd + Shift + P 在 Mac 上,然后键入“打开工作区设置 (JSON)”。
        • 或者,在旧版本上,您可以单击 GUI 选项卡右上角的小 {} 图标:
      2. 将排除的文件夹添加到files.exclude。另请查看search.excludefiles.watcherExclude,因为它们也可能有用。这个 sn-p 包含它们的解释和默认值:

         {
           // Configure glob patterns for excluding files and folders. 
           // For example, the files explorer decides which files and folders to show 
           // or hide based on this setting. 
           // Read more about glob patterns [here](https://code.visualstudio.com/docs/editor/codebasics#_advanced-search-options).
           "files.exclude": {
             "**/.git": true,
             "**/.svn": true,
             "**/.hg": true,
             "**/CVS": true,
             "**/.DS_Store": true
           },
           // Configure glob patterns for excluding files and folders in searches. 
           // Inherits all glob patterns from the `files.exclude` setting.   
           // Read more about glob patterns [here](https://code.visualstudio.com/docs/editor/codebasics#_advanced-search-options).
           "search.exclude": {
             "**/node_modules": true,
             "**/bower_components": true
           },
           // Configure glob patterns of file paths to exclude from file watching. 
           // Patterns must match on absolute paths 
           // (i.e. prefix with ** or the full path to match properly). 
           // Changing this setting requires a restart. 
           // When you experience Code consuming lots of cpu time on startup, 
           // you can exclude large folders to reduce the initial load.
           "files.watcherExclude": {
             "**/.git/objects/**": true,
             "**/.git/subtree-cache/**": true,
             "**/node_modules/*/**": true
           }
         }
        

      有关其他设置的更多详细信息,请参阅official settings.json reference

      【讨论】:

        【解决方案7】:

        Explorer Exclude 扩展正是可以做到这一点的。 https://marketplace.visualstudio.com/items?itemName=RedVanWorkshop.explorer-exclude-vscode-extension

        它在右键菜单中添加了隐藏当前文件夹/文件的选项。 它还在资源管理器菜单中添加了一个垂直选项卡隐藏项目,您可以在其中查看当前隐藏的文件和文件夹并轻松切换它们。


        【讨论】:

          【解决方案8】:

          您可以配置模式以在资源管理器和搜索中隐藏文件和文件夹。

          1. 打开 VS 用户设置(主菜单:文件 > 首选项 > 设置)。这将打开设置屏幕。

          2. 在顶部的搜索中搜索files:exclude

          3. 根据需要使用新的 glob 模式配置用户设置。在这种情况下,添加此模式 node_modules/ 然后单击 OK。模式语法很强大。您可以在 Search Across Files 主题下找到模式匹配的详细信息。

              {
                 "files.exclude": {
                  ".vscode":true,
                  "node_modules/":true,
                  "dist/":true,
                  "e2e/":true,
                  "*.json": true,
                  "**/*.md": true,
                  ".gitignore": true,
                  "**/.gitkeep":true,
                  ".editorconfig": true,
                  "**/polyfills.ts": true,
                  "**/main.ts": true,
                  "**/tsconfig.app.json": true,
                  "**/tsconfig.spec.json": true,
                  "**/tslint.json": true,
                  "**/karma.conf.js": true,
                  "**/favicon.ico": true,
                  "**/browserslist": true,
                  "**/test.ts": true,
                  "**/*.pyc": true,
                  "**/__pycache__/": true
                }
              }
          

          【讨论】:

            【解决方案9】:

            这是 2021 年在 Mac 上使用 Visual Studio Code 的答案。我使用的是 Code v1.60。

            1. 打开设置(命令-P)。

            2. 选择工作区选项卡。

            1. 使用设置搜索栏搜索“排除”。然后查找“文件:排除”部分。点击显示“添加图案”的蓝色按钮。

            1. 将出现一个新的文本字段。添加要排除的目录的名称。如果目录名为excluded_directory,则输入**/excluded_directory。点击确定。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2017-06-08
              • 2010-10-01
              • 2017-08-27
              • 1970-01-01
              • 2016-11-01
              • 1970-01-01
              • 2017-01-16
              相关资源
              最近更新 更多