348

I am working on a typescript project in Visual Studio code and would like to hide the .js.map (and maybe even the .js) files from appearing in the file explorer.

Is it possible to display only the .ts files in the file explorer?

Mark
  • 143,421
  • 24
  • 428
  • 436
Tyler
  • 17,669
  • 10
  • 51
  • 89
  • Does this answer your question? [How do I hide certain files from the sidebar in Visual Studio Code?](https://stackoverflow.com/questions/30140112/how-do-i-hide-certain-files-from-the-sidebar-in-visual-studio-code) – JΛYDΞV May 12 '22 at 21:41

13 Answers13

678

In your settings (either user or workspace) there is a setting that you can tweak to hide anything you'd like:

{
    "files.exclude": {
        "**/.git": true,
        "**/.DS_Store": true
    }
}

So you can add in the following to hide .js and .js.map files

"**/*.js": true,
"**/*.js.map": true

As this other answer explains, most people probably only want to hide .js files when there is a matching .ts file.

So instead of doing:

"**/*.js": true

you might want to do:

"**/*.js": {"when": "$(basename).ts"}
Community
  • 1
  • 1
Brocco
  • 62,737
  • 12
  • 70
  • 76
  • 12
    Perfect solution. To add on, I suspect many users interested in this will also be interested in hiding the node_modules directory. That can be done like so: `"**/node_modules/": true` – Josh1billion Jan 08 '16 at 22:10
  • is there a shortcut to deactivate/activate the files.exclude config option? sometime I want to check somethings hidden – Abdulaziz Alsubaie Mar 04 '16 at 09:43
  • 107
    `"**/*.js": {"when": "$(basename).ts"},"**/*.map": {"when": "$(basename).map"}` – iuristona Apr 08 '16 at 14:20
  • Leaving aside just how clunky nfortunately you are not going to want to hide your ".js" files even with this clunky workaround: "**/*.js": {"when": "$(basename).ts"} – Rick O'Shea Jul 06 '16 at 16:59
  • 1
    @aaalsubaie To activate/deactivate the file exclude filter, there is an extension available in MarketPlace at https://marketplace.visualstudio.com/items?itemName=nwallace.peep – A J Qarshi Aug 24 '16 at 15:13
  • it took me some time here to understand,so the total output is:{ "files.exclude": { "**/*.js.map": true, "**/*.js": {"when": "$(basename).ts"} } } – dang Nov 12 '16 at 20:02
  • What does the double asterisk at the beginning mean? I know the single asterisk is a wildcard. – Jan Navarro Dec 04 '16 at 05:47
  • The double asterisk (`**`) means to search through all subdirectories. – Brocco Dec 04 '16 at 13:49
  • what about when the ts files are in a different folder? – Th3B0Y Feb 25 '17 at 00:01
  • @Th3B0Y I'm not sure how to handle that on a per-file-basis. But if they're all in one file, you can just hide that entire folder. – Brocco Feb 25 '17 at 19:41
  • @Brocco , by hiding the whole folder all js files which should not be hidden are also gone =( – Th3B0Y Feb 26 '17 at 20:38
  • Is there a way to do this in Visual Studio - not Visual Studio Code? – HaveSpacesuit Mar 28 '17 at 14:45
  • You CAN do this in Visual Studio 2017, if you add a .vscode subdirectory with the settings.json file. – HaveSpacesuit Mar 28 '17 at 16:50
  • This doesn't work with VS 2017 IDE - you need to [follow this SO post to exlcude files](http://stackoverflow.com/a/42839023/175679) – SliverNinja - MSFT May 19 '17 at 19:47
  • 2
    Is `"**/*.map": {"when": "$(basename).map"}` the same as `"**/*.map": true`? – gdbj Nov 06 '17 at 18:50
  • `"**/*.js": {"when": "$(basename).ts"}` - it's amazing it's possible to do that. Thank you so much for the tip! – laurent Oct 17 '20 at 15:22
201

I found this, If you have standard JS files then these will be hidden too which may not always be what you want. Perhaps this is better as it only hides JS files that match TS files...

{
    "files.exclude": {
        "**/.git": true,
        "**/.DS_Store": true,
        "**/*.js.map": true,
        "**/*.js": {"when": "$(basename).ts"}
    }
}
AlwaysLearning
  • 2,385
  • 1
  • 15
  • 18
101

I really don't know how this is implemented but for hiding .js files works:

"**/*.js": {"when": "$(basename).ts"}

For hiding .js.map files works:

"**/*.js.map": {"when": "$(basename)"}
acdcjunior
  • 132,397
  • 37
  • 331
  • 304
Luka
  • 1,603
  • 1
  • 14
  • 8
  • 7
    This solution also handles `.js.map`, as Visual Studio Code seems to interpret `$(basename)` to be everything _before_ the final `.`. Could be simplified to `"**/*.map: {"when": "$(basename)"}`, but this would exclude _any_ `.map` files which have a corresponding non-`.map` file. – Njål Nordmark May 06 '16 at 04:28
47

When you are working with TypeScript, you often don’t want to see generated JavaScript files in the explorer or in search results. VS Code offers filtering capabilities with a files.exclude setting (File > Preferences > Workspace Settings) and you can easily create an expression to hide those derived files:

"**/*.js": { "when": "$(basename).ts"}

Similarly hide generated .map files by:

 "**/*.js.map": { "when": "$(basename)"}

So you will have a configuration like in:

settings.json

// Place your settings in this file to overwrite default and user settings.
{
    "files.exclude": {
        "**/*.js": { "when": "$(basename).ts"},
        "**/*.js.map": { "when": "$(basename)"}
    }
}

Link: https://code.visualstudio.com/docs/languages/typescript#_hiding-derived-javascript-files

Asim K T
  • 16,864
  • 10
  • 77
  • 99
23

John Papa Twitter LINK says use the following:

"files.exclude": {
        "**/.git": true,
        "**/.DS_Store": true,
        "**/*.js" : {
            "when": "$(basename).ts"
        },
        "**/*.js.map": {
            "when": "$(basename)"
        }
}
YourAboutMeIsBlank
  • 1,787
  • 3
  • 18
  • 27
15

From the official doc:

to exclude JavaScript files generated from both .ts and .tsx source files, use this expression:

"**/*.js": { "when": "$(basename).ts" },
"**/**.js": { "when": "$(basename).tsx" }

This is a bit of a trick. The search glob pattern is used as a key. The settings above use two different glob patterns to provide two unique keys but the search will still match the same files.

UPDATE 10/3/2017: with this trick we have a problem with "search in folder". Please see the issue

Dmitry Kurmanov
  • 765
  • 2
  • 12
  • 19
  • 1
    There is [another workaround](https://github.com/microsoft/vscode/issues/1214#issuecomment-164163514) for this. I posted it as an answer below. – Zach Olivare Nov 08 '19 at 18:57
14

1. Go to preferences > settings

enter image description here

2. Click on "Edit on settings.json" (It's on the bottom of the image)

enter image description here

3. Update the object json as you can see in the image. Then save your changes Ctrl + S and that's all.

"files.exclude": {
    "**/*.js": {"when": "$(basename).ts"}
}

enter image description here

YourAboutMeIsBlank
  • 1,787
  • 3
  • 18
  • 27
Daniel Delgado
  • 4,813
  • 5
  • 40
  • 48
7

Please add the following lines in "User Settings" panel in order to override "Default Settings". You can hide files {basename}.js and {basename}.js.map when you create file as {basename}.ts.

"files.exclude": {
        "**/*.js": {
            "when": "$(basename).ts"
        },
        "**/*.js.map": {
            "when": "$(basename)"
        }        
    }
mutlugokhan
  • 91
  • 2
  • 4
5

Add these settings to your settings.json in your .vscode folder

// Place your settings in this file to overwrite default and user settings.
{
    "files.exclude" :{
    "**/.git":true,
    "**/.DS_Store":true,
    "**/*.map":true,
    "**/app/**/*.js":true

    }
}

If the settings.json is not available click on File ---> Preferences --> Workspace Settings.

Alireza Fattahi
  • 42,517
  • 14
  • 123
  • 173
Aravind
  • 40,391
  • 16
  • 91
  • 110
  • Where is this File ---> Preferences --> Workspace Settings? When I go to File I don't have anything related to the Preferences and work spaces so I must be in the wrong place. For your information I am using Visual Studio 2017 Enterprise. – MHOOS Apr 08 '17 at 16:58
4

Maybe it's better to hide .map and .js files when they match their corresponding .ts file.
You can do that by copying the following lines in VS User Settings (Preferences > User Settings):

// Workspace settings
"files.exclude": {
        "**/*.js":  {"when": "$(basename).ts"},
        "**/*.map": true
 }
gre_gor
  • 6,669
  • 9
  • 47
  • 52
3

In VS Code go to Code (or File for Windows users) > Preferences > Workspace Settings and add this code snippet:

{
   "files.exclude": {
      "**/*.js": {"when": "$(basename).ts"},
      "**/*.map": {"when": "$(basename).map"}
   }
}
reza.cse08
  • 5,938
  • 48
  • 39
2

In v1.67 (see https://github.com/microsoft/vscode-docs/blob/vnext/release-notes/v1_67.md):

Explorer File Nesting

The explorer now supports nesting related files based on their names. There are several settings to control this behaviour:

  • explorer.fileNesting.enabled: Controls whether file nesting is enabled at-large. It can be set either globally or for a specific workspace.
  • explorer.fileNesting.expand: Controls whether nested files are expanded by default.
  • explorer.fileNesting.patterns: Controls how files are nested. The default configuration provides nesting intelligence for TypeScript and JavaScript projects, but you're encouraged to modify this to fit your own project's structure. Some examples:

Default configuration: default configuration

Nesting under index.ts when a file matches the directory's name ("index.ts": "${dirname}.ts"): enter image description here

Nesting files that have the same name as a different file but with an added segment ("*": "${basename}.*.${dirname}"): enter image description here

Those following closely my recall this has been an experimental setting for several iterations. The behavior now is mostly unchanged, with the expeption of file operations. The experimental setting explorer.experimental.fileNesting.operateAsGroup has been removed in favor of treating nests as a group when collapsed, but as single entities otherwise. This means that if you want to copy, cut, drag, or delete an entire nested stack of files, you can collapse the nest then operate on it as a single entity. When non collapsed, selections will behave as normal.


Previous answer:

There is a new, currently experimental, feature in the Insiders Build v1.64 called File Nesting which although it will not hide auto-generated files, it will nest them in a collapsed state if you wish under the parent file from which the related files were generated. If possible, you should test your case in the Insiders Build now.

file nesting settings

So with this modification to the File Nesting: Patterns :

"*.ts": "$(capture).js, $(capture).d.ts, $(capture).js.map" you can achieve what the OP requested easily. Demo:

file nesting demo .ts generated files

File Nesting will not hide those nested files from searches.

Mark
  • 143,421
  • 24
  • 428
  • 436
0

There is still no official solution for excluding a file glob based on two different conditions. See this issue.

There is a workaround though, to define two different glob patterns which target the same files:

{
    "files.exclude": {
        "**/*.js": { "when": "$(basename).ts"},
        "**/*?.js": { "when": "$(basename).tsx"}
    }
}
Zach Olivare
  • 3,805
  • 3
  • 32
  • 45