Friday, January 6, 2012

Listing all keyboard shortcuts in Visual Studio 2010

The NuGet package manager for Visual Studio comes with a hidden gem - a PowerShell console called Package Manager Console. This console not only allows developers to install and configure NuGet packages, but also has a useful variable called $dte, which is the Visual Studio automation COM object.

To demonstrate the usefulness of this feature, I thought it would be interesting to list all the keyboard shortcuts that I currently had bound. To do this, I ran the following PowerShell one-liner:

$dte.Commands | ? { $_.Bindings.length -ne 0 -and $ -ne 0 } | sort Name | select Name, Bindings | % { $b = $_.Name; $_.Bindings | % { $_ -match '(.*)::(.*)' | out-null; new-object psobject -Property @{ Name = $b; Context = $Matches[1]; Key = $Matches[2]} } } | sort Context, Name, Key | select Context, Name, Key | ft -AutoSize
Of course, if you want to grab this information and format it for better printing, just replace the last step in the pipeline (ft -AutoSize) with ConvertTo-Csv | clip. This will copy all your shortcuts into the clipboard in CSV format, and you can paste them into Excel for better formatting and printing.


Post a Comment