Convert Figma logo to code with AI

PowerShell logoPSReadLine

A bash inspired readline implementation for PowerShell

3,694
293
3,694
180

Top Related Projects

95,296

The new Windows Terminal and the original Windows console host, all in the same place!

The most customisable and low-latency cross platform/shell prompt renderer

172,325

šŸ™ƒ A delightful community-driven (with 2,300+ contributors) framework for managing your zsh configuration. Includes 300+ optional plugins (rails, git, macOS, hub, docker, homebrew, node, php, python, etc), 140+ themes to spice up your morning, and an auto-update tool that makes it easy to keep up with the latest updates from the community.

The user-friendly command line shell.

3,630

Mirror of the Z shell source code repository.

A Zsh theme

Quick Overview

PSReadLine is a PowerShell module that enhances the command-line editing experience in PowerShell. It provides a rich set of features including syntax highlighting, multi-line editing, customizable key bindings, and predictive IntelliSense. PSReadLine aims to improve productivity and ease of use for PowerShell users.

Pros

  • Enhances PowerShell command-line editing with advanced features
  • Customizable key bindings and color schemes
  • Supports cross-platform usage (Windows, macOS, Linux)
  • Integrates well with existing PowerShell workflows

Cons

  • May require some learning curve for users accustomed to basic PowerShell editing
  • Some features might not work consistently across all platforms
  • Can occasionally conflict with other PowerShell modules or scripts
  • Performance impact on slower systems when using advanced features

Code Examples

  1. Enabling predictive IntelliSense:
Set-PSReadLineOption -PredictionSource History

This code enables predictive IntelliSense based on command history.

  1. Customizing key bindings:
Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward

This example changes the up and down arrow keys to search through history based on the current input.

  1. Changing color scheme:
Set-PSReadLineOption -Colors @{
    Command = 'Yellow'
    Parameter = 'DarkGray'
    Operator = 'Magenta'
    Variable = 'Green'
    String = 'Cyan'
    Number = 'DarkGreen'
    Member = 'DarkYellow'
    Type = 'DarkBlue'
    Comment = 'DarkGray'
}

This code customizes the color scheme for different syntax elements.

Getting Started

To get started with PSReadLine:

  1. Install the module (if not already included in your PowerShell version):

    Install-Module -Name PSReadLine -AllowPrerelease -Force
    
  2. Import the module in your PowerShell profile:

    Import-Module PSReadLine
    
  3. Configure options in your PowerShell profile:

    Set-PSReadLineOption -PredictionSource History
    Set-PSReadLineOption -HistorySearchCursorMovesToEnd
    Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward
    Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward
    

These steps will install PSReadLine, import it, and set up some basic configurations to enhance your PowerShell experience.

Competitor Comparisons

95,296

The new Windows Terminal and the original Windows console host, all in the same place!

Pros of Terminal

  • Provides a full-featured terminal emulator with customizable UI and themes
  • Supports multiple shells and tab management for improved productivity
  • Offers GPU-accelerated text rendering for better performance

Cons of Terminal

  • Larger project scope, potentially more complex to contribute to or customize
  • Focused on the terminal interface rather than command-line editing features
  • May have a steeper learning curve for users new to terminal customization

Code Comparison

PSReadLine (PowerShell command-line editing):

Set-PSReadLineOption -PredictionSource History
Set-PSReadLineOption -HistorySearchCursorMovesToEnd
Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward

Terminal (JSON configuration):

{
    "profiles": {
        "defaults": {
            "colorScheme": "Campbell",
            "fontFace": "Cascadia Code",
            "useAcrylic": true
        }
    }
}

Summary

PSReadLine focuses on enhancing PowerShell's command-line editing capabilities, while Terminal provides a comprehensive terminal emulator with support for multiple shells. PSReadLine is more specialized for PowerShell users, offering features like predictive IntelliSense and custom key bindings. Terminal, on the other hand, provides a broader set of features for various command-line interfaces, including customizable UI, tabs, and GPU acceleration.

The most customisable and low-latency cross platform/shell prompt renderer

Pros of oh-my-posh

  • Highly customizable prompt themes with rich visual elements
  • Cross-platform support (Windows, macOS, Linux)
  • Extensive icon and glyph support for enhanced visual appeal

Cons of oh-my-posh

  • Steeper learning curve for configuration and customization
  • May impact performance on slower systems due to additional processing
  • Requires installation of a compatible font for full functionality

Code Comparison

PSReadLine (default prompt):

PS C:\Users\username>

oh-my-posh (example custom prompt):

