Multiple commands/tasks with Visual Studio Code

后端 未结 5 1918
清歌不尽
清歌不尽 2020-12-05 00:31

I have a local folder that I use as a scratch pad for multiple little sample and toy pieces of code. I store a host of python, C++, shell scripts etc. in this directory.

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-05 00:49

    You can always use bash as your task runner and then assign arbitrary terminal commands as your tasks.

    {
        "version": "0.1.0",
        "command": "bash",
        "isShellCommand": true,
        "showOutput": "always",
        "args": [
            "-c"
        ],
        "tasks": [
            {
                "taskName": "My First Command",
                "suppressTaskName": true,
                "isBuildCommand": true,
                "args": ["echo cmd1"]
            },
            {
                "taskName": "My Command Requiring .bash_profile",
                "suppressTaskName": true,
                "args": ["source ~/.bash_profile && echo cmd2"]
            },
            {
                "taskName": "My Python task",
                "suppressTaskName": true,
                "args": ["/usr/bin/python ${file}"]
            }
        ]
    }
    

    A few notes on what is happening here:

    • Using bash -c for all tasks by putting it in args list of the command so that we can run arbitrary commands. The echo statements are just examples but could be anything executable from your bash terminal.
    • The args array will contain a single string to be passed to bash -c (separate items would be treated as multiple arguments to the bash command and not the command associated with the -c arg).
    • suppressTaskName is being used to keep the taskName out of the mix
    • The second command shows how you can load your ~/.bash_profile if you need anything that it provides such as aliases, env variables, whatever
    • Third command shows how you could use your Python command you mentioned

    This will not give you any sort of file extension detection, but you can at least use cmd+p then type "task " to get a list of your tasks. You can always mark your 2 most common commands with isBuildCommand and isTestCommand to run them via cmd+shift+b or cmd+shift+t respectively.

    This answer has some helpful information that might be useful to you as well.

提交回复
热议问题