Snippet - VS Code - Toggle files.exclude

4 years ago

One of my great things about VS Code is the ability to endlessly tweak things, often just by updating User or Workspace Settings. One of my favorite settings is files.exclude which allows you to hide files shown in explorer. There are many files that are used only initially or changed or viewed very infrequently and these can get distracting. A few examples are .gitignore, node_modules, or build related folders. A typical settings to hide some of these files might looks something like this:

    "files.exclude": {
        ".gitignore": true,
        ".vscode": true,
        "**/.DS_Store": true,
        "**/*/.env": true,
        "**/*/.gitignore": true,
        "**/*/deploy": true,
        "**/*/node_modules": true,
        "**/*/public": true,
        "**/*/utility": true,
        "lerna*": true,
        "node_modules": true,
        "yarn*": true

The manual way would be to hit Command+, to open User Settings, highlight the code block and hit Command+/ to comment or uncomment. This doesn't take long, but why not create a simple script. Of course you could create an extension as well, but I'm not a big fan of bogging my IDE with too many extras. I typically have a utility or task folder at the root of my project and I would put something like this in "utility-dev.js".

const fs = require('fs')
const os = require('os')
const path = require('path')

const TYPES = {

try {
  const args = process.argv.slice(2)
  const type = TYPES[args[0].toUpperCase()]
  if (type) {
    switch (type) {
        const userSettingsPath =
          os.platform() === 'darwin'
            ? path.join(os.homedir(), 'Library/Application Support/Code/User/settings.json') // OSX
            : path.join(os.homedir(), 'User/settings.json') // Windows
        const userSettings = require(userSettingsPath)
        userSettings['files.exclude'] = Object.keys(userSettings['files.exclude']).reduce((acc, key) => {
          return {
            [key]: !userSettings['files.exclude'][key]
        }, {})
        fs.writeFileSync(userSettingsPath, JSON.stringify(userSettings, null, 2))
  } else {
    console.log('Error: type required')
} catch (error) {
  console.log('Error: Unknown Error - ' + error)

Now you can simply run the following command or create an entry in package.json to initiate quickly.

node ./utility/utility-dev.js IDE_TOGGLE_FILES


    "scripts": {
        "ide-tf": "node ./utility/utility-dev.js IDE_TOGGLE_FILES"

Then use npm or yarn...

npm run ide-tf
yarn ide-tf
Discuss on Twitter