有没有办法用 VS Code 任务运行命令? [英] Is there a way to run a command with VS Code tasks?

查看:34
本文介绍了有没有办法用 VS Code 任务运行命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 VS Code 中,我有一个扩展,我可以通过按 F1 并按名称搜索来运行它的命令.但是我想从任务(tasks.json)中自动运行它.我从键盘快捷键知道它的全名.

In VS Code, I have an extension whose commands I can run by pressing F1 and searching by name. However I would like to automatically run it from a task (tasks.json). I know its full name from keyboard shortcuts.

推荐答案

您可以使用 ${command:} 语法在 tasks.json 中运行有效命令:

You can run a valid command in tasks.json with the ${command:} syntax:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "echo",
            "command": "${command:editor.action.addCommentLine}",
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

上面的例子注释掉当前行

The above example comments out the current line

如果你想顺序/并行运行命令,你可以使用 dependsOn 属性来运行多个命令:

If you would like to run commands sequentially/parallel, you can use the dependsOn property to run multiple commands:

表示另一个任务的字符串或该任务所依赖的其他任务数组.

Either a string representing another task or an array of other tasks that this task depends on.

例如,假设有一个场景,您要复制当前所在的行,在上面将其注释掉,然后任意地,出于演示目的,立即将终端聚焦:

For example, let's say there's a scenario where you want to duplicate the current line you are on down, comment it out above, and arbitrarily, for demonstration purposes, focus the terminal immediately:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "create temporary duplicate line",
            "command": "${command:workbench.action.terminal.focus}",
            "dependsOn": [
                "duplicate line up",
                "comment line out"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "comment line out",
            "command": "${command:editor.action.addCommentLine}"
        },
        {
            "label": "duplicate line up",
            "command": "${command:editor.action.copyLinesUpAction}"
        }
    ]
}

假设您复制的行是:

<div></div>

任务将运行并实现:

<!-- <div></div> -->
<div></div>

然后专注于集成终端

您可以查看他们关于命令变量的文档和其他占位符语法,例如接收用户输入以创建动态任务,或使用 runOn 属性在文件夹启动时自动运行任务

You can review their documentation on command variables and other placeholder syntax, like taking in user input for creating dynamic tasks, or using the runOn property to automatically run a task on folder startup

这篇关于有没有办法用 VS Code 任务运行命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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