Sunday, February 26, 2017

Customizing tab completion in PowerShell

Pressing the Tab key asks PowerShell to autocomplete the command part you're typing. Interestingly, you can override the default function to implement your own tab completion. In PowerShell 5, that function is TabExpansion2; before, it was just TabExpansion.

The easiest way to customize it is to get its original source code and then insert your own logic, delegating to the original code when necessary. You can extract a function's PowerShell code like so:

(Get-Command 'TabExpansion2').ScriptBlock

Once you have your custom version, you probably want to put it in one of your profile scripts so it's automatically loaded and usable.

For example, my Super User answer removes the .exe extension from tab completion suggestions when it's not needed.

No comments:

Post a Comment