如何在 Visual Studio 代码中从集成终端引用当前文件

How to refer to current file from Integrated Terminal in Visual Studio Code

我想知道是否可以(使用内置变量)从集成终端直接处理在 Visual Studio 中打开的当前文件, 例如:

>some_command $current_file   (Where $current_file would be a built-in variable that calls the current active file)

而不是如果终端是 CMD (DOS) 我现在必须做的事情:

> more C:\The\Path\to\File\MyFile.txt

或者如果使用的终端是bash:

$ cat /The/Path/to/File/MyFile.txt

作为变通方法,您可以使用新功能通过此类键绑定将 ${file} 等变量发送到终端(请参阅 vscode docs)。在您的 keybindings.json 文件中添加:

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

然后,在终端输入 some_command 并按下 Ctrl-Shift-T 和当前文件名将被附加和命令 运行.

\u000D 是一个 return.

基于上述答案,只有当终端处于焦点时才激活:

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