Skip to content

Instantly share code, notes, and snippets.

@atsushifx
Last active June 9, 2022 07:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atsushifx/a9ae10e2d0cbab8df899aa9b410aa2ce to your computer and use it in GitHub Desktop.
Save atsushifx/a9ae10e2d0cbab8df899aa9b410aa2ce to your computer and use it in GitHub Desktop.
<#
.SYNOPSIS
select command from history and execute
.DESCRIPTION
select history wuth peco and set command line to execute this.
.EXAMPLE
Set-PSReadLineKeyHandler -chord Ctrl+p -scriptBlock { SelectandExecHistory }
using above that hit ctrl+p to use this.
#>
function global:SelectandExecHistory()
{
$selectCmd = (tail -20 (Get-PSReadLineOption).HistorySavePath)|peco --select-1 --on-cancel error
if ($?) {
[Microsoft.PowerShell.PSConsoleReadLine]::InvokePrompt()
[Microsoft.PowerShell.PSConsoleReadLine]::Insert($selectCmd)
# [Microsoft.PowerShell.PSConsoleReadLine]::EndOfLine()
# [Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine()
} else {
[Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment