LoginSignup
2
2

More than 5 years have passed since last update.

PowerShellのヘルプ表示をlessにする

Posted at

PowerShellのhelp

PowerShellでhelp lsとかman cdとか打つとHelpが表示されます。
この表示にはmoreコマンド(Windowsの場合はC:\WINDOWS\system32\more.com)が使われます。
しかし、moreは戻ったりできないのでより高機能なlessを使えるようにしたいと思います。

helpの表示の仕方

(Get-Command help).Definitionと打つことでhelpコマンドの定義が見られます。

help(ver5.1)
PS C:\> (Get-Command help).Definition

<#
.FORWARDHELPTARGETNAME Get-Help
.FORWARDHELPCATEGORY Cmdlet
#>
[CmdletBinding(DefaultParameterSetName='AllUsersView', HelpUri='https://go.microsoft.com/fwlink/?LinkID=113316')]
param(
    [Parameter(Position=0, ValueFromPipelineByPropertyName=$true)]
    [string]
    ${Name},

    [string]
    ${Path},

    [ValidateSet('Alias','Cmdlet','Provider','General','FAQ','Glossary','HelpFile','ScriptCommand','Function','Filter','ExternalScript','All','DefaultHelp','Workflow','DscResource','Class','Configuration')]
    [string[]]
    ${Category},

    [string[]]
    ${Component},

    [string[]]
    ${Functionality},

    [string[]]
    ${Role},

    [Parameter(ParameterSetName='DetailedView', Mandatory=$true)]
    [switch]
    ${Detailed},

    [Parameter(ParameterSetName='AllUsersView')]
    [switch]
    ${Full},

    [Parameter(ParameterSetName='Examples', Mandatory=$true)]
    [switch]
    ${Examples},

    [Parameter(ParameterSetName='Parameters', Mandatory=$true)]
    [string]
    ${Parameter},

    [Parameter(ParameterSetName='Online', Mandatory=$true)]
    [switch]
    ${Online},

    [Parameter(ParameterSetName='ShowWindow', Mandatory=$true)]
    [switch]
    ${ShowWindow})

    #Set the outputencoding to Console::OutputEncoding. More.com doesn't work well with Unicode.
    $outputEncoding=[System.Console]::OutputEncoding

    Get-Help @PSBoundParameters | more

ずらっと出てますが、Get-Helpコマンドの結果をmoreに渡していることがわかります。
moreの定義も見てみます。

more(ver5.1)
PS C:\> (Get-Command more).Definition

param([string[]]$paths)
$OutputEncoding = [System.Console]::OutputEncoding
if($paths) {
    foreach ($file in $paths)
    {
        Get-Content $file | more.com
    }
} else { $input | more.com }

ifはパイプで渡されたか引数で渡されたかの違いなので実質はhelp関数内で直接more.comをパイプに渡してるのてと同等の記述です。

PowerShell Core 6.0以降ではマルチプラットフォーム対応でもう少し記述が多いですが、おおよそ同じかと思います。

lessへの差し替え

とりあえずmoreコマンドを使うことはないと割り切ってmore関数を上書きしてしまうのが楽でしょう。
使うlessはgit bashのMinGWでもwslのものでも大丈夫です。
下記はwslを使っていますが、MinGWの場合はwsl lessの部分をless.exeのフルパスにしたら動くはずです。
lessはUTF-8を受ける動作が規定なのでUTF-8で出力するようにします。

less
function less { 
    param([string[]]$paths)
    $OutputEncoding = [System.Console]::OutputEncoding
    if ($paths) {
        foreach ($file in $paths) {
            Get-Content $file | & wsl less
        }
    }
    else { $input | & wsl less }
}
Set-Alias more less
[System.Console]::OutputEncoding = $OutputEncoding = [System.Text.UTF8Encoding]::new()

これを$PROFILEに追記します。

すると、man lsなどでもlessが使えます。
他の箇所でもlessが使えるようになります。

man ls
ls | less
less $PROFILE
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2