The new TabExpansion feature...

The new TabExpansion feature...

Rate This
  • Comments 13

One of the nicest new features in the latest drop of Windows PowerShell is enhanced tab-completion. We now tab-complete properties on variables and parameters on cmdlets in addition to the old filename completion. But that's not the interesting part. The cool bit is that it's done through a user-definable function. In the same way that you can redefine your prompt, you can also define custom tab completion.  Here’s how it works:

 

When you hit tab after typing some text, the function TabExpansion is called to generate the list of possible completion matches. You can find this function by doing:

 

PS (16) > ls function:*tab*

 

CommandType     Name                                      Definition

-----------     ----                                      ----------

Function        TabExpansion                              ...

 

And see the current definition:

 

PS (17) > $function:tabexpansion

 

  # This is the default function to use for tab expansion.

  :

  :

 

Or save it to a text file so you can edit it:

 

PS (18) > $function:tabexpansion > c:\temp\tabexpansion.ps1

PS (19) > notepad $$

 

(The $$ above refers to the last token in the previous command – in this case, it’s the name of the file we saved the function too.)

 

The parameter declaration for this function is

 

           param($line, $lastWord)

 

where $line is the full command line as typed, and $lastWord is the last word or token in the line. So – if the line is:

            cd c:\windows

then the last word would be “c:\windows”. If the line is

            cd “c:\program files”

the last word would be “c:\program files”.

 

These two pieces are provided because most expansions work on the last token (e.g. property name expansion) but some require the entire line (e.g. parameter expansion.)

 

The TabExpansion function should return an array of strings representing the possible matches. The host will store these strings in a circular buffer the first time you hit tab after typing some text. Subsequent tabs will step you through the list of possible matches. Typing anything other than a tab will reset the sequence.

 

If this function returns null, then the expansion process will fall through to the old built-in file-name completion code to try and generate a match. (The file name completion stuff is still implemented in the host as compiled code. It was too much work to migrate it to the script for v1.)

 

Since TabExpansion is a function, it can do anything – pop up menus, create GUIs, talk to the user, or whatever. The one thing to keep in mind is performance – you want to get the results back quickly. If you have a complex completion scenario that is too slow in script then you might need to write a helper cmdlet.

 

I’m really looking forward to seeing what the community can do with this feature!

 

Bruce Payette

Windows PowerShell Technical Lead

 

PSMDTAG:INTERNAL: Tab Expansion, Tab Completion

PSMDTAG:SHELL: Tab Expansion, Tab Completion
PSMDTAG:FAQ: Why doesn't tab completion do xxxx?

 

Leave a Comment
  • Please add 6 and 4 and type the answer here:
  • Post
  • One thing I would like to be the default way to do tab completion would be that instead of the cmd functionality of displaying the results in a cirular buffer one at a time would be to list all of the possibles in a list like in the bash shell. Even though I'm more of a Windows user than *nix user the bash way for tab completion is more productive for me personally to work with.

    Kjartan
  • It should be possible to do this in this environment. The tab completion function could display a list, then return only the item selected.  The circular buffer is only used if more than one item is returned.
    It's not the default behaviour, but at least it's possible...

    Bruce Payette
    Windows PowerShell Technical Lead
  • Bruce,

    The current release does not seem to support not using the circular buffer.  Or rather, if you return a single item, pressing tab again does not result in your function getting called again.  Will this be supported in the future?

    aaron
  • >I’m really looking forward to seeing what the >community can do with this feature!

    we are getting started ;-):

    http://mow001.blogspot.com/2006/05/powershell-tab-completion.html

    http://mow001.blogspot.com/2006/06/powershell-tab-completion-part-2.html

    http://monadblog.blogspot.com/2006/06/extending-tabexpansion-function.html

    http://powershell.pastebin.com/758931

    Greetings /\/\o\/\/
  • I'm an intellisense-addict. I hate doing work that my computer can do for me, andtyping long namespace

  • I'm an intellisense-addict. I hate doing work that my computer can do for me, andtyping long namespace

  • I assume that by now everyone is using PowerShell as their default shell, if not, time to get rid of

  • Test-Path: We Goofed! (also known as a documentation errata…) Test-Path is a very handy little cmdlet.

  • Test-Path: We Goofed! (also known as a documentation errata…) Test-Path is a very handy little cmdlet.

  • Test-Path: We Goofed! (also known as a documentation errata…) Test-Path is a very handy little cmdlet.

  • Test-Path: We Goofed! (also known as a documentation errata…) Test-Path is a very handy little cmdlet.

  • Here's a great hint from one of our Windows PowerShell PMs. We all know that you can interrupt a command

  • Windows PowerShell (fka Monad) RC1 Available

Page 1 of 1 (13 items)