Emulating Bash / GNU Readline with PowerShell 3.0

Introducing PSReadline 1.0 (Beta)

This is a module that takes advantage of a new hook added to PowerShell 3.0 that allows you to completely take over the readline API. To do so, you must define a function like:

function PSConsoleHostReadline {

The is an example of a very simple implementation. You may think it's enough, until you realise that there is absolutely zero line editing: The cursor keys don't work, no home/end, no tab completion, nothing! There's a lot more to do than just grabbing a line of text.

This module tries to emulate the Unix Bash/GNU Readline experience. Tab completion works by dumping out a long line of space-separated matches, and will only complete the current line up to the maximum amount of shared leading letters for all matches based on the current token.

Thankfully, you will also get Bash style tab completion for types, cmdlets, parameters and their values as the PSReadline module uses PowerShell 3.0's powerful and fast tab completion APIs.

This is a beta release, so expect a glitch or two. Currently all of the bindings are based on EMACS. The next release will let you define your own bindings. The EMACS bindings are documented below.

Have fun!

- Download ZIP file, unblock with unblock-file cmdlet.
- Extract to ~\documents\windowspowershell\modules\
  -- This should result in a PSReadline folder 

PS> Import-Module PSReadline

Known issues:

- <esc> does not clear the current line
- does not use powershell history (so get-history returns nothing)
- doesn't support fancy prompt functions with newlines and/or those
  that use write-host; single line prompt function only

- Miguel de Icaza (getline.cs)
  Thanks to his unending masochism & for donating 
  the guts of the Readline emulation, which was torn kicking &
  screaming from an old Mono REPL C# shell. Much massaging and 
  poking was needed for NT & PowerShell, but it works.

Common Bindings
Home          Cursor Home
LeftArrow     Cursor Left
RightArrow    Cursor Right
UpArrow       History - Previous
DownArrow     History - Next
Enter         Done
Backspace     Backspace
Delete        Delete Character
Tab           Tab / Tab Complete

EMACS Bindings
Ctrl+A        Home
Ctrl+E        End
Ctrl+B        Left
Ctrl+F        Right
Ctrl+P        History - Previous
Ctrl+N        History - Next
Ctrl+K        Kill to EOL
Ctrl+Y        Yank
Ctrl+D        Delete Character
Ctrl+L        Refresh
Ctrl+R        Reverse Search History

Alt+B         Word - Backwards
Alt+F         Word - Forwards
Alt+D         Word - Delete
Alt+BkSpc     Word - Delete Backwards

Ctrl+Q        Quote
blog comments powered by Disqus

About the author

Irish, PowerShell MVP, .NET/ASP.NET/SharePoint Developer, Budding Architect. Developer. Montrealer. Opinionated. Montreal, Quebec.

Month List

Page List