Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A bash inspired readline implementation for PowerShell

License

NotificationsYou must be signed in to change notification settings

PowerShell/PSReadLine

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 aboutPSReadLine:

  • Keith Hill wrote agreat introduction (2013) toPSReadLine.
  • Ed Wilson (Scripting Guy) wrote aseries (2014-2015) onPSReadLine.
  • John Savill has avideo (2021) covering installation, configuration, and tailoringPSReadLine to your liking.

Installation and Upgrading

You will need the1.6.0 or a higher version ofPowerShellGet to install or upgrade to the latest prerelease version ofPSReadLine.

PowerShell 6+ already has a higher version ofPowerShellGet built-in.However, Windows PowerShell 5.1 ships an older version ofPowerShellGet which doesn't support installing prerelease modules.So, Windows PowerShell users need to install the latestPowerShellGet (if not yet) by running the following commands from an elevated Windows PowerShell session:

Install-Module-Name PowerShellGet-Force;exit

After installingPowerShellGet, you install or upgrade to the latest prerelease version ofPSReadLine by running

Install-Module PSReadLine-Repository PSGallery-Scope CurrentUser-AllowPrerelease-Force

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

Install-Module PSReadLine-Repository PSGallery-Scope CurrentUser-Force

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

Usage

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 toSet-PSReadLineOption.PSReadLine has help for its cmdlets as well as anabout_PSReadLine topic - see those topics for more detailed help.

To set your own custom keybindings, use the cmdletSet-PSReadLineKeyHandler.For example, for a better history experience, try:

Set-PSReadLineKeyHandler-Key UpArrow-Function HistorySearchBackwardSet-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 useVSCode,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.

Thesample profile file has a bunch of great examples to check out. This file is included whenPSReadLine 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 theContribution Guide for how to develop and contribute.

Building

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

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

  • Bootstrap:./build.ps1 -Bootstrap
  • Build:./build.ps1 -Configuration Debug
  • Test:
    • Targeting .NET 4.7.2 (Windows only):./build.ps1 -Test -Configuration Debug -Framework net472
    • Targeting .NET 6.0:./build.ps1 -Test -Configuration Debug -Framework net6.0

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 runpwsh -NonInteractive -NoProfile to not automatically load the default PSReadLine module installed.Then, load the locally built PSReadLine module byImport-Module <your-local-repo-root>/bin/Debug/PSReadLine/PSReadLine.psd1.

Change Log

The change log is availablehere.

Licensing

PSReadLine is licensed under the2-Clause BSD License.

Code of Conduct

Please see ourCode of Conduct before participating in this project.

Security Policy

For any security issues, please see ourSecurity Policy.

About

A bash inspired readline implementation for PowerShell

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors65


[8]ページ先頭

©2009-2025 Movatter.jp