Skip to content

Instantly share code, notes, and snippets.

@bielawb
Created September 7, 2022 09:54
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 bielawb/2d93a56aac1631e29e1636f0a98df5c5 to your computer and use it in GitHub Desktop.
Save bielawb/2d93a56aac1631e29e1636f0a98df5c5 to your computer and use it in GitHub Desktop.
Fix $__ in PS7 with PSReadLine/ replacing it with Get-History call.
Set-PSReadlineKeyHandler -Chord Spacebar -ScriptBlock {
param ($key, $arg)
$line = $null
$cursor = $null
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState(
[ref]$line, [ref]$cursor
)
if ($cursor -lt 3) {
return
}
$replacement = '(Get-History)[-1].Output'
if ($line.Substring(($cursor - 3),3) -eq '$__') {
[Microsoft.PowerShell.PSConsoleReadLine]::Replace(
($cursor - 3),
3,
$replacement
)
[Microsoft.PowerShell.PSConsoleReadLine]::SetCursorPosition(($cursor - 3 + $replacement.Length))
} else {
[Microsoft.PowerShell.PSConsoleReadLine]::Insert(' ')
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment