views:

281

answers:

4

The default TabExpansion in Powershell cycles through the possible completions for the fragment on the command prompt. Internally in the PowerShell host, there's a circular buffer, and the first TAB fills the buffer and puts the first potential completion on the prompt. Subsequent TABs cycle through the list of possible completions.

How can I modify TabExpansion to just display the set of possible completions when I hit ?

Also, can I invoke the TabExpansion function explicitly from the command prompt, and if so, how?

(If you don't know what I mean by "modify TabExpansion", see this link.)

+4  A: 

Check out PowerTab. It's pretty cool.

aphoria
I can't imagine Powershell without PowerTab anymore.
sgwill
Yes, I love PowerTab.
JasonMArcher
A: 

You can modify it directly at the prompt --or in your profile script-- it's just a function, so you could type this at a prompt to alter it to print all answers:

Copy Function:\TabExpansion Function:\OriginalTabExpansion
function TabExpansion([string] $line, [string] $lastword) { 
   OriginalTabExpansion $line $lastword | Out-Host
   $line
}

Or you can go a little further and format it wide:

Copy Function:\TabExpansion Function:\OriginalTabExpansion
function TabExpansion([string] $line, [string] $lastword) { 
   Write-Host # an emtpy newline to avoid outputting on the prompt line
   # A hack, because Format-Wide doesn't work on strings 
   $obj = new-object psobject | add-member noteproperty value "" -Passthru
   OriginalTabExpansion $line $lastword | ForEach { $obj.value = $_; $obj } | 
      Format-Wide -auto value | Out-Host
   ## Maybe even re-output your prompt function... depending on how it's written
   Write-Host $(prompt) -NoNewLine
   return $line # keep the command as it was
}

PS: Vote for the bug on Format-Wide

Jaykul
seriously? is it THAT easy? I tried this just now and it did not work for me. I got the blank line, but none of the potential completions were displayed.
Cheeso
I only tested that second solution in PowerShell 2 CTP3, but it *should* work
Jaykul
A: 

This one-liner seems to work around the problem with format-wide and an array of strings:

$ComputerList | Select-Object -Property @{Expression={[Object[]] $_};Name="Name"} | Format-Wide -Property Name -Column 5

The array of strings in this case is $ComputerList defined like this:

[array] $ComputerList = @("WINSRV2003-32B","WINSRV2003-64B")