Announcing PSReadLine 2.1+ with Predictive IntelliSense

Jason Helmick


Tab completion has accelerated the success of new and experienced PowerShell users for over a decade. New users get the benefit of discovery, seeing available cmdlets and parameters as options while interactively typing. Experienced users receive the benefit of acceleration, typing less while using the Tab key to quickly complete a command.

As the scale and complexity of cloud deployments and IT environments grow, cmdlet surface area is continually being updated with new cmdlets and additional parameter sets. For example, If you’re beginning your journey with Azure, the Azure PowerShell module can be daunting. In this increasingly complex world, Predictive IntelliSense is here to help.

Predictive IntelliSense is an addition to the concept of tab completion that assists the user in successfully completing commands. The prediction suggestion appears as colored text following the user’s cursor. This enables new and experienced users of PowerShell to discover, edit, and execute full commands based on matching predictions from the user’s history and additional domain specific plugins.

Image pred wlc

PSReadLine 2.1.0 + History Based Prediction Release

Predictive IntelliSense is implemented in the PowerShell engine and presented through the PSReadLine module. In an effort to support the most PowerShell users, the presentation of history-based predictions is available in the following versions:

Availability:

  • PSReadLine 2.1.0 currently available for download from PSGallery
  • PSReadLine 2.1.0 will ship with PowerShell 7.1

Supported PowerShell versions:

  • Windows PowerShell 5.1
  • PowerShell 7.0+

Included in this version:

  • History based prediction in the InlineView

Installing Predictive IntelliSense

To get started with Predictive IntelliSense, download and install the PSReadLine module from PSGallery.

The current release is PSReadLine 2.1.0:

Install-Module PSReadLine -RequiredVersion 2.1.0

Enabling and Disabling Predictions

By default, Predictive IntelliSense is disabled.

To enable Predictive IntelliSense:

Set-PSReadLineOption -PredictionSource History

To disable Predictive IntelliSense:

Set-PSReadLineOption -PredictionSource None

Run these command at the command line or add them to your profile script.

Predictive IntelliSense in PSReadLine 2.1.0 supports the following arguments for prediction source:

  • None – This option disables Predictive IntelliSense
  • History – This option uses the PSReadLine history for predictions

Change the Color for Predictions

By default, predictions appear in light grey text on the same line the user is typing. To support accessibility needs, the prediction color is customizable from the command line or your profile script.

In PSReadLine 2.1.0, the Set-PSReadLineOption -Colors Hashtable includes InlinePrediction to set the color of predictive text for InlineView.

The default light-grey prediction text color can be restored using:

Set-PSReadLineOption -Colors @{ InlinePrediction = "$([char]0x1b)[38;5;238m"}

Examples of user changing prediction color:

Image pi color

Image pi color2

Image pi color3 background

Set-PSReadLineOption -Colors @{ InlinePrediction = '#8A0303'}
Set-PSReadLineOption -Colors @{ InlinePrediction = '#2F7004'}
Set-PSReadLineOption -Colors @{ InlinePrediction = "$([char]0x1b)[36;7;238m"}

Key Bindings for Predictions

Key bindings control cursor movement and additional features within the prediction. To support users running Predictive IntelliSense on multiple platforms, key bindings are user-settable from the command line or your profile script.

PSReadLine contains functions to navigate and accept predictions. As an example, to accept a displayed prediction, PSReadLine contains functions:

  • AcceptSuggestion – Accept the current inline suggestion
  • AcceptNextSuggestionWord – Accept the next word of the inline suggestion

AcceptSuggestion is built within ForwardChar, which by default is bound to RightArrow. Pressing RightArrow accepts an inline suggestion when the cursor is at the end of the current line.

AcceptNextSuggestionWord is built within the function ForwardWord, which can be bound with Ctrl+f by Set-PSReadLineKeyHandler -Chord "Ctrl+f" -Function ForwardWord. Pressing Ctrl+f accepts the next word of an inline suggestion when the cursor is at the end of current editing line.

As a user, you can bound other keys to AcceptSuggestion and AcceptNextSuggestionWord for similar functionalities. Search for ForwardCharAndAcceptNextSuggestionWord in SamplePSReadLineProfile.ps1 for an example to make RightArrow accept the next word from inline suggestion, instead of the whole suggestion line.

List of additional suggested key bindings defined in PSReadLine SamplePSReadLineProfile.ps1

PSReadLine 2.2.0-beta1 + Plugin Prediction Release Plan

