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

Ger*_*Cas 12 terminal reference built-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)
Run Code Online (Sandbox Code Playgroud)

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

> more C:\The\Path\to\File\MyFile.txt
Run Code Online (Sandbox Code Playgroud)

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

$ cat /The/Path/to/File/MyFile.txt
Run Code Online (Sandbox Code Playgroud)

Mar*_*ark 14

作为一种变通方法,您可以使用新的功能将变量发送${file}到具有此类键绑定的终端。在您的 keybindings.json 文件中添加:

{
  "key": "ctrl+shift+t",
  "command": "workbench.action.terminal.sendSequence",
  "args": { "text": "'${file}'\u000D" }
}
Run Code Online (Sandbox Code Playgroud)

然后,在终端中键入some_command并点击Ctrl- Shift-T将附加当前文件名并运行命令。

\u000D 是回报。

  • @Rohit如果你的意思是像我展示的特定键绑定一样,它会进入你的“keybindings.json”文件。或者您想问“sendSequence”具体记录在哪里? (2认同)

nvd*_*nvd 10

根据上述答案,仅当终端处于焦点时才激活:

{
  "key": "ctrl+shift+t",
  "command": "workbench.action.terminal.sendSequence",
  "args": { "text": "'${file}'\u000D" },
  "when": "terminalFocus"
}
Run Code Online (Sandbox Code Playgroud)