gpt4 book ai didi

powershell - PowerShell 'less'工具

转载 作者:行者123 更新时间:2023-12-03 00:58:59 29 4
gpt4 key购买 nike

这不是与此相关的其他问题的重复(我查看了它们,但在我看到的问题中没有得到回答)。这些其他问题围绕Out-Host -Pagingmore(即使它们在问题标题中提到了less)也是如此。

为了关注这一点,除了Microsoft Windows环境以外,没有人知道复制less功能的PowerShell方法吗?即使我们能够在文档中上下滚动(使用光标键逐行滚动或使用PgUp / PgDn键逐页滚动)以查看帮助和其他文件(例如,以便我们可以执行Get-Help Get-ChildItem -Full | less)。

这将非常有用。我不喜欢Windows的第三方可执行less工具(因为将不会启用管道等)(当然有很多)。我相信PSCX中有这样的东西,但是每当我尝试安装它时,我都会看到很多冲突,并且不确定使用-AllowClobber以防它破坏其他东西。但是,也许在那上面,如果其中有一个less,是否有人能够拆分该功能并独立于PSCX使用?

最佳答案

使用给定平台的Get-Help Get-ChildItem -Full | less实用程序(通常为less),/usr/bin/less在类似Unix的平台上可以正常工作-无需额外的工作。

I am not after a 3rd party executable less tool (because that won't be pipeline enabled etc)


根据定义,从stdin(标准输入)读取并输出到stdout(标准输出)的任何外部程序(实用程序)均已启用管道,尽管始终仅针对文本:发送到此类实用程序的数据将转换为文本,并且从此类实用程序返回的数据将解释为文本。
在Windows上,默认情况下仅-功能限制-more.com传呼机可用-参见下文。
但是, 可以在Windows 上安装 less:
  • 如果您已经安装了WSL的Linux发行版,则可以通过管道直接传递到wsl less;例如。:
  • Get-Help Get-ChildItem | wsl less
  • 警告:从PowerShell调用时,PageUp / PageDown似乎不起作用,但是f(前进)和b(后退)提供了相同的实用程序。

  • 否则,请考虑安装 less.exe Windows console application项目的一部分GnuWin(选择最新文件夹中的版本)(安装程序需要管理员权限)。
  • 这将为您提供常规的PageUp / PageDown支持。


  • 注意:Windows还有另一个 less端口,它与其他实用程序 bundle 在一起。尚未亲自尝试过: UnxUtils
    警告:
  • less显然期望UTF-8输入,而不考虑 Activity 的OEM代码页面,并且如果[console]::OutputEncoding]设置为UTF-8,则只能正确显示非ASCII字符。
  • 因此,必须将$OutputEncoding[console]::OutputEncoding]都设置为UTF-8([Text.Utf8Encoding]::new()),才能正常显示非ASCII字符。 (在PowerShell [Core] v6 +中,$OutputEncoding默认为UTF-8,但[console]::OutputEncoding]仍反射(reflect)系统的OEM代码页。)
  • 有关如何使PowerShell中的命令more / help函数使用less而不是more.com的信息,请参见底部,这是通过自定义more函数实现的,该函数还确保使用UTF-8编码

  • GnuWin less.exe版本394 (在撰写本文时为最新版本,但于2006年1月3日发布)有时表现为异​​常,但不显示任何内容;开始新的 session 会使问题消失。

  • -功能较弱(没有双关语)- Windows上与less对应的是more(more.com),它通过stdin /管道或文件名参数接受文本。
    值得注意的是, more.com似乎仅支持分页向下,且带有空格,但不备份;也就是说,您无法向后滚动-参见 here
    PowerShell自己的 Out-Host -Paging具有相同的限制
  • Windows PowerShell提供了一个围绕more.com的内置包装函数,也称为more(这意味着仅执行more即可执行该函数),从而确保使用 Activity OEM代码页的编码来输出指定文件的内容。 more.com期望。
  • PowerShell [Core] 6+已​​不再提供此包装。

  • 在这两个版本中,内置的 help函数本身都会包装 Get-Help,将后者的输出隐式地传递给 more-在Windows PowerShell中始终不变,默认情况下在Windows的PowerShell 6+中(在Unix上,默认为 less)。
    在PowerShell 6+中,您还可以通过将 $env:PAGER变量设置为要为分页 help输出进行调用的命令行来定义自定义寻呼机。
    在Windows PowerShell中,唯一的选择是替换/定义自定义 more函数(在PowerShell 6+中也将起作用)。
    换句话说:如下所示,默认情况下会为您提供交互式页面输出:
    help Get-ChildItem  # Effectively the same as: Get-Help Get-ChildItem | more

    如果您在Windows上有 less并想要使用它代替 more:
    覆盖内置的/定义 more函数,如下所示(在 $PROFILE文件中):
  • 通过WSL使用less:
  • # Via WSL
    function more {
    $prevOe, $prevCoe = $OutputEncoding, [console]::OutputEncoding
    try {
    $OutputEncoding = [console]::OutputEncoding = [Text.Utf8Encoding]::new()
    $Input | wsl less
    }
    finally {
    $OutputEncoding, [console]::OutputEncoding = $prevOe, $prevCoe
    }
    }

    # If running PowerShell Core (v6+):
    # Force the `help` function to use the custom function.
    if ($IsCoreClr) { $env:PAGER = 'more' }
  • 使用GnuWin的less.exe:
  • # Via GnuWin (assuming the default installation location)
    function more {
    $prevOe, $prevCoe = $OutputEncoding, [console]::OutputEncoding
    try {
    $OutputEncoding = [console]::OutputEncoding = [Text.Utf8Encoding]::new()
    $Input | & 'C:\Program Files (x86)\GnuWin32\bin\less.exe'
    }
    finally {
    $OutputEncoding, [console]::OutputEncoding = $prevOe, $prevCoe
    }
    }

    # If running PowerShell Core (v6+):
    # Force the `help` function to use the custom function.
    if ($IsCoreClr) { $env:PAGER = 'more' }
    注意:这使得 more仅接受管道输入,但是扩展函数以接受文件名参数也并不困难。

    如果满足以下条件,则由 David Hatch建议使用 更简单的解决方案:
  • 您已经安装了GnuWin less.exe
  • 您不需要支持非ASCII字符。
  • 可以,但是您的 session 已经配置为将$OutputEncoding[console]::OutputEncoding都设置为UTF-8。 (在PowerShell [Core] v6 +中,$OutputEncoding默认为UTF-8,但不是[console]::OutputEncoding])。
  • 有关如何通过$PROFILE将PowerShell session 切换为一致使用UTF-8的信息,请参见this answer
  • 有关如何在系统范围内配置Windows 10以使用代码页65001 == UTF-8,请参见this answer,但请注意,在撰写本文时,该功能仍处于beta版本,并且具有副作用和局限性。值得注意的是,它使Windows PowerShell命令使用 Activity 的ANSI代码页(Get-Content / Set-Content),然后默认为UTF-8。


  • Windows PowerShell:
    Set-Alias more 'C:\Program Files (x86)\GnuWin32\bin\less.exe'
    PowerShell [Core] v6 +:
    $env:PAGER = 'C:\Program Files (x86)\GnuWin32\bin\less.exe'

    关于powershell - PowerShell 'less'工具,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59240742/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com