13

I'm writing shaders for vulkan, which have to be compiled into spir-v. I have a very nice batch file that will go through and build my shaders for me using the GLSlangvalidator. I'm trying to get a keypress to run my batch file in VsCode so I can check my code for errors and so that it is built. I have the following:

    {
    "key": "f5",
    "label": "build",
    "type": "shell",
    "command": "workbench.action.terminal.sendSequence",
    "args" : {"text": ".\\compile.bat"},
    "presentation" : {
        "reveal": "always"
    }

This nearly works - but I still have to focus on the inbuilt terminal panel and hit enter. Surely there is a way to execute a command, rather than to just input the string? Thanks!

Figwig
  • 592
  • 1
  • 3
  • 16
  • Is your question, how do I run a batch file with its `cmd.exe` window topmost?, in focus? or both? The idea of this site is that we help you to fix an issue with the code you have written. The code you have posted doesn't really exhibit an issue, the problem is that you haven't attempted to code it. I would advise you to search for reference material and/or ask on a more suitable platform, perhaps [Super User](https://superuser.com/questions/ask). – Compo Jan 21 '20 at 16:56
  • My question is to have Visual Studio Code run a batch file on command. My above code successfully adds the required command to the inbuilt terminal, but doesn't execute. If you're not familiar with VSc, there's an inbuilt terminal which runs as a cmd window would, that you can execute various tasks on, most of which you can program in with scripts like the above (I believe it is in json) – Figwig Jan 21 '20 at 19:11
  • TBF, the inbuilt terminal runs any one of many shells depending upon configuration settings. e.g. `"C:\Windows\System32\cmd.exe"`, `"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"`, `"C:\Program Files\Git\bin\bash.exe"`, or `"C:\Windows\System32\bash.exe"`. I would assume that the ability to run and interact with that terminal is also a configuration option, so your question may be about software settings, not about programming as such. – Compo Jan 21 '20 at 19:53
  • As can be seen from the already posted answer, and intimated in my previous comment, the issue is with configuration settings inside the JSON file, not a programming issue. Your question is therefore off topic on StackOverflow. – Compo Jan 21 '20 at 21:52

2 Answers2

19

In tasks.json, create a task to run the .bat. Something like this:

{
    "label": "MY_TASK",
    "type": "shell",
    "command": "MY_BAT_FILE.bat",
    "presentation": {"echo": true, "reveal": "always", "focus": false, "panel": "shared", "showReuseMessage": false, "clear": true},
    "group": {"kind": "build", "isDefault": true},
},

Then, use the Tasks: Run Build Task hotkey (CtrlShiftB by default).


You can have more than one such task.

At most one task can have "isDefault": true, the one that CtrlShiftB should run.

You can assign custom hotkeys to those tasks, by adding following to your keybindings.json:

{"key": "f5", "command": "workbench.action.tasks.runTask", "args": "MY_TASK"},
//       ^~ key                                                     ^~~~~~~ task name
HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
3
{
  "key": "alt+5",
  // "label": "build",               // no effect in a keybinding
  // "type": "shell",                // no effect in a keybinding
  "command": "workbench.action.terminal.sendSequence",
  "args": {
    "text": ".//test.bat\u000D"
  // },
  // "presentation": {              // no effect in a keybinding
    // "reveal": "always"
  // },
},

Those keys and values I commented out have no effect in this keybinding. They would go into a task. If those are important to you, then you should go with the task approach in the other answer. If all you want to do with presentation is make sure the terminal opens you could combine the sendSequence call with terminal.focus in a macro keybinding like:

{
  "key": "alt+3",
  "command": "extension.multiCommand.execute",
  "args": {
    "sequence": [
      "terminal.focus",
      {
        "command": "workbench.action.terminal.sendSequence",
        "args": {
          "text": ".//test.bat\u000D"
        }
      }
    ]
  },
}

Otherwise you were very close to your initial keybinding. Just add a return to the end of the text sent to the terminal by using the unicode character \u000D and it will run immediately.

So if you manually opened the terminal before or after triggering the keybinding, this is enough:

  {
    "key": "alt+5",
    "command": "workbench.action.terminal.sendSequence",
    "args": {
      "text": ".//test.bat\u000D"   // these path separators work for me on Windows
    }
  }
Mark
  • 143,421
  • 24
  • 428
  • 436