Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Jaykul
Last active March 27, 2022 00:40
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jaykul/761c3b962e1f28998867e120c49c67ae to your computer and use it in GitHub Desktop.
Save Jaykul/761c3b962e1f28998867e120c49c67ae to your computer and use it in GitHub Desktop.
Indent and Outdent hotkeys for PSReadline

Here's a couple of key handlers to add indenting and outdenting with Alt [ and ] for PSReadLine.

# Hack in a [ReadLine] accelerator
$xlr8r = [psobject].assembly.gettype("System.Management.Automation.TypeAccelerators")
if ($xlr8r::AddReplace) {
$xlr8r::AddReplace("ReadLine", [Microsoft.PowerShell.PSConsoleReadLine])
} else {
$null = $xlr8r::Remove("ReadLine")
$xlr8r::Add("ReadLine", [Microsoft.PowerShell.PSConsoleReadLine])
}
# Now some key handlers
Set-PSReadLineKeyHandler "Alt+]" {
param($key, $arg)
$start = $null
$length = $null
[ReadLine]::GetSelectionState([ref]$start, [ref]$length)
$command = $null
$cursor = $null
[ReadLine]::GetBufferState([ref]$command, [ref]$cursor)
# if there's no selection, these should be set to the cursor
if ($start -lt 0) { $start = $cursor; $length = 0 }
$end = $start + $length
# Write-Host "`e[s`e[0;0H`e[32mStart:$start End:$end Length:$Length `e[u"
# pretend that entire lines are selected
if ($start -gt 0) {
$start = $command.SubString(0, $start).LastIndexOf("`n") + 1
}
$end = $end + $command.SubString($end).IndexOf("`n")
$length = $end - $start
# Write-Host "`e[s`e[2;0H`e[34mStart:$start End:$end Length:$Length `e[u"
$lines = $command.SubString($start, $length)
$count = ($lines -split "`n").Count
# Write-Host "`e[s`e[3;0H`e[36m$lines`e[u"
# Write-Host "`e[s`e[2;0H`e[34mStart:$start End:$end Length:$Length Lines:$Count`e[u"
[ReadLine]::Replace($start, $length, ($lines -replace "(?m)^", " "))
[ReadLine]::SetCursorPosition($start)
[ReadLine]::SelectLine()
if ($count -gt 1) {
while (--$Count) {
[ReadLine]::SelectForwardChar()
[ReadLine]::SelectLine()
}
}
}
Set-PSReadLineKeyHandler "Alt+[" {
param($key, $arg)
$start = $null
$length = $null
[ReadLine]::GetSelectionState([ref]$start, [ref]$length)
$command = $null
$cursor = $null
[ReadLine]::GetBufferState([ref]$command, [ref]$cursor)
# if there's no selection, these should be set to the cursor
if ($start -lt 0) { $start = $cursor; $length = 0 }
$end = $start + $length
# Write-Host "`e[s`e[0;0H`e[32mStart:$start End:$end Length:$Length `e[u"
# pretend that entire lines are selected
if ($start -gt 0) {
$start = $command.SubString(0, $start).LastIndexOf("`n") + 1
}
$end = $end + $command.SubString($end).IndexOf("`n")
$length = $end - $start
# Write-Host "`e[s`e[2;0H`e[34mStart:$start End:$end Length:$Length `e[u"
$lines = $command.SubString($start, $length)
$count = ($lines -split "`n").Count
# Write-Host "`e[s`e[3;0H`e[36m$lines`e[u"
# Write-Host "`e[s`e[2;0H`e[34mStart:$start End:$end Length:$Length Lines:$Count`e[u"
[ReadLine]::Replace($start, $length, ($lines -replace "(?m)^ ", ""))
[ReadLine]::SetCursorPosition($start)
[ReadLine]::SelectLine()
if ($count -gt 1) {
while (--$Count) {
[ReadLine]::SelectForwardChar()
[ReadLine]::SelectLine()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment