diff --git a/powershell/startup-scripts/key-handlers.ps1 b/powershell/startup-scripts/key-handlers.ps1 index d5b8e63..534ab56 100644 --- a/powershell/startup-scripts/key-handlers.ps1 +++ b/powershell/startup-scripts/key-handlers.ps1 @@ -1,4 +1,4 @@ -# Ctrl+Home: bring cursor to start of input buffer. +# Ctrl+Home: Bring cursor to start of input buffer. Set-PSReadLineKeyHandler -Key Ctrl+Home ` -BriefDescription BeginningOfInput ` -Description "Bring cursor to start of input" ` @@ -6,7 +6,7 @@ Set-PSReadLineKeyHandler -Key Ctrl+Home ` [Microsoft.PowerShell.PSConsoleReadLine]::SetCursorPosition(0) } -# Ctrl+End: bring cursor to end of input buffer. +# Ctrl+End: Bring cursor to end of input buffer. Set-PSReadLineKeyHandler -Key Ctrl+End ` -BriefDescription EndOfInput ` -Description "Bring cursor to end of input" ` @@ -17,13 +17,13 @@ Set-PSReadLineKeyHandler -Key Ctrl+End ` [Microsoft.PowerShell.PSConsoleReadLine]::SetCursorPosition($text.Length) } -# Ctrl+Shift+Home: select text until start of input. +# Ctrl+Shift+Home: Select text until start of input. Set-PSReadLineKeyHandler -Key Ctrl+Shift+Home ` -BriefDescription SelectBackwardToStart ` -Description "Select text until start of input" ` -ScriptBlock { selectLines $false } -# Ctrl+Shift+End: select text to the end of the input buffer. +# Ctrl+Shift+End: Select text to the end of the input buffer. Set-PSReadLineKeyHandler -Key Ctrl+Shift+End ` -BriefDescription SelectForwardToEnd ` -Description "Select text until end of input" ` @@ -48,3 +48,30 @@ function selectLines { $selectionLength = $newLength } } + +# Tab/Shift+Tab: Complete the input using the next completion. Use the first +# completion in the case of no input. +Set-PSReadLineKeyHandler -Key Tab,Shift+Tab ` + -BriefDescription TabCompleteFirstOrNext ` + -Description "Complete the input using the next completion. Use the first completion in the case of no input" ` + -ScriptBlock { + param($key, $arg) + + # If the current input is blank, use ".\" to start completing with file + # system entries. + $text = $null + [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState( + [ref]$text, [ref]$null) + if ([string]::IsNullOrWhitespace($text)) { + [Microsoft.PowerShell.PSConsoleReadLine]::RevertLine() + [Microsoft.PowerShell.PSConsoleReadLine]::Insert(".\") + } + + # Call into the standard tab completion APIs. Cycle next/previous based on + # if shift is pressed. + if (($key.Modifiers -band [System.ConsoleModifiers]::Shift) -ne 0) { + [Microsoft.PowerShell.PSConsoleReadLine]::TabCompletePrevious() + } else { + [Microsoft.PowerShell.PSConsoleReadLine]::TabCompleteNext() + } +}