Skip to content

Commit

Permalink
if the current input is blank, start completing with file system entries
Browse files Browse the repository at this point in the history
  • Loading branch information
avocadianmage committed Sep 9, 2018
1 parent 5ff2841 commit f66d6da
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions powershell/startup-scripts/key-handlers.ps1
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# 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" `
-ScriptBlock {
[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" `
Expand All @@ -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" `
Expand All @@ -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()
}
}

0 comments on commit f66d6da

Please # to comment.