Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shortcut for running terminal command in VS code

Is there a way to make a hotkey for running specific command in terminal? Say I want to compile my TypeScript files by hotkey and not to type to terminal "tsc" or any other variation of that command. (Edit: I know it is possible to recompile TS on save, but the question is still the same)

like image 356
artberry Avatar asked Oct 12 '18 19:10

artberry


People also ask

What is the shortcut for Run in terminal VS Code?

A faster way to launch the terminal is to use the default keyboard shortcut Ctrl +`.

What does Ctrl J do in VS Code?

You can also fold/unfold all regions in the editor with Fold All (Ctrl+K Ctrl+0) and Unfold All (Ctrl+K Ctrl+J).

What is Ctrl P in VS Code?

Quick file navigation Tip: You can open any file by its name when you type Ctrl+P (Quick Open). The Explorer is great for navigating between files when you are exploring a project. However, when you are working on a task, you will find yourself quickly jumping between the same set of files.

What does Ctrl d do in VS Code?

Ctrl+D selects the word at the cursor, or the next occurrence of the current selection. Tip: You can also add more cursors with Ctrl+Shift+L, which will add a selection at each occurrence of the current selected text.


1 Answers

Typically you would set up a build or another task or an npm script and then trigger that with a hotkey.

There is another new way to do it with send text to the terminal.

For example, try this in your keybindings (Preferences: Open Keyboard Shortcuts (JSON)):

 {   "key": "ctrl+alt+u",   "command": "workbench.action.terminal.sendSequence",   "args": {     "text": "node -v\u000D"   } } 

for an npm script:

{   "key": "ctrl+alt+u",   "command": "workbench.action.terminal.sendSequence",   "args": {     "text": "npm run-script test\u000D"   } } 

The first will run the node -v command (the \u000D is a return so it runs). I still recommend actually setting up a build task though, and then there are keychords for running your build task: Ctrl-shift-B. Or an npm script.

For example, if you had a more complex script to run, see how to bind a task to a keybinding or how to keybind an external command.


EDIT: As of v1.32 you can now do something like this:

{   "key": "ctrl+shift+t",   "command": "workbench.action.terminal.sendSequence",   "args": { "text": "tsc '${file}'\u000D" } } 

You can now use the built-in variables, like ${file}, with the sendSequence command in a keybinding. I wrapped ${file} in single quotes in case your directory structure has a folder with a space in the name. And \u000D is a return.

like image 107
Mark Avatar answered Sep 20 '22 21:09

Mark