āÆ ~/Documents
[main ā‰”] āš” 3 šŸ“¦ 5 šŸ•™ 10:30:45

PSReadLine focuses on enhancing the PowerShell command-line editing experience, while oh-my-posh is primarily designed for customizing and enhancing the PowerShell prompt appearance. PSReadLine provides features like syntax highlighting, predictive IntelliSense, and improved command history navigation. oh-my-posh, on the other hand, offers highly customizable prompt themes with segment-based configurations, allowing users to display various information such as git status, battery level, and execution time in their prompt.

172,325

šŸ™ƒ A delightful community-driven (with 2,300+ contributors) framework for managing your zsh configuration. Includes 300+ optional plugins (rails, git, macOS, hub, docker, homebrew, node, php, python, etc), 140+ themes to spice up your morning, and an auto-update tool that makes it easy to keep up with the latest updates from the community.

Pros of Oh My Zsh

  • Extensive plugin ecosystem with a wide range of pre-configured options
  • Highly customizable themes for enhanced terminal aesthetics
  • Active community support and frequent updates

Cons of Oh My Zsh

  • Limited to Zsh shell, not compatible with PowerShell environments
  • Can potentially slow down shell startup time due to numerous plugins

Code Comparison

Oh My Zsh (theme configuration):

ZSH_THEME="robbyrussell"
plugins=(git docker kubectl)
source $ZSH/oh-my-zsh.sh

PSReadLine (custom key bindings):

Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward
Set-PSReadLineOption -PredictionSource History

Oh My Zsh focuses on providing a rich set of pre-configured options and themes for Zsh, while PSReadLine enhances PowerShell's command-line editing experience. Oh My Zsh offers a more comprehensive out-of-the-box solution for terminal customization, but is limited to Zsh. PSReadLine, on the other hand, provides powerful command-line editing features specifically for PowerShell, with a focus on productivity and efficiency rather than visual customization.

The user-friendly command line shell.

Pros of fish-shell

  • More user-friendly out-of-the-box with features like autosuggestions and web-based configuration
  • Syntax highlighting is more advanced and customizable
  • Simpler scripting language, easier for beginners to learn

Cons of fish-shell

  • Less compatible with POSIX shell scripts, which may require modifications
  • Smaller ecosystem and community compared to PowerShell
  • Limited cross-platform support, primarily focused on Unix-like systems

Code Comparison

fish-shell:

function greet
    echo "Hello, $argv!"
end

greet World

PSReadLine:

function Greet {
    param($name)
    Write-Output "Hello, $name!"
}

Greet -name "World"

Key Differences

  • fish-shell uses a simpler syntax for function definition and parameter handling
  • PSReadLine (PowerShell) has more verbose function declarations but offers stronger typing and parameter validation
  • fish-shell's scripting language is designed to be more intuitive for interactive use
  • PowerShell provides a more comprehensive scripting environment with object-oriented features

Both projects aim to enhance the command-line experience, but fish-shell focuses on user-friendliness and interactive use, while PSReadLine extends PowerShell's capabilities for both interactive and scripting scenarios.

3,630

Mirror of the Z shell source code repository.

Pros of zsh

  • More extensive customization options and plugins ecosystem
  • Better out-of-the-box experience with features like advanced tab completion
  • Cross-platform compatibility (Unix-like systems, including macOS)

Cons of zsh

  • Steeper learning curve for advanced features and customization
  • Less integrated with modern Windows environments
  • May require more manual configuration for optimal setup

Code Comparison

PSReadLine:

Set-PSReadLineOption -PredictionSource History
Set-PSReadLineOption -HistorySearchCursorMovesToEnd
Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward

zsh:

autoload -U compinit && compinit
zstyle ':completion:*' menu select
bindkey '^[[A' history-substring-search-up
bindkey '^[[B' history-substring-search-down

Summary

PSReadLine is a module for PowerShell that enhances the command-line editing experience, while zsh is a full-featured shell with its own ecosystem. PSReadLine is more focused on Windows environments and integrates tightly with PowerShell, whereas zsh offers broader customization options and is more prevalent in Unix-like systems. Both provide advanced features for command-line editing, history searching, and auto-completion, but zsh generally offers more extensive customization possibilities at the cost of a steeper learning curve.

A Zsh theme

Pros of powerlevel10k

  • Highly customizable and feature-rich prompt theme for Zsh
  • Extremely fast and efficient, even with complex configurations
  • Supports a wide range of icons and glyphs for enhanced visual appeal

Cons of powerlevel10k

  • Limited to Zsh shell, not compatible with PowerShell
  • Steeper learning curve for configuration compared to PSReadLine
  • May require additional font installation for full glyph support

