简体   繁体   中英

How do I pipe output into Visual Studio Code?

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?

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

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

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.

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 goofy 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.

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.

I made an extension for VSCode that solves this problem while having VSCode still open

https://marketplace.visualstudio.com/items?itemName=YuvrajMishra.vscode-pipe

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM