LoginSignup
1
0

More than 3 years have passed since last update.

Powershellでpecoをショートカットキーで呼び出す

Last updated at Posted at 2021-02-13

ここに書くまでもない程簡単にできたけど、意外とドンピシャの記事は見つからなかったので書き記しておく。
PowerShell 7.1.2で確認。

  • Ctrl+Tで$HOME\.snippetsに配置したワンライナー集をpeco
  • Ctrl+Shift+Tでプロンプト上の1行を.snippetsに追加
  • Ctrl+Rでコマンド履歴をpeco
Microsoft.PowerShell_profile.ps1
Set-PSReadLineKeyHandler -Key Ctrl+t -BriefDescription "snipetts" -LongDescription "peco .snippets" -ScriptBlock {
    $snipet = Get-Content $HOME\.snippets | peco

    [Microsoft.PowerShell.PSConsoleReadLine]::ClearScreen()
    [Microsoft.PowerShell.PSConsoleReadLine]::RevertLine()
    if($snipet -eq $null){return}
    [Microsoft.PowerShell.PSConsoleReadLine]::Insert($snipet)
}

Set-PSReadLineKeyHandler -Key Ctrl+Shift+t -BriefDescription SaveInHistory -LongDescription "Add line to .snippets" -ScriptBlock {
    param($key, $arg)

    $line = $null
    $cursor = $null
    [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)
    if($cursor -eq 0){return}
    Add-Content $HOME\.snippets $line
    [Microsoft.PowerShell.PSConsoleReadLine]::RevertLine()
}
Set-PSReadLineKeyHandler -Key Ctrl+r -BriefDescription History -LongDescription 'peco history' -ScriptBlock {
    $hist = @(Get-Content $(Get-PSReadLineOption).HistorySavePath)
    $command = ($hist)[$hist.length..0] | peco

    [Microsoft.PowerShell.PSConsoleReadLine]::ClearScreen()
    [Microsoft.PowerShell.PSConsoleReadLine]::RevertLine()
    if($command -eq $null){return}
    [Microsoft.PowerShell.PSConsoleReadLine]::Insert($command)
}

日本語が混じると微妙な感じになるけど、使い道としてそんなに混じることは無いので妥協した。

1
0
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
1
0