Code Comparison

PSReadLine (PowerShell):

Set-PSReadLineOption -PredictionSource History
Set-PSReadLineOption -HistorySearchCursorMovesToEnd
Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward

powerlevel10k (Zsh):

POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir vcs)
POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(status root_indicator background_jobs time)
POWERLEVEL9K_PROMPT_ON_NEWLINE=true

Summary

While PSReadLine enhances PowerShell's command-line editing capabilities, powerlevel10k focuses on creating a highly customizable and visually appealing prompt for Zsh. PSReadLine is more versatile across different shells, whereas powerlevel10k offers a more advanced and feature-rich prompt experience specifically for Zsh users. The choice between the two depends on the user's preferred shell and desired level of prompt customization.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

appveyor-build-status

PSReadLine

This module replaces the command line editing experience of PowerShell for versions 3 and up. It 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

The "out of box" experience is meant to be very familiar to PowerShell users - there should be no need to learn any new key strokes.

Some good resources about PSReadLine:

  • Keith Hill wrote a great introduction (2013) to PSReadLine.
  • Ed Wilson (Scripting Guy) wrote a series (2014-2015) on PSReadLine.
  • John Savill has a video (2021) covering installation, configuration, and tailoring PSReadLine to your liking.

Installation

There are multiple ways to install PSReadLine.

Install from PowerShellGallery (preferred)

You will need the 1.6.0 or a higher version of PowerShellGet to install the latest prerelease version of PSReadLine.

Windows PowerShell 5.1 ships an older version of PowerShellGet which doesn't support installing prerelease modules, so Windows PowerShell users need to install the latest PowerShellGet (if not yet) by running the following commands from an elevated Windows PowerShell session:

Install-Module -Name PowerShellGet -Force
Exit

After installing PowerShellGet, you can get the latest prerelease version of PSReadLine by running

Install-Module PSReadLine -AllowPrerelease -Force

If you only want to get the latest stable version, run:

Install-Module PSReadLine

[!NOTE] Prerelease versions will have newer features and bug fixes, but may also introduce new issues.

If you are using Windows PowerShell on Windows 10 or using PowerShell 6+, PSReadLine is already installed. Windows PowerShell on the latest Windows 10 has version 2.0.0-beta2 of PSReadLine. PowerShell 6+ versions have the newer prerelease versions of PSReadLine.

Install from GitHub (deprecated)

With the preview release of PowerShellGet for PowerShell V3/V4, downloads from GitHub are deprecated. We don't intend to update releases on GitHub, and may remove the release entirely from GitHub at some point.

Post Installation

If you are using Windows PowerShell V5 or V5.1 versions, or using PowerShell 6+ versions, you are good to go and can skip this section.

Otherwise, you need to edit your profile to import the module. There are two profile files commonly used and the instructions are slightly different for each. The file C:\Users\[User]\Documents\WindowsPowerShell\profile.ps1 is used for all hosts (e.g. the ISE and powershell.exe). If you already have this file, then you should add the following:

if ($host.Name -eq 'ConsoleHost')
{
    Import-Module PSReadLine
}

Alternatively, the file C:\Users\[User]\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 is for powershell.exe only. Using this file, you can simply add:

Import-Module PSReadLine

In either case, you can create the appropriate file if you don't already have one.

Upgrading

When running one of the suggested commands below, be sure to exit all instances of powershell.exe, pwsh.exe or pwsh, including those opened in VSCode terminals.

Then, to make sure PSReadLine isn't loaded:

  • if you are on Windows, run the suggested command below from cmd.exe, powershell_ise.exe, or via the Win+R shortcut;
  • if you are on Linux/macOS, run the suggested command below from the default terminal (like bash or zsh).

If you are using the version of PSReadLine that ships with Windows PowerShell, you need to run: powershell -noprofile -command "Install-Module PSReadLine -Force -SkipPublisherCheck -AllowPrerelease". Note: you will need to make sure PowershellGet is updated before running this command.

If you are using the version of PSReadLine that ships with PowerShell 6+ versions, you need to run: <path-to-pwsh-executable> -noprofile -command "Install-Module PSReadLine -Force -SkipPublisherCheck -AllowPrerelease".

If you've installed PSReadLine yourself from the PowerShell Gallery, you can simply run: powershell -noprofile -command "Update-Module PSReadLine -AllowPrerelease" or <path-to-pwsh-executable> -noprofile -command "Update-Module PSReadLine -AllowPrerelease", depending on the version of PowerShell you are using.

If you get an error like:

