1

我正在尝试模仿 Sublime Text 的功能,在这里我可以创建一个“构建系统”,允许用户在终端中运行一些命令,然后是当前打开的文件的路径。

例如,我可以制作一些基本脚本,点击cmd + b并在集成窗口中查看输出(附截图)。

在 Sublime Text 中构建 JavaScript 系统

有什么方法可以创建一个快捷方式,例如传递给node {currentlyFocusedFile}VSCode 中的集成终端?

4

2 回答 2

2

创建一个tasks.json作为"node"命令的。使用变量之一${file}${relativeFile}作为参数以传递当前文件。

这是一个示例tasks.json

{
    "version": "0.1.0",
    "command": "node",
    "isShellCommand": true,
    "args": [],
    "showOutput": "always",
    "echoCommand": true,
    "suppressTaskName": true,
    "tasks": [
        { 
            "taskName": "execNodeWithAbsolutePath",
            "args": ["${file}"]
        },
        {
            "taskName": "execNodeWithRelativePath",
            "args": ["${relativeFile}"]
        }
    ]
}

在此处阅读有关该主题的更多信息:https ://code.visualstudio.com/Docs/editor/tasks

于 2017-01-17T15:44:36.407 回答
0

由于发布了问题和答案,因此将已解析变量作为命令的一部分发送到终端的功能已添加到 vscode。请参阅https://code.visualstudio.com/updates/v1_32#_variable-support-in-send-sequence-command

发送序列命令中的变量支持

现在可以在 workbench.action.terminal.sendSequence命令中使用变量,例如:

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

所以你可以做

{   
  "key": "ctrl+shift+t",                // whatever keybinding you want
  "command": "workbench.action.terminal.sendSequence",
  "args": { 
    "text": "node ${file}"  // or "node ${relativeFile}"
  }
}
于 2021-08-23T04:19:04.763 回答