Visual Studio hotkeys at the PowerShell command line in Windows Terminal
Muscle memory is a heck of a thing. When I want to build code I type Ctrl-Shift-B. I can't not. It's built into my hands! Ctrl-Shift-T is test (even though it's non-standard, it's there, in my hands.
I spend a lot of time at the command line, in Windows Terminal, in PowerShell, using PSReadLine. So why not make a few of these intuitive hotkeys work for me there as well?
PSReadLine supports Set-PSReadLineKeyHandler which is basically hotkey bindings to any arbitrary script block.
Here's Shift-Ctrl-B typing dotnet build and pressing enter. Just add these to your $profile, after you've imported PSReadLine via
if ($host.Name -eq 'ConsoleHost')
{
Import-Module PSReadLine
}
Building with Shift-Ctrl-B
Set-PSReadLineKeyHandler -Key Ctrl+Shift+b `
-BriefDescription BuildCurrentDirectory `
-LongDescription "dotnet Build the current directory" `
-ScriptBlock {
[Microsoft.PowerShell.PSConsoleReadLine]::RevertLine()
[Microsoft.PowerShell.PSConsoleReadLine]::Insert("dotnet build")
[Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine()
}
Here's Shift-Ctrl-T typing dotnet test and pressing enter.
Set-PSReadLineKeyHandler -Key Ctrl+Shift+t `
-BriefDescription TestCurrentDirectory `
-LongDescription "dotnet Test the current directory" `
-ScriptBlock {
[Microsoft.PowerShell.PSConsoleReadLine]::RevertLine()
[Microsoft.PowerShell.PSConsoleReadLine]::Insert("dotnet test")
[Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine()
}
Here's it in Animated Gif Form! (Using Carnac to see the hotkeys being pressed)
Since I am using Ctrl+Shift+T for testing (that's just me) I did need to manually unbind it from New Tab in my Windows Terminal settings. Just be aware.
{
"command": "unbound",
"keys": "shift+ctrl+T"
},
Sweet. What hotkeys will YOU hook up?
Sponsor: Have what it takes to code securely? Select from dozens of complimentary interactive challenges in Veracode Security Labs, test your skills, and earn a badge. Learn more.
About Scott
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.
About Newsletter
- Cut: SHIFT+DELETE CTRL+X
- Copy: CTRL+INSERT CTRL+C
- Paste: SHIFT+INSERT CTRL+V
They still work in surprisingly many applications.
Set-PSReadlineKeyHandler -Function CaptureScreen -Chord Ctrl+[
To run it adhoc is
[Microsoft.PowerShell.PSConsoleReadLine]::CaptureScreen()
Comments are closed.
A bit offtopic:
May you share your current oh-my-posh configuration? It looks like you have already upgraded to 3.*.
Do you still use Cascadia Code, and if yes, how?
What's your prompt theme now?
Thanks!