Remove-Item : Cannot remove item
C:\Users\{yourName}\Documents\WindowsPowerShell\Modules\PSReadLine\Microsoft.PowerShell.PSReadLine.dll: Access to the path
'C:\Users\{yourName}\Documents\WindowsPowerShell\Modules\PSReadLine\Microsoft.PowerShell.PSReadLine.dll' is denied.

or a warning like:

WARNING: The version '2.0.0' of module 'PSReadLine' is currently in use. Retry the operation after closing the applications.

Then you didn't kill all the processes that loaded PSReadLine.

Usage

To start using, just import the module:

Import-Module PSReadLine

To use Emacs key bindings, you can use:

Set-PSReadLineOption -EditMode Emacs

To view the current key bindings:

Get-PSReadLineKeyHandler

There are many configuration options, see the options to Set-PSReadLineOption. PSReadLine has help for it's cmdlets as well as an about_PSReadLine topic - see those topics for more detailed help.

To set your own custom keybindings, use the cmdlet Set-PSReadLineKeyHandler. For example, 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've entered some text though, it will search the history for commands that start with the currently entered text.

To enable bash style completion without using Emacs mode, you can use:

Set-PSReadLineKeyHandler -Key Tab -Function Complete

Here is a more interesting example of what is possible:

Set-PSReadLineKeyHandler -Chord '"',"'" `
                         -BriefDescription SmartInsertQuote `
                         -LongDescription "Insert paired quotes if not already on a quote" `
                         -ScriptBlock {
    param($key, $arg)

    $line = $null
    $cursor = $null
    [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)

    if ($line.Length -gt $cursor -and $line[$cursor] -eq $key.KeyChar) {
        # Just move the cursor
        [Microsoft.PowerShell.PSConsoleReadLine]::SetCursorPosition($cursor + 1)
    }
    else {
        # Insert matching quotes, move cursor to be in between the quotes
        [Microsoft.PowerShell.PSConsoleReadLine]::Insert("$($key.KeyChar)" * 2)
        [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)
        [Microsoft.PowerShell.PSConsoleReadLine]::SetCursorPosition($cursor - 1)
    }
}

In this example, when you type a single quote or double quote, there are two things that can happen. If the character following the cursor is not the quote typed, then a matched pair of quotes is inserted and the cursor is placed inside the the matched quotes. If the character following the cursor is the quote typed, the cursor is simply moved past the quote without inserting anything. If you use Resharper or another smart editor, this experience will be familiar.

Note that with the handler written this way, it correctly handles Undo - both quotes will be undone with one undo.

The sample profile file has a bunch of great examples to check out. This file is included when PSReadLine is installed.

See the public methods of [Microsoft.PowerShell.PSConsoleReadLine] to see what other built-in functionality you can modify.

If you want to change the command line in some unimplmented way in your custom key binding, you can use the methods:

    [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState
    [Microsoft.PowerShell.PSConsoleReadLine]::Insert
    [Microsoft.PowerShell.PSConsoleReadLine]::Replace
    [Microsoft.PowerShell.PSConsoleReadLine]::SetCursorPosition

Developing and Contributing

Please see the Contribution Guide for how to develop and contribute.

Building

To build PSReadLine on Windows, Linux, or macOS, you must have the following installed:

  • .NET Core SDK 2.1.802 or a newer version
  • The PowerShell modules InvokeBuild and platyPS

The build script build.ps1 can be used to bootstrap, build and test the project.

  • Bootstrap: ./build.ps1 -Bootstrap
  • Build:
    • Targeting .NET 4.6.2 (Windows only): ./build.ps1 -Configuration Debug -Framework net462
    • Targeting .NET Core: ./build.ps1 -Configuration Debug -Framework netcoreapp2.1
  • Test:
    • Targeting .NET 4.6.2 (Windows only): ./build.ps1 -Test -Configuration Debug -Framework net462
    • Targeting .NET Core: ./build.ps1 -Test -Configuration Debug -Framework netcoreapp2.1

After build, the produced artifacts can be found at <your-local-repo-root>/bin/Debug. In order to isolate your imported module to the one locally built, be sure to run pwsh -NonInteractive -NoProfile to not automatically load the default PSReadLine module installed. Then, load the locally built PSReadLine module by Import-Module <your-local-repo-root>/bin/Debug/PSReadLine/PSReadLine.psd1.

Change Log

The change log is available here.

Licensing

PSReadLine is licensed under the 2-Clause BSD License.

Code of Conduct

Please see our Code of Conduct before participating in this project.

Security Policy

For any security issues, please see our Security Policy.