Predictive IntelliSense in PSReadLine 2.2 will include an extension model to support the registration of additional providers. Additional providers enhance predictions by providing domain specific command and task completions.

Beginning with 2.2.0-beta1, predictions are displayed in one of two views depending on the user preference.

  • InlineView – This is the default view and displays the prediction inline with the user’s typing. This view is similar to other shells Fish and ZSH.
  • ListView – ListView provides a dropdown list of predictions below the line the user is typing. Users may quickly scan the list, highlight and select the desired prediction.

    Note: Currently for 2.2.0-beta1, ListView is only supported for the Windows edit mode. We will spend more time improving the UX of ListView in Emacs and VI edit modes. To change to the Windows edit mode, run Set-PSReadLineOption -EditMode Windows

Below, predictions are displayed with a dropdown in ListView.

Image pred listview

Availability:

  • PSReadLine 2.2.0-beta1 currently available for download from PSGallery

Supported PowerShell versions for ListView (History-based predictions only):

  • Windows PowerShell 5.1
  • PowerShell 7.0+

Supported PowerShell versions for the plugin subsystem (History and plugin predictions):

  • PowerShell 7.1+

Changing the Prediction View

You can change the view at the command line using the keybinding F2 or Set-PSReadLineOption. The Set-PSReadLineOption may be stored in your profile script.

Set-PSReadLineOption -PredictionViewStyle ListView

Key bindings for ListView

Key bindings control cursor movement and additional features within the prediction. To support ListView, additional key bindings have been added to the Windows edit mode.

F2 is bound with SwitchPredictionView by default for switching between the InlineView and ListView.

Ctrl+z will revert to the original line when a list item is selected, and keep the list view.

Escape will revert to the original line, whether or not a list item is selected, and clear the list view.

UpArrow and DownArrow are used to select an item in the ListView. To navigate history commands while the ListView is present, first press Escape to clear the ListView, then use UpArrow and DownArrow.

Added new color options

In PSReadLine 2.2.0-beta1, Set-PSReadLineOption -Colors Hashtable includes:

  • ListPrediction to set color for the leading > character and source names in ListView
  • ListPredictionSelection to set color for the highlighted selection in ListView

Plugins as a prediction source

Starting with PSReadLine 2.2.0-beta1, Set-PSReadLineOption -PredictionSource will support the following values:

  • None – This option disables Predictive IntelliSense
  • History – This option uses only the PSReadLine history for predictions
  • Plugin – This option uses only registered plugin modules for predictions (can be used only on PS 7.1+)
  • HistoryAndPlugin – This option uses both for predictions (can be used only on PS 7.1+)

But wait! There’s more!

Working with Azure? When you’re ready to extend predictions to include additional plugins, try out the latest preview of the Az.Tools.Predictor from the Azure PowerShell team.

For the latest preview:

Find-Module Az.Tools.Predictor -AllowPrerelease

Read more information, see Az.Tools.Predictor.

3 comments

Discussion is closed. Login to edit/delete existing comments.

  • Michael Mientus 0

    Thanks for ParenthesizeSelection.

    For clarity, ‘discovery’ requires PSReadline 2.2-beta1 and is limited to Az.Tools.Predictor. This is not IntelliSense for PowerShell. When are predictions for standard PowerShell coming?

    Az.Tools.Predictor requires Internet access. How will we know which plugins require Internet access? What privacy policy applies to those lookups?

  • Christine Natalia 0

    Is this going to help with PowerShell in VSCode? It’s my understanding that PowerShell suffered from PSReadline issues. It’s why I went to PowerShell Preview for VSCode because it handled PSReadline better.

    • Jason HelmickMicrosoft employee 0

      Hey Christine! I don’t have anything to announce at this time, but we are investigating adding Predictive IntelliSense to VSCode. In part, this means understanding how to present the information that Predictive IntelliSense provides in VSCode. You may have noticed that predictions using InLineView present an entire line from your history, making it easy to quickly recognize and execute the whole line completion that you want. If you change to ListView and try the Az.Tools.Predictor, you might notice that instead of an entire line, you receive specific parameters (and arguments) based on your current work. These tokenized completions attempt to show you only the parameters you need to complete a specific task – helping you learn the new cmdlet and be successful.

      Today, we haven’t made any decisions as we’re very early in this investigation. I look forward to engaging the community for feedback and suggestions as we proceed.

      Jason

Feedback usabilla icon