PowerShell CLI syntax coloring, auto-completion and much more: aka PSReadline


You can’t miss it, PowerShell version 5 CLI comes with the nice color syntax highlighting.
But that’s just the tip of the iceberg.  I like to call it one of the “best kept secrets” about PowerShell v5.
That is all thanks to Jason Shrik (aka lzybkr), member of the PowerShell Product Group and the creator of the PS module ‘PSReadline’.

Once a GitHub project, now by default included in PowerShell version 5.

And also available in the PowerShell Gallery, thus can be installed using PowerShellGet onto any machine running PowerShell version 3 and 4 (see below).

image

Let’s dive into some of the additional capabilities of the PSReadline module.

This PowerShell module replaces the command line editing experience in PowerShell and provides:

  • Syntax coloring
  • Simple syntax error notification
  • A good multi-line experience (both editing and history)
  • Customizable key bindings
  • Cmd and Emacs modes (neither are fully implemented yet, but both are usable)
  • Many configuration options
  • Bash style completion (optional in Cmd mode, default in Emacs mode)
  • Bash/zsh style interactive history search (CTRL-R)
  • Emacs yank/kill ring
  • PowerShell token based “word” movement and kill
  • Undo/redo
  • Automatic saving of history, including sharing history across live sessions
  • “Menu” completion (somewhat like Intellisense, select completion with arrows) via Ctrl+Space – yeah baby

    Keith Hill also wrote a great introduction to PSReadline here

    Below, I cover Installation, Utilization and Customization of the PSReadline module and it’s functionality.

    Installation

    Check your local version of the PSReadline module, using

  •    Get-Module –Name PSReadline –ListAvailable

    Check the online version of the PSReadline module in the PowerShell Gallery, using

       Find-Module –Name PSReadline

    Install the PSReadline module from the PowerShell Gallery, using

       Install-Module –Name PSReadline –Force

     

    Utilization

    Let’s just run through some of my preferred features.

    Syntax coloring

    image

    Simple syntax error notification

    image

    A good multi-line experience (both editing and history)

    image

    Customizable key bindings

    Key bindings is all about binding a function/operation to a key or key combination.

    To view the current key bindings:

    Get-PSReadlineKeyHandler

    image

    Bash style interactive history search using Ctrl+R

    image

    image

    PowerShell token based “word” movement and kill

    Move the cursor back to the start of the current word, or if between words, the start of the previous word, using Ctrl-left/right arrow.

    Undo/redo using Ctrl+Z

    Reverts all of the input since the last input was accepted and executed.
    This is equivalent to doing Undo until there is nothing left to undo.

    Automatic saving of history, including sharing history across live sessions

    Classic function keys F7 and F8 (HistorySearchBackward), but also new function keys like Shift-F8 (HistorySearchForward ).

    “Menu” like completion using Ctrl+Space

    Somewhat like Intellisense, but allows to select completion using arrows.

    image
    Full completion, by typing “Get-Command -CommandType <Ctrl-Space>”

    image
    Word specific completion, by typing “Get-Service -DisplayName V <Ctrl-Space>”

     

    Customization

    PSReadline supports custom key bindings using the cmdlet Set-PSReadlineKeyHandler
    Most custom key bindings will call one of the above functions, for example:

      Set-PSReadlineKeyHandler -Key UpArrow -Function HistorySearchBackward

    You can bind a ScriptBlock to a key.  The ScriptBlock can do pretty much anything you want.
    Some useful examples include:

  • edit the command line
  • opening a new window (e.g. help)
  • change directories without changing the command line

    To set your own custom keybindings, use the cmdlet Set-PSReadlineKeyHandler.

    Example #1, for a better history experience, try:

      Set-PSReadlineKeyHandler -Key UpArrow   -Function HistorySearchBackward
      Set-PSReadlineKeyHandler -Key DownArrow -Function HistorySearchForward

    With these bindings, up arrow/down arrow will work like PowerShell/cmd if the current command line is blank.
    If you have entered some text though, it will search the history for commands that start with the currently entered text.

    image
    A better history experience.

    Example #2, for jumping between ‘braces’ (curly brace, square brackets), try:

      Set-PSReadlineKeyHandler -Function GotoBrace -Key Ctrl+B

    A summary of some existing key handlers

    Function Key Description
    SelectAll Ctrl+A Select the entire line. Moves the cursor to the end of the line.
    Redo Ctrl+Y Redo an insertion or deletion that was undone by Undo.
    Undo Ctrl+Z Undo a previous insertion or deletion.
    PreviousHistory <UpArrow> Replace the current input with the ‘previous’ item from PSReadline history.
    NextHistory <DownArrow> Replace the current input with the ‘next’ item from PSReadline history.
    NextWord <Ctrl+RightArrow> Move the cursor forward to the start of the next word.
    BackwardWord <Ctrl+LeftArrow> Move the cursor back to the start of the current word, or if between words, the start of the previous word.
    ForwardSearchHistory <Ctrl+S> Search forward from the current history line interactively.
    MenuComplete <Ctrl+Space>

    Attempt to perform completion on the text surrounding the cursor. If there are multiple possible completions, a list of possible completions is displayed and you can select the correct completion using the arrow keys or Tab/Shift+Tab.

    CopyOrCancelLine <Ctrl+C> Either copy selected text to the clipboard, or if no text is selected, cancel editing the line with CancelLine.
    Cut <Ctrl+X> Delete selected region placing deleted text in the system clipboard.
    Paste <Ctrl+V> Paste from the system clipboard.
    ClearScreen <Ctrl+L> Clears the screen and displays the current prompt and input at the top of the screen.
    ScrollDisplayUp  <PgUp> Scroll the display up one screen.
    ScrollDisplayUpLine <Ctrl+PageUp> Scroll the display up one line.
    ScrollDisplayDown <PageDown> Scroll the display down one screen.
    ScrollDisplayDownLine <Ctrl+PageDown> Scroll the display down one line.

    More shortcuts on Hey, Scripting Guy! blog.

    Help

    PSReadline has help for it’s cmdlets as well as an about_PSReadline topic – see those topics for more detailed help.

      Get-Help about_PSReadline

     

    More information and/or feedback on GitHub here.

    Related reading:

  • PowerShellGet – Package Management for PowerShell modules – Part 1
  • PowerShellGet – Package Management for PowerShell modules – Part 2
  • Scripting Guy Blog – A Better PowerShell Console with Custom PSReadLine Functions
  • Useful Shortcuts from PSReadLine PowerShell Module
  • One thought on “PowerShell CLI syntax coloring, auto-completion and much more: aka PSReadline

    Leave a comment