Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute a bat file on keypress in VsCode

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!

like image 958
Figwig Avatar asked Jan 21 '20 16:01

Figwig


People also ask

How do I run bat file in VS Code?

Then, use the Tasks: Run Build Task hotkey ( Ctrl Shift B by default). You can have more than one such task. At most one task can have "isDefault": true , the one that Ctrl Shift B should run.

How do you add a run button in VS Code?

To bring up the Run and Debug view, select the Run and Debug icon in the Activity Bar on the side of VS Code. You can also use the keyboard shortcut Ctrl+Shift+D.


2 Answers

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
like image 109
HolyBlackCat Avatar answered Oct 14 '22 04:10

HolyBlackCat


{
  "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
    }
  }
like image 27
Mark Avatar answered Oct 14 '22 04:10

Mark