多条命令/使用Visual Studio code任务 [英] Multiple commands/tasks with Visual Studio Code

查看:2455
本文介绍了多条命令/使用Visual Studio code任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我为code的多个小样本,玩具枚便签使用本地文件夹中。我店蟒蛇,C ++,shell脚本等主机此目录中。

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.

我使用(在OS X)的Visual Studio code和期待到其任务的运行/编译code片段,而无需切换到终端。

I'm using Visual Studio Code (on OS X) and am looking into its tasks to run/compile the code snippets without having to switch to a terminal.

例如,我发现此以下任务将当前打开的文件运行Python。

For example, I found this following task will run python on the currently open file.

// A task runner that runs a python program
{
    "version": "0.1.0",
    "command": "/usr/bin/python",
    "args": ["${file}"]
}

这个任务将使用Python作为文件我目前正在编辑类型的任务,不论亚军

This task will use python as the task runner irrespective of the type of file I'm currently editing.

我如何实施运行基于文件类型的命令的任务(或多个命令之间进行选择)?即如果我在编辑一个C ++文件,它将运行铛++。

How do I implement a task to run a command based on the file type (or select between multiple commands)? I.e. if I'm editing a C++ file, it will run clang++.


  • 如果根据文件类型,我不能做到这一点;有什么替代呢?

  • 另一种方法是;在多个命令支持?

推荐答案

您可以随时使用bash作为你的任务亚军,然后分配任意终端命令为您的任务。

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:


  • 使用bash -c 通过将它放在 ARGS 列表中的命令的所有任务,使我们可以运行任意命令。在回声语句只是例子,但可能是从你的bash终端的任何可执行文件。

  • ARGS 数组将包含一个字符串被传递到的bash -c (单独项目将被视为为多个参数与 -c ARG)。
  • 相关bash命令,而不是命令
  • 燮pressTaskName 被用来保持 TASKNAME 出混合

  • 第二个命令显示如何加载〜/ .bash_profile中如果你需要什么,它提供了诸如别名,ENV变量,无论

  • 第三个命令显示了如何使用你提到你的Python命令

  • 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

这不会给你任何类型的文件扩展名的检测,但你至少可以使用<大骨节病> CMD + <大骨节病> P 然后键入任务来获取你的任务列表。您可以随时与 isBuildCommand isTestCommand 通过运行它们<大骨节病> CMD + <大骨节病>移 + <大骨节病>乙或<大骨节病> CMD + <大骨节病>移动 + <大骨节病> T

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.

这篇关于多条命令/使用Visual Studio code任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