batch-filevisual-studio-codevscode-tasksfinite-element-analysis

How can I set a shorcut key in VS code to run a .bat file in my working directory?


In my research I work with the finite element method, we use the solver from Abaqus which uses subroutines made with Fortran and I use vs code to edit them. So to run my codes I made a batch file that makes this process simpler (to avoid typing many instructions in the cmd).

In this image you can see the files in my working directory, it would save me a lot of time if I could set a shortcut to execute the principal.bat file and another one to terminate.bat directly from vs code: batch files I appreciate any help, I've tried to set a task.json file for this purpose, but I am a little lost.


Solution

  • You can use the extension multi-command to type the batch file command to the terminal

    Add this to your settings.json:

      "multiCommand.commands": [
        {
          "command": "multiCommand.callPrincipal",
          "sequence": [
            { "command": "workbench.action.terminal.sendSequence",
              "args": { "text": "principle.bat\u000D" }
            }
          ]
        },
        {
          "command": "multiCommand.callTerminate",
          "sequence": [
            { "command": "workbench.action.terminal.sendSequence",
              "args": { "text": "terminate.bat\u000D" }
            }
          ]
        }
      ]
    

    And then define 2 keybindings in keybindings.json

      {
        "key": "shift+alt+F1",  // or any other key combo
        "command": "multiCommand.callPrincipal"
      },
      {
        "key": "shift+alt+F2",  // or any other key combo
        "command": "multiCommand.callTerminate"
      }
    

    You can add when clauses to limit the validity of the keybinding


    Edit

    It is not needed to use the multi-command extension when you only want 1 terminal command, you can define the argument to the keybinding

      {
        "key": "shift+alt+F1",  // or any other key combo
        "command": "workbench.action.terminal.sendSequence",
        "args": { "text": "principle.bat\u000D" }
      },
      {
        "key": "shift+alt+F2",  // or any other key combo
        "command": "workbench.action.terminal.sendSequence",
        "args": { "text": "terminate.bat\u000D" }
      }
    

    Or you can define 2 tasks to start the batch files and run them with Terminal | Run Task....

    If you designate one as the build task you can use the shortcut key for that.

    I can't find a command that runs a named task, so it can be used in a keybinding.