Skip to content

Commit 23882b7

Browse files
authored
Add a sample for transforming Unicode code point to Unicode char by Alt+x (#3652)
1 parent 59fbc46 commit 23882b7

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

PSReadLine/SamplePSReadLineProfile.ps1

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ Set-PSReadLineKeyHandler -Key RightArrow `
603603

604604
# Cycle through arguments on current line and select the text. This makes it easier to quickly change the argument if re-running a previously run command from the history
605605
# or if using a psreadline predictor. You can also use a digit argument to specify which argument you want to select, i.e. Alt+1, Alt+a selects the first argument
606-
# on the command line.
606+
# on the command line.
607607
Set-PSReadLineKeyHandler -Key Alt+a `
608608
-BriefDescription SelectCommandArguments `
609609
-LongDescription "Set current selection to next command argument in the command line. Use of digit argument selects argument by position" `
@@ -656,3 +656,36 @@ Set-PSReadLineKeyHandler -Key Alt+a `
656656
[Microsoft.PowerShell.PSConsoleReadLine]::SetMark($null, $null)
657657
[Microsoft.PowerShell.PSConsoleReadLine]::SelectForwardChar($null, ($nextAst.Extent.EndOffset - $nextAst.Extent.StartOffset) - $endOffsetAdjustment)
658658
}
659+
660+
# Allow you to type a Unicode code point, then pressing `Alt+x` to transform it into a Unicode char.
661+
Set-PSReadLineKeyHandler -Chord 'Alt+x' `
662+
-BriefDescription ToUnicodeChar `
663+
-LongDescription "Transform Unicode code point into a UTF-16 encoded string" `
664+
-ScriptBlock {
665+
$buffer = $null
666+
$cursor = 0
667+
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref] $buffer, [ref] $cursor)
668+
if ($cursor -lt 4) {
669+
return
670+
}
671+
672+
$number = 0
673+
$isNumber = [int]::TryParse(
674+
$buffer.Substring($cursor - 4, 4),
675+
[System.Globalization.NumberStyles]::AllowHexSpecifier,
676+
$null,
677+
[ref] $number)
678+
679+
if (-not $isNumber) {
680+
return
681+
}
682+
683+
try {
684+
$unicode = [char]::ConvertFromUtf32($number)
685+
} catch {
686+
return
687+
}
688+
689+
[Microsoft.PowerShell.PSConsoleReadLine]::Delete($cursor - 4, 4)
690+
[Microsoft.PowerShell.PSConsoleReadLine]::Insert($unicode)
691+
}

0 commit comments

Comments
 (0)