DEV Community

Cover image for Productivity on Visual Studio Code - Running scripts from keyboard shortcuts
Varlen Pavani Neto
Varlen Pavani Neto

Posted on

Productivity on Visual Studio Code - Running scripts from keyboard shortcuts

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 to make coffee (taken from here) 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

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.

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

{
   "label" : "my nice task",
   "type" : "shell",
   "command" : "./myscript.sh"
}
Enter fullscreen mode Exit fullscreen mode

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

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"
}
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (1)

Collapse
 
slushnys profile image
Zigmas Slušnys

Pretty cool tutorial and it's use case!