Phil Haack was at my desk today and saw something that I’ve set up in Visual Studio 2008 for quickly changing my profile.  It’s a trick that Sara Ford taught us in her infamous Visual Studio Tip of the Day series.  I use toolbar buttons to quickly change profiles in Visual Studio.

I work in 2 different codebases, and as it turns out, they have different formatting guidelines; one uses KnR and the other uses Allman.

   1: // KnR
   2: if (condition) {
   3:     DoWork();
   4: }
   6: // Allman (a.k.a. BSD)
   7: if (condition)
   8: {
   9:     DoWork();
  10: }


In addition to this, I like to use a black background theme, very similar to Rob Conery’s.  In fact, I started with his theme and then customized it slightly.  The black background is great for coding, but sometimes when I need to copy and paste code into a document or other medium, it fails.  Thus I need to be able to quickly switch to a more documentation-friendly format.  There’s always the need for quickly jumping into presentation mode too.  To recap, I need to be able to quickly switch to the following formats:

  1. My default settings
  2. Documentation settings
  3. Presentation mode
  4. Allman code formatting
  5. KnR code formatting

I’ve leveraged Sara’s tip to give myself 5 toolbar buttons for these purposes, after exporting settings for each of the items.  My default settings export file includes ALL VS options—consider it a reboot of my settings so that no matter what state I’m in, I can always revert back to that.  The Documentation settings only has fonts and colors defined, leaving all other VS settings alone.  Presentation mode hides a a bunch of things to maximize the code window; it also adjusts the fonts for the audience’s benefit.  The Allman and KnR settings only adjust the code formatting settings, leaving everything else alone.

Once I had all of the setting exports tucked away, I was able to easily create a set of macros to quickly import each of them.

   1: Imports System
   2: Imports EnvDTE
   3: Imports EnvDTE80
   4: Imports EnvDTE90
   5: Imports System.Diagnostics
   7: Public Module Settings
   9:   Private RootFolder As String = "[Your Root]\Documents\Visual Studio 2008\Settings\"
  11:   Public Sub ImportCodingSettings()
  12:         ImportSettingsFile("Handley.Active")
  13:   End Sub
  15:   Public Sub ImportDesignDocumentSettings()
  16:         ImportSettingsFile("DesignDocumentColors")
  17:   End Sub
  19:   Public Sub ImportPresentationSettings()
  20:         ImportSettingsFile("Presentation")
  21:   End Sub
  23:   Public Sub ImportKnRFormatting()
  24:         ImportSettingsFile("KnRFormatting")
  25:         DTE.ExecuteCommand("Edit.FormatDocument")
  26:   End Sub
  28:   Public Sub ImportAllmanFormatting()
  29:         ImportSettingsFile("AllmanFormatting")
  30:         DTE.ExecuteCommand("Edit.FormatDocument")
  31:   End Sub
  33:   Private Sub ImportSettingsFile(ByVal FileName As String)
  34:         FileName = IO.Path.Combine(RootFolder, FileName & ".vssettings")
  35:         DTE.ExecuteCommand("Tools.ImportandExportSettings", "-import:""" & FileName & """")
  36:   End Sub
  38: End Module

Notice that the Allman formatting and KnR formatting macros not only import the settings, but they also format the current document as a nice little touch.

After setting up the macros, I then moved on to create toolbar buttons for each one.

Since Phil is like the 7th or so person to ask me to send this stuff to him, I figured I’d just blog it instead.  You can grab all of my settings files and a text file with the above code here.