Scott Hanselman

Visual Studio hotkeys at the PowerShell command line in Windows Terminal

March 18, 2021 Comment on this post [7] Posted in Open Source | VS2019
Sponsored By

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.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service
March 23, 2021 7:15
Thanks for sharing, there plenty of possibilities! 👍

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!
March 23, 2021 9:31
Hello! I really have a good day every time when I read awesome essay works at the most pro and the greatest online writing agency. It is very informative and you've the power to persuade your readers. I hope, they will always update their projects, so we can see what is the latest interesting and great news they can share with us at all!
March 23, 2021 9:58
I tend to use the clipboard hotkeys from Windows 3.0.

  • Cut: SHIFT+DELETE CTRL+X
  • Copy: CTRL+INSERT CTRL+C
  • Paste: SHIFT+INSERT CTRL+V



They still work in surprisingly many applications.
March 23, 2021 17:33
My favorite was shared by Joey Aiello at an Ignite tabletalk: Take a "screenshot" of the terminal.

Set-PSReadlineKeyHandler -Function CaptureScreen -Chord Ctrl+[


To run it adhoc is
[Microsoft.PowerShell.PSConsoleReadLine]::CaptureScreen()

March 24, 2021 7:41
Oh this is really nice! Already thinking about how to add this to my bookmarking system. The use of insert/acceptline is a bit of a revelation - I'd been struggling with "replaying" stored commands because the shell kept tokenising quoted strings inside the command - hopefully acceptline will work around that.
March 25, 2021 9:53
Very hard written article, Thanks for sharing with us, keep writing this type of the article. Shein Customer Service
March 27, 2021 14:51
Can we use shortcuts to clear the screen while dotnet run is still running? Typically I start my app, do something, observe console output, and then I want to clear the console so I can observe the output again. In Rider, I use CTRL+L but haven’t figured out how to do the same in Windows Terminal.

Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.