如何更改VSCode中集成的shell运行命令?

How to change the integrated shell run command in VSCode?

VSCode 运行 当我按 f5ctrl-f5,

时,集成终端中的以下命令
C:\Stuff\More Stuff> cmd /C "c:\Users\user\.vscode\extensions\ms-vscode.cpptools-1.4.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-4suak3sc.nfy --stdout=Microsoft-MIEngine-Out-pa0ujvmf.a5n --stderr=Microsoft-MIEngine-Error-v2fuqlu2.g0m --pid=Microsoft-MIEngine-Pid-0jy3ikvh.a3z "--dbgExe=C:\Program Files\CodeBlocks\TDM-GCC-64\bin\gdb.exe" --interpreter=mi "
Hello from my program!

我想 运行 一些额外的命令,

@echo off & cls & 'commands run by VSCode' & @echo on & echo.

有办法吗?

谢谢!

您需要创建一个任务:

The command you want to build, will have to be built using a task. Once built in a task, you can then bind it to which ever key configuration you like. Before I answered this I built a simple example to help demonstrate what I just said.



第 1 步:创建必要的任务 JSON 文件:

.vscode目录下创建tasks.json文件。您可以从项目根目录使用此命令:

~$ mkdir .vscode; touch .vscode/tasks.json

NOTE: "if you already have a .vscode dir, then just use the following:
  • $ touch ./.vscode/tasks.json"




第 2 步:创建满足您需求的自定义任务:

Tasks are like creating complicated keybinding (well sort-of), its more like a complex keybinding that took steroids, and can do a bunch of stuff keybindings cannot do. All BS aside, it is an extremely powerful tool. V.S. Code users that are not using it, are missing out on one of the most powerful features that V.S. Code offers. Anyhow, this is how you create a task that, in-a-nutshell, defines and executes a shell command.

    /** @filename "./.vscode/tasks.json" */

    {
        "version": "2.0.0",
        "tasks": [ 
            {
                "label": "echo",
                "type": "shell",
                "command": "echo Hello World!"
            }
        ]
    }


任务可高度自定义。我在上面的示例中定义的任务向您展示了执行您要求的任务类型的最简单形式。它执行 shell 命令,在 command 属性 中定义:~$ echo Hello World!。他们在这里有一个问题,以上适用于 Linux 平台,您似乎正在使用 windows。我用上面的是因为我是运行Linux,所以你的任务看起来更像这样:

{
    // Same task as above, but uses the windows property so it will run on                        
    // windows platforms
  
      "version": "2.0.0",
      "tasks": [
        {
          "label": "echo",
          "type": "shell",
          "command": "echo Hello World!",
          "windows":{
              "command": ".\scripts\test.cmd"
          }
        }
      ]
    }

要深入了解 Microsoft Tasks API,您应该查看这两个链接。两者均来自官方 Visual Studio 和 Visual Studio 代码源 AKA Microsoft :

Microsoft Tasks API Documentation

V.S. Code: Tasks Explained


_______________________________________________________________

第 3 步将组合键绑定到您的任务:

最后一步是将您的任务绑定到组合键。有几种方法可以解决这个问题。 IMO 第二个是更好的选择,但如果您使用第一个,请在 V.S 中打开文件。代码,这样你就可以从intellisense的提示和自动完成中获得帮助。

  1. 第一种方法——手动打开位于@~/.config/Code/User/keybindings.json

    的键绑定JSON文件
  2. 对于第二种方法——按 F1 键打开快捷菜单下拉菜单,然后在文本输入框中键入 KEYBOARD SHORTCUTS。你可以在下面的图片中看到我做的。



Whether you opened the keybinding.json file using method 1, or method 2, doesn't matter after it is open, so long as you have it open (and you opened the right file) there are two keybinding files, look at the picture, and make sure to open the one with JSON next to it.

Now below is the code for attaching your keybinding. Create a block in the keybinding.json file, just like the one below. To clarify, args has the value echo, !NOT because it is going to execute the echo command, but because the task's label is echo. When you use the Keybinding command "workbench.action.tasks.runTask", the keybinding will always take an argument, and the arg's value will always be the task's label.

  {
    "key": "ctrl+shift+enter ctrl+shift+enter",
    "command": "workbench.action.tasks.runTask",
    "args": "echo"
  }

我使用下面的组合键选择了一个容易记住的组合,同时被分配的可能性很低,但是,您可以而且应该将其更改为适合您的组合。