简体   繁体   English

我如何 pipe output 进入 Visual Studio 代码?

[英]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.我想将 pipe 的 output 的命令转换成 Visual Studio Code 中的新文本 window。

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. Visual Studio Code 启动,但不显示输入。 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:从 1.19.1 版开始,您可以通过调用将输出通过管道传输到当前窗口:

<command> | code -

If you are using version 1.19 or earlier, you don't need the arg:如果您使用的是 1.19 或更早版本,则不需要 arg:

<command> | code

As of September 2016, it does not appear to be supported, but there's an open issue to implement it:截至 2016 年 9 月,它似乎不受支持,但有一个未解决的问题来实施它:

https://github.com/Microsoft/vscode/issues/6161 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.我在 Ubuntu Gnome 17.10 (Artful Aardvark) 上运行 Visual Studio Code v1.19.3。 Just piping to code is not enough to bin to stdin.仅管道到code不足以bin 到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.当我使用接受的答案时,控制台会被阻止,直到我关闭 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:由于我经常希望在 VS Code 中保持选项卡在我继续使用控制台时保持打开状态,因此我想出了这个愚蠢的解决方法:

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.它重定向到当前目录中的文件t ,告诉 VS Code 打开该文件,然后将其删除。 You will see the contents of the file in VS Code in a tab labeled t (deleted) .您将在 VS Code 中一个标签为t (deleted)的选项卡中看到该文件的内容。

A slight delay (1 second works for me) is needed if VS Code isn't already open:如果 VS Code 尚未打开,则需要稍微延迟(1 秒对我有用):

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

Notes笔记

  • I tested this on Windows 10 using either Git Bash or PowerShell 7.我使用 Git Bash 或 PowerShell 7 在 Windows 10 上对此进行了测试。
  • 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:您还可以使用终端扩展中的复制/管道,并在终端中使用cp2codetee2codetee2code不会终止管道链),如下所示:

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.如果您有多个 VS 代码 windows,它会显示您运行命令的 window 上的复制数据。

I made an extension for VSCode that solves this problem while having VSCode still open我为 VSCode 做了一个扩展,解决了这个问题,同时 VSCode 仍然打开

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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