Productivity on Visual Studio Code - Running scripts from keyboard shortcuts
Tags: vscode, productivity
Published: 2020-08-13 01:19:58

---
title: Productivity on Visual Studio Code - Running scripts from keyboard shortcuts
published: true
description: How to configure tasks & keyboard shortcuts for maximum speed on repetitive script calls
tags: vscode, productivity
cover_image: http://absfreepic.com/absolutely_free_photos/small_photos/red-racing-car-on-track-3504x2336_58299.jpg
---

Have you ever dealt with some scripts that you need to run over and over again via command line in a project? Some redeploy script, custom compilation or something? 

I was working on a project where it was necessary to recreate the database from scratch in a docker container every few moments and it was really boring to invoke the rebuild script by hand on the command line every time I changed its creation script. Here I share a simple tip that increased my productivity and development experience considerably.

Instead of typing the command in the CLI or right clicking it everytime it was needed to run it, it is possible to create a task and add a keyboard shortcut to it. 

By doing this, we don't need to access the CLI to invoke the script anymore. [It is even possible](https://github.com/NARKOZ/hacker-scripts/blob/master/fucking_coffee.rb) to make coffee [(taken from here)](https://www.stuff.co.nz/technology/digital-living/99164024/a-programmer-wrote-scripts-to-secretly-automate-his-job) with a keyboard shortcut without leaving Visual Studio Code! The only thing is that you will need a compatible coffee machine in your network :)

To create a task, open Visual Studio Code command pallete and search for ```> Tasks: Configure Tasks``` 

Then choose ```Create tasks.json file from template``` and pick one of the available templates, according to your needs. For example, I will use *Others* template:

![Creating a task](https://dev-to-uploads.s3.amazonaws.com/i/aa2sod0xahih66e6njj0.gif)

This will create the **.vscode/tasks.json** file, allowing you to configure tasks.

It is possible to do all kinds of stuff using Visual Studio Code tasks and you can find the full documentation [here](https://go.microsoft.com/fwlink/?LinkId=733558).

Now edit your tasks.json file to include your script in a object inside the tasks array:

```json
{
   "label" : "my nice task",
   "type" : "shell",
   "command" : "./myscript.sh"
}
```

After creating the task, you can version this file in your repository to share this configuration with other developers.

Now if you open the command pallete again and select **Tasks: Run Task**, you will be able to see and run your task from there. 

Now, it is necessary to configure the keyboard shortcut.

Open the command pallete (**Ctrl+Shift+P** or F1) again, type in "keyboard" and look for **Preferences: Open Keyboard Shortcuts (JSON)**

![JSON Keyboard configuration](https://dev-to-uploads.s3.amazonaws.com/i/co3j6fvvig04xhh784zh.png)

This will open the **keybindings.json** file. You can version or keep this file in a (gist)[gist.github.com]. There, you can define a new keybinding by clicking in the button on the bottom right or type in manually the configuration:

```
{
   "key": "ctrl+k 1",
   "command": "workbench.action.tasks.runTask",
   "args" : "my nice task"
}
```

Let's look what each parameter does:
- **key** : This is the sequence of keys that will be used to trigger your script. From the configuration in the example above, it will be necessary to press ctrl and k simultaneously and then press 1 (This is what is called a "chord").
- **command** : This is the reference (command id) to the internal command of Visual Studio Code that will be triggered by the keypress. Here, it is the command that runs a given task. The full list of commands can be found by opening **Default Keyboard Shortcuts (JSON)**.
- **args** : This is an argument to the command. In our case, it is the exact name of the custom task.

Save the keybindings file and you're good to go. Try pressing the key sequence that you just configured. 

There's a lot of customization options for keybindings and if you find it useful, I highly recommend checking out the docs on this feature of VSCode as well.