自定义命令:
-
打开 VS Code 并进入命令面板。你可以按下 F1 或 Ctrl+Shift+P(Windows 和 Linux)/ Cmd+Shift+P(Mac)打开命令面板。
-
在命令面板中输入“Preferences: Open Keyboard Shortcuts(JSON)”,然后选择打开。这将打开一个名为 keybindings.json 的文件,其中包含你的自定义键绑定。
-
在 keybindings.json 文件中,输入以下代码:
{
"key": "alt+x", // 你想要的快捷键
"command": "workbench.action.terminal.sendSequence",
"args": {
"text": "node $(echo \"${file}\" | sed 's/\\/\//g')\n"
},
"when": "editorTextFocus && !terminalFocus && !inDebugRepl && editorLangId == 'javascript'"
}
命令解释:
node $(echo \"${file}\" | sed 's/\\/\//g')\n
node :使用 nodejs 运行
$(echo \"${file}\" | sed 's/\\/\//g'): 将vscode的${file}转为字符串然后将其中的 '\\' 以及 '\'转换为'/'
效果: