Run custom commands as run configurations in Visual Studio Code
Asked Answered
P

2

5

I'm trying to figure out how to make a custom run configuration in Visual Studio Code, and am not succeeding in finding any documentation describing my use case.

I want to make a Run Configuration that runs arbitrary commands that I can set to run my code. This is neccecary because the language I am using doesn't have extensions providing run configurations.

I did find that this is possible with tasks, but I cannot figure out how to run a task by pressing F5 (like you would with a run configuration).

So, this is what I am looking to do: Define something that will run a command (run.exe ${currently selected VSCode file}) when I press F5.

Pieper answered 23/2, 2021 at 11:44 Comment(0)
A
8

Define this task and put it in .vscode/tasks.json:

{
  "label": "Run current",
  "type": "shell",
  "command": "run.exe ${file}"
}

Then add an F5 keybinding in keybindings.json

{ "key": "F5", "command": "-workbench.action.debug.start" },
{ "key": "F5", "command": "-workbench.action.debug.continue" },
{
  "key": "F5",
  "command": "workbench.action.tasks.runTask",
  "args": "Run current"
}
Aeropause answered 23/2, 2021 at 13:15 Comment(6)
where does the first go?Sarsaparilla
@Sarsaparilla place it in your .vscode/tasks.jsonAeropause
To avoid being prompted for result scanning, I included "problemMatcher": [] per https://mcmap.net/q/765947/-fix-quot-continue-without-scanning-the-task-output-quot-in-visual-studio-code.Foliaceous
@Aeropause overriding global shortcuts for project level tasks?!Rokach
@AntonDuzenko if the OP want to use F5, be my guest, he can also choose a different shortcut like F20 (if he has that key), the global shortcuts are suggestions you can completely redefine all of them.Aeropause
Note that you can restrict keybindings to only be active in specific folders, as described over on https://mcmap.net/q/346053/-a-keybindings-json-per-workspace-in-visual-studio-code (which is not great, but better than a global override)Sf
H
0

I handled this by putting my commands in a Python file build.py, and then defining .vscode/launch.json to be:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Build",
            "type": "debugpy",
            "request": "launch",
            "program": "build.py",
            "console": "integratedTerminal"
        }
    ]
}

That way whenever I press F5 it starts a Python debugger that executes build.py, no matter what current file I have open in VS Code.

Heid answered 27/9, 2024 at 13:8 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.