Answer a question

How would I have vscode auto run 2 separate commands upon file saving? Would be for formatting document and auto sorting imports.

Answers

There is no default support for running Tasks, or Commands, on save (onSave). However, there is 3rd party support via the VSCode extension "Run on Save".

Here is the link: https://marketplace.visualstudio.com/items?itemName=emeraldwalk.RunOnSave

This is a very popular extension, I used it once a while back and it worked well for my use-case.

Also if you just want to lint, or format code, on save that can be done through VSCode settings.

EDIT June 20th 9:04pm UTC

If you want to add the command to your settings you have to configure a the extension, which is explained in the extensions README.md, that can be viewed by clicking on the extensions icon in the VSCode extensions explorer.

Configuration

Add "emeraldwalk.runonsave" configuration to user or workspace settings.

  • "shell" - (optional) shell path to be used with child_process.exec options that runs commands.

  • "autoClearConsole" - (optional) clear VSCode output console every time commands run. Defaults to false.

  • "commands" - array of commands that will be run whenever a file is saved.

    • "match" - a regex for matching which files to run commands on

    NOTE Since this is a Regex, and also in a JSON string backslashes have to be double escaped such as when targetting folders. e.g. "match": "some\\\\folder\\\\.*"

    • "cmd" - command to run. Can include parameters that will be replaced at runtime (see Placeholder Tokens section below).
    • "isAsync" (optional) - defaults to false. If true, next command will be run before this one finishes.

Sample Config

This sample configuration will run echo statements including the saved file path. In this sample, the first command is async, so the second command will get executed immediately even if first hasn't completed. Since the second isn't async, the third command won't execute until the second is complete.

"emeraldwalk.runonsave": {
    "commands": [
        {
            "match": ".*",
            "isAsync": true,
            "cmd": "echo 'I run for all files.'"
        },
        {
            "match": "\\.txt$",
            "cmd": "echo 'I am a .txt file ${file}.'"
        },
        {
            "match": "\\.js$",
            "cmd": "echo 'I am a .js file ${file}.'"
        },
        {
            "match": ".*",
            "cmd": "echo 'I am ${env.USERNAME}.'"
        }
    ]
}

Configuring a command to execute when you save, isn't going to be as simple as hitting [F1] to open the quick-input, and finding the extensions task that you want to run. You will have to follow the instructions, and when you get stuck, you might have to come back and ask a question about the issue you are experiencing, or google it however; if your a Linux user, or you have a lot of MS-Dos or Powershell experience, this will be pretty straight forward.

Logo

开发云社区提供前沿行业资讯和优质的学习知识,同时提供优质稳定、价格优惠的云主机、数据库、网络、云储存等云服务产品

更多推荐