Issue
VSCode runs the following commands in the integrated terminal when I press f5
or ctrl-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!
I want to run some extra commands along with this,
@echo off & cls & 'commands run by VSCode' & @echo on & echo.
Is there anyway to do this?
Thanks!
Solution
You Will Need to Create a Task:
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.
STEP-1: Create the Necessary Tasks JSON File:
Create a tasks.json
file in the .vscode
directory. You can use this command from your projects root:
~$ mkdir .vscode; touch .vscode/tasks.json
NOTE: "if you already have a
.vscode
dir, then just use the following:
$ touch ./.vscode/tasks.json
"
STEP-2: Create the Customized Task That Fits Your Needs:
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!"
}
]
}
Task are extremely customizable. The task that I defined in the example above shows you the most simplistic form of the type of task that does what you are asking for. It executes the shell command, defined in the command
property: ~$ echo Hello World!
. Theirs a catch here, the above works for Linux platforms, you appear to be using windows. I used the above because I am running Linux, so your task will look more like this:
{
// 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"
}
}
]
}
To take an in-depth look at the Microsoft Tasks API you should check out these two links. Both are from official Visual Studio, and Visual Studio Code sources AKA Microsoft :
Microsoft Tasks API Documentation
_______________________________________________________________
STEP-3 Bind a Key-Combination to Your Task:
The final-step is to bind your task to a Key-Combo. There are a couple ways to go about this. IMO the second is a better option, but if you use the first, open the file in V.S. Code so you get help from intellisense
's hints & auto complete.
The first method — Manually open the keybinding JSON file located @
~/.config/Code/User/keybindings.json
For the second method — Press the F1 key to open the quick menu drop down, and type KEYBOARD SHORTCUTS into the text input. You can see me do it in the picture below.
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 valueecho
, !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"
}
I used the key combo below to pick-one that was easy to remember, while having a low probability of being assigned, however, you can, and should, change it to something that works for you.
Answered By - JΛYÐΞV Answer Checked By - Mildred Charles (WPSolving Admin)