59

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Craig Walker
  • 49,871
  • 54
  • 152
  • 212
  • Note: At this time there is a issue causes `echo 123 | code -` not work when you're using *Remote* extensions. – imba-tjd Dec 23 '19 at 23:24

5 Answers5

94

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
Carl Walsh
  • 6,100
  • 2
  • 46
  • 50
CodeMonkey
  • 4,067
  • 1
  • 31
  • 43
3

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rémi Becheras
  • 14,902
  • 14
  • 51
  • 81
2

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

Craig Walker
  • 49,871
  • 54
  • 152
  • 212
2

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
MarredCheese
  • 17,541
  • 8
  • 92
  • 91
  • Unfortunately this bug that `code -` does not exit when the input stream is closed or reaches end has not been fixed yet although it was reported multiple times. For example: https://github.com/microsoft/vscode/issues/66600 – pabouk - Ukraine stay strong Jul 17 '22 at 20:59
1

You can also use the Copy/Pipe from Terminal extension and use cp2code or tee2code (the tee2code doesn't terminate the piping chain) in the terminal like this:

ls ~ | cp2code
ls ~ | tee2code | sort

It opens a new document with the data piped into that. If you have multiple VS Code windows, it'll show you the copied data on that window you did run the command.

Babak
  • 131
  • 2