rioj7 / commandOnAllFiles

Visual Studio Code extension to apply a command to all files in a Workspace
4 stars 0 forks source link

Apply one or more commands to all files in the Workspace.

If you want to apply multiple commands or a command with arguments you need to use the extension multi-command by ryuta46.

This extension is based on the Format All Files in Workspace by Alex Ross

Extension commands

The extension exports the following command:

applyOnWorkspace

The command will:

This means that files that are open in a tab will be closed if they meet the criteria. If there is a way to get a list of files currently open this will change.

Extension Settings

No matter what the value of commandOnAllFiles.excludeFolders is the ".git" entry will always be added. This to prevent that if you make a mistake in the configuration you could corrupt your Source Control Repository.

To prevent an incorrect directory match in the includeFolders glob patterns always include the separator /. Using ["/src/"] prevents a match on directory src-test.

Example

If you know the command description from the View > Command Palette (Ctrl+Shift+P), what do you need to set the "command" property.

For example you want to apply the command Inline CSS, you have to find the command ID for this command with the Keyboard Shortcuts editor.

  1. Use menu: File > Preferences > Keyboard Shortcuts
  2. Search for Inline
  3. Locate the Inline CSS command
  4. From context menu (right click): Copy Command ID
  5. Use this as the value of the "command" property

An example of how to configure the extension to add Hello to the end of all .txt files in the Workspace. We need the extension multi-command because the have to perform a sequence of commands.

The default value for commandOnAllFiles.excludeFolders is enough for this example.

In settings.json:

  "multiCommand.commands": [
    {
      "command": "multiCommand.addHelloAtEnd",
      "sequence": [
        "cursorBottom",
        { "command": "type",
          "args": { "text": "Hello" }
        }
      ]
    }
  ],
  "commandOnAllFiles.commands": {
    "Add Hello to the End": {
      "command": "multiCommand.addHelloAtEnd",
      "includeFileExtensions": [".txt"]
    }
  }

In keybindings.json:

  {
    "key": "ctrl+i a", // or any other key combo
    "command": "commandOnAllFiles.applyOnWorkspace",
    "args": ["Add Hello to the End"]
  }

If you want to limit the files included with a regular expression you need to use the includeFiles property:

In settings.json:

  "commandOnAllFiles.commands": {
    "Add Hello to the End": {
      "command": "multiCommand.addHelloAtEnd",
      "includeFiles": [
        { "regex": "/test/test-server.*\\.py" },
        { "regex": "/test/test-game.*\\.py" }
      ]
    }
  }

TODO