How do I disable/adjust the behaviour of tab completion for specific functions in PowerShell?
You want a Private function to hide your helper functions from your general shell - The syntax is helpfully Function Private:MyFunction
or $Private:Foo
for variables. Here's an example that defines both kinds:
# Create a function you can run directly
Function Do-Foo ($Foo) {
$Private:Bar = $Foo+$Foo
Write-Output "Top level Foo is $Foo"
Write-Output "Top level Bar is $Bar"
# Create a helper function
Function Private:Do-Bar {
Write-Output "Child Foo is $Foo"
Write-Output "Child Bar is $Bar" # Can't be used since it was private
}
# Can be used by function Do-Foo, but not by you!
Do-Bar
}
If you add this to your profile or just run it normally, you'll be able to tab-complete Do-Foo
like normal, but you won't even be able to run Do-Bar
:
PS C:\> Do-Foo -Foo 1
Top level Foo is 1
Top level Bar is 2
Child Foo is 1
Child Bar is
PS C:\> Do-Bar
Do-Bar : The term 'Do-Bar' is not recognized as the name of a cmdlet, Function, ...
Success!
Other than that, the Powershell tab-complete function is simply alphabetical order by default. This can be changed with PSReadLine in newer versions of powershell:
# The default setting:
Set-PSReadLineKeyHandler -Key Tab -Function TabCompleteNext
There's a lot of different built-in options, some I see that might interest you are Complete
, MenuComplete
, PossibleCompletions
, or ViTabCompleteNext
. I'm using PSReadLine version 2.2.0.