【问题标题】:How do I pipe output into Visual Studio Code?如何将输出通过管道传输到 Visual Studio Code?
【发布时间】:2017-01-18 00:15:13
【问题描述】:

我想将命令的输出通过管道传输到 Visual Studio Code 中的新文本窗口中。

通常,我会这样做:

echo foo | code

...但这似乎行不通; Visual Studio Code 启动,但不显示输入。有没有办法在命令行上做管道?

【问题讨论】:

  • 注意:此时有一个问题导致echo 123 | code - 在您使用Remote 扩展程序时不起作用。

标签: pipe stdin visual-studio-code


【解决方案1】:

从 1.19.1 版开始,您可以通过以下方式将输出通过管道传输到当前窗口:

<command> | code -

如果您使用的是 1.19 或更早版本,则不需要 arg:

<command> | code

【讨论】:

  • 我必须使用 &lt;command&gt; | code - 否则我会收到此消息:Run with 'code -' to read from stdin (e.g. 'ps aux | grep code | code -').
  • @GuiSim 对我来说它没有工作,不知道有什么区别。
  • @CodeMonkey:不同的是v1.19.1的变化:code.visualstudio.com/updates/v1_19
  • @CraigWalker 现在编辑了我的回复。
【解决方案2】:

截至 2016 年 9 月,它似乎不受支持,但实施它存在一个未解决的问题:

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

【讨论】:

    【解决方案3】:

    我在 Ubuntu Gnome 17.10 (Artful Aardvark) 上运行 Visual Studio Code v1.19.3。仅通过管道连接到 code 不足以将其放入标准输入。

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

    您必须添加- 运算符:

    $ ps aux | code -
    

    这可以工作,并打开一个由命令输出填充的新文本选项卡。

    【讨论】:

    【解决方案4】:

    当我使用接受的答案时,控制台会被阻止,直到我关闭 VS Code 中的相应选项卡。由于在继续使用控制台时,我经常希望在 VS Code 中保持选项卡处于打开状态,因此我想出了这个解决方法:

    ls > t; code t; rm t
    

    它重定向到当前目录中的文件t,告诉VS Code打开该文件,然后将其删除。您将在 VS Code 中标记为 t (deleted) 的选项卡中看到文件的内容。

    如果 VS Code 尚未打开,则需要稍许延迟(我需要 1 秒):

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

    注意事项

    • 我使用 Git Bash 或 PowerShell 7 在 Windows 10 上对此进行了测试。
    • 当然,要小心使用一次性文件的名称,以免覆盖真实文件。

    编辑:我为此创建了一个函数以将其放入我的 PowerShell 配置文件中。

    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
    

    用法

    ls | oc
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-13
      • 1970-01-01
      • 2015-03-30
      • 1970-01-01
      相关资源
      最近更新 更多