Easily set Visual Studio keyboard bindings with the Nuget Package Manager Console

December 6th, 2012

After a fresh install of Visual Studio, I often re-configure some options and setup keyboard bindings. This tends to be a bit of a pain as each and every time I have to remember what setting is in what U.I. configuration pane and how do I find/configure that again? I also will often forget about certain settings until the point I need them and it really breaks my coding rhythm down to go hunt for and reconfigure these options.

I recently found a simple solution to get my VS just the way I want it.

P.S. Some of you may want to tell me all about the VS import/export settings, and you may be right, but I just haven’t spent the time to use/understand/easily find a way to integrate it into my ‘flow’.

So I present to you “MY happy path” to Visual Studio Environment configuration.

1. Install the NuGet Package Manager Extension

You’re going to probably install this extension anyway, so go-ahead and install it now if you don’t already have it… We need this so you can get access to the NuGet Package Manager Console

image

2. Execute Function Set-VisualStudioEnvironmentConfiguration

Wait! What is “Set-VisualStudioEnvironmentConfiguration”?

This is a little function that I have in My PowerShell $Profile which gets setup on all development environment.

So what’s in this PowerShell script?

As of writing this, I only have two specific setup commands, but thought I’d share as an example of what you can do:

  1. Set a keyboard binding to a specific command.
    # Map Ctrl+W to close a tab
    $DTE.Commands.Item("File.Close").Bindings = "Global::Ctrl+W";
  2. ShowLineNumbers for all language files.
    # Turn on line numbers for ALL language types
    ($DTE.Properties("TextEditor", "AllLanguages") | where {$_.Name -eq "ShowLineNumbers" } ).Value = $true
  3. Turn on whitespace?
    # This doesn't work and I wish it did... 
    $DTE.ExecuteCommand("Edit.ViewWhiteSpace")
    # Fails with: "Command "Edit.ViewWhiteSpace" is not available."
    # Maybe one of you can help me out?

      How can I find and setup the settings I like?

    I’m no VS expert and only know as much about the $DTE object as what I’ve written about here, but I’ll give you some tips and you can go from there…

  • Google/Bing are your friends. Type “DTE Visual Studio {TheThingYouWantToAccomplish}”
  • After your search, most examples you find will be VB macros and as it turns out VB (in this case) translates nicely to PowerShell (EX: 
      VB Macro:  DTE.Commands.Item("File.Close").Bindings = "Global::Ctrl+W"

    PowerShell: $DTE.Commands.Item("File.Close").Bindings = "Global::Ctrl+W"

    See the difference? (yep just the ‘$’ at the beginning of the PowerShell one) Nifty eh?

  • Don’t be afraid to use PowerShell to search/filter things in the $DTE. Try this:

    PM>  $DTE.Commands | where { $_.Name –match ‘Close’ } | select { $_.Name }

    Happy VS Environment setting-uppers!

  • peterritchie

    Yeah, export/import is much easier. Don’t have to install nuget package manager extension, and don’t have to write powershell commands–you can just use the built-in VS UI to edit commands/menus/etc.

  • ElegantCoder

    The ability to automate at this level is huge. Thanks for this.

  • http://twitter.com/oising Oisin (x0n) Grehan

    @peterritchie:disqus @d762c661517d3fec78da4c00d3a35fa0:disqus Have neither of you guys seen studioshell.codeplex.com yet? There’s a nuget package too – the entire VS DTE OM is mapped to a powershell provider – fully automatable with idiomatic powershell provider cmdlets.