在 VS 代码中运行终端命令的快捷方式 [英] Shortcut for running terminal command in VS code
问题描述
有没有办法为在终端中运行特定命令制作热键?假设我想通过热键编译我的 TypeScript 文件,而不是输入到终端tsc"或该命令的任何其他变体.(我知道可以在保存时重新编译 TS,但问题仍然相同)
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. ( I know it is possible to recompile TS on save, but the question is still the same)
推荐答案
通常,您会设置构建或其他任务或 npm 脚本,然后使用热键触发.
Typically you would set up a build or another task or an npm script and then trigger that with a hotkey.
还有另一种新方法可以使用 send文本到终端.
There is another new way to do it with send text to the terminal.
例如,在您的键绑定中尝试此操作(首选项:打开键盘快捷键 (JSON)):
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"
}
}
或者对于 npm 脚本:
or for an npm script:
{
"key": "ctrl+alt+u",
"command": "workbench.action.terminal.sendSequence",
"args": {
"text": "npm run-script test\u000D"
}
}
第一个将运行 node -v
命令(\u000D
是一个返回,所以它会运行).我仍然建议实际设置一个构建任务,然后有运行构建任务的键:Ctrl-shift-B.或者一个 npm 脚本.
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.
编辑:从 v1.32 开始,您现在可以执行以下操作:
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" }
}
您现在可以在键绑定中使用内置变量,例如 ${file}
,以及 sendSequence
命令.我将 ${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.
这篇关于在 VS 代码中运行终端命令的快捷方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!