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.
© 2021 Scott Hanselman. All rights reserved.