Towards a better console - PSReadLine for PowerShell command line editing
Sometimes textmode is where it's at. I've long blogged about tools and techniques that will make your Windows console experience better. Perhaps you're a *nix person who is using Windows in your day job, or you wish the Windows PowerShell prompt was more nix-y. Or perhaps you're a PowerShell person who wants to take your command-line to the next level.
Well, just as NuGet is how we get .NET libraries quickly, and Chocolately is a kind of apt-get for Windows, PsGet is a way to easily add PowerShell modules to your prompt.
To install PsGet you run this script (feel free to vet it):
(new-object Net.WebClient).DownloadString("http://psget.net/GetPsGet.ps1") | iex
Once you've got PsGet, the purpose of this post is to introduce you to PSReadLine.
To install PsReadLine with PsGet, just
If you want to install PsReadLine manually, you can from their readme. It does a LOT:
PsReadLine replaces the command line editing experience in PowerShell.exe. 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
- Automatic saving of history, including sharing history across live sessions
- "Menu" completion (somewhat like Intellisense, select completion with arrows) via Ctrl+Space
But it doesn't replace it in a scary "moved my cheese" way, but in a comfortable familiar way, similar to how Bash works now. It will add things that you WILL miss when you move to another machine that doesn't have PsReadline. If you are already comfortable (or learning) PowerShell, this will feel comfortable immediately. It's not Dvorak. ;)
Some cool PsReadLine examples
Syntax coloring for things like keywords (cd) and common commands (git):
PowerShell often has you opening parentheses, brackets and things, and then you have to count them to close them. PsReadLine helps with that also:
Not only does it give you nice syntax-highlighting for things like function building, it also shows me with the red > that I haven't closed the block.
When you are editing a multi-line script, you can also now backup to other lines!
If you are typing something like Get-Process and either want to autocomplete switches, or autocomplete results, you can press Ctrl-Space:
If you're advanced, check out get-PSReadlineKeyHandler and not only look at what functions are bound to which hotkeys, BUT also check out all the functions that AREN'T bound. You have a lot of power for customization here!
You can even set Emacs keybindings!
Set-PSReadlineOption -EditMode Emacs
Go check out https://github.com/lzybkr/PSReadLine on GitHub and give it a star!
- Making a better, somewhat prettier, but definitely more functional Windows Command Line<
- ConEmu - The Windows Terminal/Console/Prompt we've been waiting for?
- Console2 - A Better Windows Command Prompt
Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.
- Ctrl+v pastes clipboard text to the console.
- Ctrl+a, Ctrl+c copies the entire command line to the clipboard
- Ctrl+z and Ctrl+y act as undo/redo for command line edits. Accidentally delete too many characters? Press Ctrl+z a few times.
- PageUp and PageDown work as you would expect.
- Ctrl+l clears the screen
- You can get PSReadline to save history of all sessions and then search it later
install-module PsReadline(as in the post) doesn't work, but
install-module PsReadLinedoes. (Or I just got some transient error when trying the first command.)
These tools makes me not want to use powershell on a regular machine in the wild :)
System.MethodAccessException: Attempt by method 'PSConsoleUtilities.PSConsoleReadLine.DoReplacementForCompletion(System.Management.Automation.CompletionResult, System.Management.Automation.CommandCompletion)' to access method 'System.Management.Automation.CommandCompletion.set_ReplacementLength(Int32)' failed.
at PSConsoleUtilities.PSConsoleReadLine.DoReplacementForCompletion(CompletionResult completionResult, CommandCompletion completions)
at PSConsoleUtilities.PSConsoleReadLine.Complete(Boolean forward)
at PSConsoleUtilities.PSConsoleReadLine.ProcessOneKey(ConsoleKeyInfo key, Dictionary`2 dispatchTable, Boolean ignoreIfNoAction, Object arg)
at PSConsoleUtilities.PSConsoleReadLine.ReadLine(Runspace remoteRunspace)
It would be very good to have something like intellisense for command parameters!
Import-Module : The 'C:\...\WindowsPowerShell\Modules\PSReadLine\PSReadLine.psd1' module cannot be imported because its manifest contains one or more members that are not valid. The valid manifest members are ('ModuleToProcess', 'NestedModules', 'GUID', 'Author', 'CompanyName', 'Copyright', 'ModuleVersion', 'Description', 'PowerShellVersion', 'PowerShellHostName', 'PowerShellHostVersion', 'CLRVersion', 'DotNetFrameworkVersion', 'ProcessorArchitecture', 'RequiredModules', 'TypesToProcess', 'FormatsToProcess', 'ScriptsToProcess', 'PrivateData', 'RequiredAssemblies', 'ModuleList', 'FileList', 'FunctionsToExport', 'VariablesToExport', 'AliasesToExport', 'CmdletsToExport'). Remove the members t
hat are not valid ('RootModule'), then try to import the module again.
At C:\Users\Ali\Documents\WindowsPowerShell\Modules\PsGet\PsGet.psm1:1312 char:22
+ Import-Module <<<< -Name $ModuleBase -Global -Force:$Force
+ CategoryInfo : InvalidData: (C:\Users\Ali\Do...PSReadLine.psd1:String) [Import-Module], InvalidOperatio
+ FullyQualifiedErrorId : Modules_InvalidManifestMember,Microsoft.PowerShell.Commands.ImportModuleCommand
What worked for me was disabling PowershellGet temporary while installing PsReadLine as follows:
My only gripe with this is that the highlighting (coloring) doesn't work as well on PowerShell ISE as it does on PowerShell.
Comments are closed.