Skip to content

Commit ba3ebf2

Browse files
authored
Add an key binding example: ForwardCharAndAcceptNextSuggestionWord (#1601)
1 parent 2005773 commit ba3ebf2

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

PSReadLine/SamplePSReadLineProfile.ps1

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,7 @@ Set-PSReadLineKeyHandler -Key Alt+j `
562562
[Microsoft.PowerShell.PSConsoleReadLine]::InvokePrompt()
563563
}
564564

565+
# Auto correct 'git cmt' to 'git commit'
565566
Set-PSReadLineOption -CommandValidationHandler {
566567
param([CommandAst]$CommandAst)
567568

@@ -579,3 +580,22 @@ Set-PSReadLineOption -CommandValidationHandler {
579580
}
580581
}
581582
}
583+
584+
# `ForwardChar` accepts the entire suggestion text when the cursor is at the end of the line.
585+
# This custom binding makes `RightArrow` behave similarly - accepting the next word instead of the entire suggestion text.
586+
Set-PSReadLineKeyHandler -Key RightArrow `
587+
-BriefDescription ForwardCharAndAcceptNextSuggestionWord `
588+
-LongDescription "Move cursor one character to the right in the current editing line and accept the next word in suggestion when it's at the end of current editing line" `
589+
-ScriptBlock {
590+
param($key, $arg)
591+
592+
$line = $null
593+
$cursor = $null
594+
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)
595+
596+
if ($cursor -lt $line.Length) {
597+
[Microsoft.PowerShell.PSConsoleReadLine]::ForwardChar($key, $arg)
598+
} else {
599+
[Microsoft.PowerShell.PSConsoleReadLine]::AcceptNextSuggestionWord($key, $arg)
600+
}
601+
}

0 commit comments

Comments
 (0)