Home > other >  How to open a file from the integrated terminal in VSCode to a new tab
How to open a file from the integrated terminal in VSCode to a new tab

Time:06-04

If my script is run within vscode, it want it to open a .txt file in a new tab in vscode. Else, open the folder containing the file. However, the current "code" command opens it in the terminal window instead of a new edit tab.

if ($env:TERM_PROGRAM -eq 'vscode') {
  code 'C:\temp\_Release_Evidence\test.txt'
}
else {
  explorer 'C:\temp\_Release_Evidence'
}

CodePudding user response:

Normally, code refers Visual Studio Code's CLI, which is assumed to be in one of the directories listed in $env:PATH:

  • On Windows, it refers to the code.cmd batch file that serves as the CLI entry point.
  • On Unix-like platforms it refers to a code Bash script.

Its default behavior is to open a given file as a new tab in the most recently activated Visual Studio Code window (which, when run from inside Visual Studio Code, is by definition the current window).

If that doesn't happen for you, perhaps code refers to a different executable on your machine:

  • To avoid ambiguity, use the full CLI path instead, which, however, requires you to know Visual Studio Code's install location; typical CLI locations are:

    • Windows: $env:LOCALAPPDATA\Programs\Microsoft VS Code\bin\code.cmd
    • macOS: /usr/local/bin/code
    • Linux: /usr/bin/code
  • On Windows, something as simple as including the filename extension in the invocation - i.e., code.cmd - may help.


However, assuming you're using the PIC (PowerShell Integrated Console), a specialized PowerShell shell that comes with the PowerShell extension for Visual Studio Code, a workaround that also performs better, because it doesn't require launching a child process:

  • The PIC comes with the psedit command (an alias for the Open-EditorFile function), which quickly opens one or more files in a tab in the current Visual Studio Code window.

    • Caveat: As of version v2022.5.1 of the PIC, specifying multiple files only works meaningfully if they are individually enumerated, as literal paths. If you try to use a wildcard pattern or a directory path, all matching files / files in the directory are uselessly opened in sequence in a single tab.

Thus, you could use the following:

if ($env:TERM_PROGRAM -eq 'vscode') {
  # Use `psedit`, if available; `code` otherwise.
  $exe = if ((Get-Command -ErrorAction Ignore psedit)) { 'psedit' } else { 'code' }
  & $exe 'C:\temp\_Release_Evidence\test.txt'
}
else {
  explorer 'C:\temp\_Release_Evidence'
}

CodePudding user response:

I can't reproduce this or explain why this might occur on your system. Running the following whether in the PowerShell Integrated Terminal (which @mklement0 explained succinctly) or a standard PowerShell terminal in VS Code's Terminal pane should open the file in a new tab where file contents are normally displayed:

code /path/to/file.txt

A suitable workaround may be to get the contents of a text file and pipe them in via STDIN. We can do this by adding a hyphen - as an empty parameter to code when piping data to it:

# Tip: Use the gc alias for Get-Content
Get-Content /path/to/file.txt | code -

You can then use Save As... to save the file to its intended target once you make your changes. You will need to use Ctrl C in the terminal to close the input stream if you need to run additional commands before closing the file or saving to a one.


Even if this isn't a suitable workaround for you, it's a handy tip in other scenarios. For example, the following command will open documentation for Get-Process inside VSCode:

Reminder: Don't forget to hit Ctrl C in the terminal once the content finishes populating to be able to run additional commands, or close the temporary file buffer.

Get-Help Get-Process -Detailed | code -
  • Related