How to Assign Terminal Commands onto Keybindings in VS Code — Streamlining Your Debugging Process

Francis Mendez
The Startup
Published in
4 min readJun 25, 2019

Have you ever gotten sick of constantly typing rake console into your Terminal every time your project has a bug? Maybe your code doesn’t break as often as mine, does, but I’m sure you could still stand to streamline this debugging process.

My typical debugging process

Normally when I debug, I use the Rake task: rake console, or some custom variation of it, to open a Pry in my terminal and test/debug my code. Often times I find myself trying to edit my code, then check it out in pry, rinse and repeat, until the bug is fixed, or the feature I’m currently trying to implement is finished. Whatever the case, it sure is a hassle to have to type rake console, open a pry in my terminal, test, exit the pry, rewrite some code, type rake console, in my terminal, test, exit the pry — you get it by now. It’s very time consuming and tedious.

What I have found to help me streamline this process is a VS Code extension called ‘terminal-command-keys’, all lower-case.

click on the cog icon to bring up a menu; select ‘Keyboard Shortcuts’

VS Code already has built in key-binding capabilities. You can already set any keys you want to any command you find appropriate. Through the keybinding settings, you will find a list of prewritten commands which you can then edit over. Commands such as: ctrl- ~, which opens the in-console terminal.

VS Code’s many keyboard shortcuts; including their ‘Command’, and ‘When’ clauses
this { } icon opens the keybindings.json file

Furthermore, if you notice the button on the upper-right corner that looks like 2 curly braces ({}), that will open a keybindings.json file that’s empty for you to edit. Within this file, using a specific syntax that is shown in the VS Code documentation, you can set keybindings to any commands that VS Code can recognize. Commands written in this file will overwrite any pre-existing keybindings, which is helpful insofar as not having to check for pre-existing keybindings to make sure there aren’t any errors. It even lets you specify case arguments, known as When clauses, such that the command will only execute if certain conditions are true. These allow for highly customized key-bindings that can truly enhance your workflow.

an empty keybindings.json (left) and an example of keybindings (right)
click ‘Extensions` in the settings menu to bring up the Marketplace
search for ‘terminal-command-keys’

However, there’s one thing it can’t do right out of the box: Run Terminal Commands. But that’s okay, it does a lot on its own already, but we’re going to need an extension to achieve that truly optimal workflow I described earlier in the article. That’s where our friend ‘terminal-command-keys’ comes into play. Look it up in the VS Code extensions Marketplace, by Pete Kinnecom.

The Marketplace page for ‘terminal-command-keys’ which shows examples of syntax and other documentation

What terminal-command-keys allows you to do is utilize that previously mentioned keybindings.json file to create keybindings that run terminal commands. Its syntax is very simple to use. It’s the same regular syntax as other keybindings:

Example of a key binding using terminalCommandKeys

Type the keys you want to use to execute your command. Next is the command to run, which, when using this extension, will always be terminalCommandKeys.run. So far, everything is looking familiar, but you notice we are running our extension as the command, the next step is where we specify the terminal command to run, which can be anything. Essentially, it’s just going to type whatever you put in args into the console for you.

You can see in my example I put rake console as the terminal command: Now whenever I press cmd + ctrl + r, my terminal will open and execute that rake console command. Very streamlined, very optimal, very clean.

No more jumping your mouse between the terminal and text editor. Just press your keybinding and it’ll handle it for you. I find this to be very convenient when debugging and testing code. It’s highly customizable and can greatly streamline your debugging process.

--

--