If you are like me, having heavy background in Windows and Linux prior to Powershell coming about. You have probably worn out your backspace key as much as I have after pressing the TAB key and having it autocomplete to the first FULL match.
Unlike the bash shell, which autocompletes up to the next unique character, allowing you to press a single (or a few) keys and continue your autocomplete adventure.
I recently started in a position where I am doing a great deal of tool making, Don said that is where we should live, and usage of autocomplete was important for me.
Most of us who are building Powershell modules are following a similar naming convention as a great deal of the major tool builders are.
Verb-{myModuleId}Noun
My example would be a module named “ThunderCat”, might have a myModuleId of Thndr, and commands such as:
Get-ThndrHost
Get-ThndrHostStatus
Get-ThndrLockedServers
Remove-ThndrTempDirectoryFiles
Remove-ThndrLockFile
The names don’t really matter too much here, you get the idea.
So, when I type Remove-Thndr{tab} I’m going to get Remove-ThndrLockFile, and if I really wanted the Remove-ThndrTempDirectoryFiles, and there was 20 other Remove-ThndrL* named functions, I either have to backspace, or tab a bunch more times. My solution is fairly simple. Create functions with names where you want the autocomplete to stop at.
[PRE]
Function Remove-Thndr
{
[CmdletBinding()]
Param
(
)
Begin { }
Process{ }
End
{
Get-Command -Module ThunderCat | Where-Object { $_.Name.StartsWith("Remove-Thndr") }
}
}
[/PRE]
Now if you type Remove-Thn{tab} the interface stops at Remove-Thndr and allows you to type the “T” and press tab again to autocomplete to the function that you want.
It isn’t perfect and takes a little extra planning if you want to create multiple stop points for sections of similarly named functions where the names get quite long. It saves me some frustration, so I figured I would share.
Of course, if you accidentally press enter at one of these autocomplete stubs, it is nice for it to spit out the commands that match from the point you are currently at.
Hope you are all enjoying life with Powershell…
~cj