Bash-like Tab Completion in PowerShell

When PowerShell first came on the scene, Unix folks were among the first to notice. They’d enjoyed a powerful shell and a vigorous heritage of automation for years – and “when I’m forced to use Windows, PowerShell rocks” was a phrase we heard many times.

This natural uptake was no mistake. There are many on the team that come from a deep Unix background, and similarities to traditional Unix shells were no mistake.

When coming from a Unix background, through, we still hear the occasional grumble that tab completion feels weird. Ctrl-R doesn’t invoke history search? Tab cycles through matches, rather than list them? Abhorrent!

In PowerShell V1 and V2, there was nothing you could reasonably do to address this. PowerShell reads its input from the console in what is known as “Cooked Mode” – where the Windows Console subsystem handles all the keypresses, fancy F7 menus, and more. When you press ENTER or TAB, PowerShell got the text of what you had typed so far, but that’s it. There was no way to know that you had pressed Ctrl-R, Ctrl-A, Ctrl-E, etc.

We resolved this issue in PowerShell version three. Windows does, of course, offer a “Raw Mode” for user input. While we wouldn’t want to replace PowerShell’s default ReadLine() implementation with that of Bash, some enterprising soul might want to. To support this, we introduced the PSConsoleHostReadLine function in PowerShell version three. If this function exists, we call that instead of the Cooked Mode ReadLine(). And that’s it – the rest is up to you.

It turns out that some enterprising soul does exist, and he’s in fact one of our MVPs: Oisin Grehan. By building on the ReadLine() implementation in Mono, he’s developed a module that offers Bash-style line editing in PowerShell.

Have at ‘er! http://nivot.org/nivot2/post/2012/09/12/Emulating-Bash-GNU-Readline-with-PowerShell-30.aspx

8 Responses to “Bash-like Tab Completion in PowerShell”

  1. Smith Cator writes:

    This is very fantastic.

    But, why can MVPs write modules with documents that weren’t documented in help? (http://technet.microsoft.com/en-us/library/bb978526.aspx)
    I can’t find documents about PSConsoleHostReadLine except Oisin’s blog (http://www.google.co.jp/search?sugexp=chrome,mod=3&sourceid=chrome&ie=UTF-8&q=PSConsoleHostReadLine)

    PowerShell team favors too much :-(

  2. Lee Holmes writes:

    Sorry, Smith – we’re still busy writing the help documentation. Blogs like this (and similar ones like this on the PowerShell blog) are a way to try to get the idea out there before the official documentation is live.

    If you feel that there are other things that only MVPs have access to, please let us know – filing a bug on Connect that says “____ needs documentation” is the best way.

  3. Smith Cator writes:

    What I want to know is understandably all modification from V2 through V3.
    Unfortunately, this wish was already rejected…
    https://connect.microsoft.com/PowerShell/feedback/details/747688/psv3-add-all-the-changed-parts-from-ps2-0-to-the-help

    Despite the fact that we don’t know PSConsoleHostReadLine, how can we file it like“____ needs documentation”?
    We can file mistake of documented help, but we can’t know undocumented help.
    People, who knows what there is and what there is not, are just only you.
    What documents were opened for MVP? What documents were opened for everyone?
    I can’t know.

  4. Lee Holmes writes:

    Smith – Our goal is that nothing is private or secret, it just takes us a bit to get it all documented.

  5. Smith Cator writes:

    Yeah, I hope so :-)
    Thanks.

  6. Oisin Grehan writes:

    As an MVP, sometimes I might hear about things a little ahead of the curve but more often than not – and as Lee will tell you – I’m a bit of a masochistic type and spend a not unreasonable amount of time spelunking in Reflector to find hooks like these. Other times, I like to use another module I wrote called Poke ( bitbucket.org/oising/poke ) that lets you do interactive exploration from powershell of managed runtime APIs, including introspection of PowerShell itself.

  7. manojlds writes:

    Second what Smith has been saying. Many small things and sometimes big things are missed out and we learn it from some MVP. It would be great if MVP communications are made public then.

  8. Jon writes:

    Looks like the original link above is broken. Try http://nivot.org/blog/post/2012/09/12/Emulating-Bash-GNU-Readline-with-PowerShell-30.

Leave a Reply