Shortcut for running terminal command in VS code
Asked Answered
A

6

49

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)

Avelin answered 12/10, 2018 at 19:38 Comment(1)
if you use a tool like webpack, your typescript file will be recompiled anytime you saveHandgrip
M
95

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.

Manganese answered 12/10, 2018 at 20:23 Comment(5)
Only downside to this is the keyboard shortcut fails silently if no terminal is open yet.Earthstar
is it possible to run sequence of commands via single shortcut? e.g. "open new terminal" && "run command"Dees
@Dees With a macro extension yes. See multi-command extension.Manganese
@Dees it is possible to run multiple commands. see my answer below. i did not try your exact sequence but should be doable.Consecrate
If you want to kill the currently running command, prefix the sequence with \u0003\n. This sends a Ctrl-C to the terminal.Rugg
S
25

You can accomplish this with VSCode tasks and then wire up your task to a keybinding. The downside to this approach is you have to have a tasks.json file in your workspace .vscode folder (it can't be global).

Here is an example where I wanted to open a file in a custom GitHub remote:

// tasks.json
{
  // See https://go.microsoft.com/fwlink/?LinkId=733558
  // for the documentation about the tasks.json format
  "version": "2.0.0",
  "tasks": [
      {
          "label": "Open in remote",
          "type": "shell",
          "command": "open https://github.custom.com/org/repo/blob/master/${relativeFile}#L${lineNumber}"
      }
  ]
}
// keybindings.json
{
  "key": "ctrl+o",
  "command": "workbench.action.tasks.runTask",
  "args": "Open in remote"
},

Here are some more VS Code variables you can use if you are curious: https://code.visualstudio.com/docs/editor/variables-reference

There is a long standing issue open here which should make this easier to do without tasks: https://github.com/microsoft/vscode/issues/871

Serif answered 24/7, 2019 at 18:32 Comment(1)
see also assign task to prelaunchtask in launch configuration: #43837361Bornu
D
5

I don't think vscode by default can do this, but you can try this extension. That work for me.

https://marketplace.visualstudio.com/items?itemName=mkloubert.vs-script-commands

Discretional answered 12/10, 2018 at 20:17 Comment(0)
C
3

As a variation on the accepted answer, note that it is also possible to set up hot-keys that run multiple commands via runCommands. Below is one that saves the current document, and moves the cursor, prior to invoking a script that operates on it. Open keybindings.json using "Open Keyboard Shortcuts (JSON)" and you could for example insert:

    {
        "key": "ctrl+shift+enter",
        "command": "runCommands",
        "args": {
            "commands": [
                "workbench.action.files.save",
                "cursorBottom",
                {
                    "command": "workbench.action.terminal.sendSequence",
                    "args": {
                        "text": "py -3 myscript.py ${file}\u000D"
                    }
                },
            ]
        },
        "when": "editorLangId == markdown"
    },

Consecrate answered 24/6, 2023 at 9:37 Comment(0)
F
2

in addition to @mark ..

"args": { "text": "npm run-script test | tee /dev/null \u000D" }

this way it will run any script including bash scripts, that doesn't conflict to their arguments (e.g try rsync without the tee)

Fluker answered 30/9, 2019 at 2:12 Comment(0)
A
1

You can create one new terminal, then run any command by workbench.action.terminal.sendSequence and don't forget close this new temp terminal by sending exit\u000D.

below is the config to add into your keybindings (Preferences: Open Keyboard Shortcuts (JSON)):

{
        "key": "ctrl+alt+p",
        "command": "runCommands",
        "args": {
            "commands": [
                {
                    "command": "openInIntegratedTerminal",
                },
                {
                    "command": "workbench.action.terminal.sendSequence",
                    "args": {
                        "text": "npm run build\u000Dexit\u000D"
                    }
                }
            ]
        }
    }
Agglomeration answered 25/10, 2023 at 5:43 Comment(1)
superb :)))))))Garganey

© 2022 - 2024 — McMap. All rights reserved.