43
votes

I want to pipe the output of a command into a new text window in Visual Studio Code.

Normally, I'd do something like this:

echo foo | code

...but that appears to not work; Visual Studio Code launches, but it does not display the input. Is there a way to do piping on the command line?

4
Note: At this time there is a issue causes echo 123 | code - not work when you're using Remote extensions. - imba-tjd

4 Answers

60
votes

Since version 1.19.1, you can pipe your output to the current window by invoking:

<command> | code -

If you are using version 1.19 or earlier, you don't need the arg:

<command> | code
4
votes

As of September 2016, it does not appear to be supported, but there's an open issue to implement it:

https://github.com/Microsoft/vscode/issues/6161

3
votes

I'm on Ubuntu Gnome 17.10 (Artful Aardvark), and I run Visual Studio Code v1.19.3. Just piping to code is not enough to bin to stdin.

$ ps aux | code
Run with 'code -' to read from stdin (e.g. 'ps aux | grep code | code -').

You have to add the - operator:

$ ps aux | code -

That's working and opens a new text tab filled by the command output.

0
votes

When I use the accepted answer, the console is blocked until I close the corresponding tab in VS Code. Since I often want to keep the tab open in VS Code while I keep using the console, I came up with this workaround:

ls > t; code t; rm t

It redirects to file t in the current directory, tells VS Code to open that file, and then deletes it. You will see the contents of the file in VS Code in a tab labeled t (deleted).

A slight delay (1 second works for me) is needed if VS Code isn't already open:

ls > t; code t; sleep 1; rm t

Notes

  • I tested this on Windows 10 using either Git Bash or PowerShell 7.
  • Of course, be careful with the name you use for the throwaway file so you don't overwrite a real file.

Edit: I made a function for this to throw in my PowerShell profile.

Function Out-Code {
    do {
        $filename = New-Guid
    } while (Test-Path $filename)
    $input > $filename
    code $filename
    Start-Sleep 1
    Remove-Item $filename
}
Set-Alias oc Out-Code

Usage

ls | oc