Automate program starts with keybinds in Visual Studio Code
While developing my Golang applications, I needed a way to simplify the start of the application — I didn’t want to type go run .
every single time 🥱 So I created a little setup to automate this in Visual Studio Code 💡
There are existing solutions for this like air, but I had some issues where I did not receive all of my logs (maybe it was already fixed — but I didn’t test it since I created this solution). This was kind of annoying, so I thought a simple “Press a key → Start the application” should do the job.
Step 1: Create a tasks.json
in your current workspace
If you already have a tasks.json
inside the .vscode
folder, you can open the file and continue with the next step. Otherwise, execute the command Tasks: Configure Task
and select the Create tasks.json file from template
option.
After this, select the Others
task template.
This will create a .vscode
folder with a tasks.json
inside of it (don’t forget to include the folder in your .gitignore
if you want). The content of the file should look something like this:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "echo",
"type": "shell",
"command": "echo Hello"
}
]
}
Step 2: Configure the run task
You can remove the example task and create a run task inside the tasks
list. The actual command that needs to be executed to start your program depends on your program itself and the programming language used, so you may need to adapt the following task. This is an example for running a Golang application.
{
"label": "Run Program",
"type": "shell",
"command": "go",
"args": [
"run",
"."
]
}
Step 3: Create a keybind to execute the run task (only once)
Execute the command Preferences: Open Keyboard Shortcuts (JSON)
and you should see your keybinds in the form of a JSON file. Create these two new entries:
{
"key": "ctrl+clear",
"command": "workbench.action.tasks.runTask",
"args": "Run Program"
},
{
"key": "ctrl+insert",
"command": "workbench.action.tasks.restartTask",
"args": "Run Program"
}
Edit the key
property just the way you like it. The args
property should match the label
property of your run task, so it is useful to have the same label across all of your run tasks. With the runTask
command, you are initially starting the application. If you are running a web server, for example, the process will run till you closed the program. Therefore, you need a second keybind to restart a task, since running an active task will give you an error. I unfortunately didn’t find a way to start or restart the task, depending on whether it is already started with one single keybind 😐.
Result
Now you are able to start your program with a keybind. For every new project, you only need to create a new tasks.json
file or copy one from another project.
I would be happy to hear of your ways to accomplish the same! Maybe this could help me and the other people that are reading this story! 🤝