Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 416
Ger Cas Avatar asked Sep 01 '17 16:09

Ger Cas


People also ask

How do you jump to a file in VS Code?

VS Code provides two powerful commands to navigate in and across files with easy-to-use key bindings. Hold Ctrl and press Tab to view a list of all files open in an editor group. To open one of these files, use Tab again to pick the file you want to navigate to, then release Ctrl to open it.

How do you select a current word in VS Code?

Ctrl+D selects the word at the cursor, or the next occurrence of the current selection. Tip: You can also add more cursors with Ctrl+Shift+L, which will add a selection at each occurrence of the current selected text.


2 Answers

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.

like image 84
Mark Avatar answered Sep 21 '22 02:09

Mark


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"
}
like image 20
nvd Avatar answered Sep 21 '22 02:09

nvd