Run Current File With Babel Node and Vscode

A lot of times I’ll open up a scratch file and run it through babel-node to see how it works while spiking out an idea or testing something that’s just awkward to test initially otherwise. I wanted to see if there was some good way to get it to run with a keyboard shortcut in VSCode, and I managed to find it! What you want is to bind the workbench.action.terminal.sendSequence command.

To get it set up, open the command palette (Ctrl+Shift+P) and search for Preferences: Open Keyboard Shortcuts (JSON). Now that you have your keyboard shortcuts file open, add a new entry by hitting Ctrl+K Ctrl+K. It’ll prompt you to enter the shortcut you want to use. I use Ctrl+r f. Once you enter the mapping, hit Enter and VSCode will add a new keybinding entry to the file. Update that entry to look like this:

1
2
3
4
5
6
{
  "key": "ctrl+r f",
  "command": "workbench.action.terminal.sendSequence",
  "args": { "text": "npx babel-node ${file}\n" },
  "when": "editorTextFocus"
}

That keybinding basically says “When I hit ctrl+r f while in the editor, run npx babel-node on my current file. It’s a simple trick, but it can really help save time vs. jumping back and forth to the terminal to run a command on the file.

Love it? Hate it? Have something to say? Let me know at comments@nalanj.dev.