You should be customizing your PowerShell Prompt with PSReadLine

I use PowerShell 7 (cross platform, open source, runs on .NET Core) as my main shell of choice at the Windows command line. I use it in the Windows Terminal and I have a pretty prompt thanks to OhMyPosh. I've also set up autocomplete in PowerShell (type something, then TAB) with git and dotnet!



I thought I had things pretty well dialed in. I even used PSReadLine, a bash inspired readline implementation for PowerShell.


But was I REALLY USING IT? No. Honestly, at the time I wouldn't be able to tell you what it offered me as a command line users. Wow was I wrong.


Don't sleep on PSReadLine if you use PowerShell as your preferred prompt. Head over there and give them some love and a star and buckle up, friends!


Head over to a prompt and run

Install-Module PSReadLine -AllowPrerelease -Force

If you want the latest, otherwise remove the Prerelease. Then edit your $profile. I usually do this:

notepad $PROFILE

And add

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

First, PSReadLine makes everything better with sprinkles of color everywhere automatically. But the thing I was not really digging into was customizing my $profile to light up cool features and set keybindings that made sense to me.



It was this totally configured and blinged out sample PSReadline Profile that made me realize I wasn't doing enough. I cherry-picked the best parts out of this and I recommend you do the same!


You get nice improvements with bash-like command line editing. The most important one being the PowerShell equivalent of ctrl-r "bck-i-search" that bash users enjoy.


You can also set command line handlers so pressing "up arrow" with some existing text will find that item in history. Set it up once in your $profile.

Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward

Like Emacs but want PowerShell to be more like Emacs? It's possible!

Set-PSReadLineOption -EditMode Emacs

Got PSReadLine already but you're not sure what is key bindings are set up today?

Get-PSReadLineKeyHandler

This will give you a complete picture. You know how you can "ctrl shift b" in Visual Studio to build your project? I made ctrl shift b type "dotnet build" for me in PowerShell! Muscle memory for the win!


# This is an example of a macro that you might use to execute a command.
# This will add the command to history.
Set-PSReadLineKeyHandler -Key Ctrl+Shift+b `
 -BriefDescription BuildCurrentDirectory `
 -LongDescription "Build the current directory" `
 -ScriptBlock {
 [Microsoft.PowerShell.PSConsoleReadLine]::RevertLine()
 [Microsoft.PowerShell.PSConsoleReadLine]::Insert("dotnet build")
 [Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine()
}


Check out the really advanced stuff in the sample $profile. I like the Smart Insert/Delete. It will take over your ' quotes and quotes " and braces { and ) parens and make them auto matching! That means you can type a variable or string, select it with Shift Arrow Key or Ctrl Shift Arrow Key, then typeover it with a " and it'll automatically quote the whole string. A very nice command line experience that you may enjoy in other shells but you never really set up PowerShell. Even more useful is Alt+' that will take a string and change it from a 'string' to a "string."


Take a few moments and think about the things you type too often. The things you type twice, or ten times a day. Check out your $profile, consider your aliases, and tidy up. I suspect PSReadLine could help. It was great for me!


Source: Paper.li

0 comments