Ger Cas
Ger Cas

Reputation: 2298

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

I want to know if it is possible (with a built in variable) to work directly with current file opened in Visual Studio from intergrated terminal, for example:

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

instead of what I have to do now if terminal is CMD (DOS):

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

Or if the terminal used is bash:

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

Upvotes: 24

Views: 7813

Answers (2)

Mark
Mark

Reputation: 181339

You could, as a workaround, use the new abilty to send variables like ${file} to the terminal with such a keybinding (see vscode docs). In your keybindings.json file add:

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

Then, in the terminal type some_command and hit Ctrl-Shift-T and the current filename will be appended and the command run.

\u000D is a return.

Upvotes: 28

nvd
nvd

Reputation: 3371

Based on the above answer with activation only when the terminal is in focus:

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

Upvotes: 12

Related Questions