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