Do you have multiple personalities in Visual Studio?  I do.  And so do I.  I like to code with a dark background;I like to send people code snippets using more traditional colors.

Awhile back, I wrote about how to quickly change profiles in Visual Studio, but with Visual Studio 2010, the approach changes slightly.  Here’s an update for how to toggle VS between different font/color themes, or other settings.

Here’s what we’re going to do:

  1. Write a macro to export the current settings, to allow for easy backups
  2. Write a method to import all settings from a specified file
  3. Write a macro to import your last settings backup file for easy restores
  4. Manually export subsets of your settings for different purposes
  5. Write macros to import each of the settings files exported
  6. Integrate all of the above macros into the menu bar

Start off by clicking on Tools\Macros\Macros IDE.  Let’s create a new Macros Module for all of the “Settings” macros.  I chose to call it “Settings” of course.

Add New Item - MyMacros - Settings

For the first macro, let’s call it ExportActiveSettings() and it will export your current settings to a backup file, keeping a date-stamped backup file.  We’re just going to hard-code some strings into the macros; it’s okay, really.

Imports System
 
Public Module Settings
 
  Private RootFolder As String = _
  "D:\Users\jeffhand\Documents\Visual Studio 2010\Settings\"
 
  Private ActiveSettingsFile As String = "Handley.Active"
 
  Public Sub ExportActiveSettings()
  Dim FileName As String = IO.Path.Combine(RootFolder, ActiveSettingsFile & ".vssettings")
  Dim Backup As String = DateTime.Today.ToString("yyyyMMdd")
 
  ' Don't overwrite the backup file -- keep it as it is the record
  ' of what the settings were before today, otherwise, a 2nd save
  ' will lose the backup.
  If System.IO.File.Exists(FileName) _
  AndAlso Not System.IO.File.Exists(FileName & "." & Backup) Then
            System.IO.File.Copy(FileName, FileName & "." & Backup)
  End If
 
        DTE.ExecuteCommand("Tools.ImportandExportSettings", "-export:""" & FileName & """")
  End Sub
 
End Module


Now let’s add a method that will import settings from a specified file, and then a macro that will invoke this method for the last-saved copy of our active settings:

Private Sub ImportSettingsFile(ByVal FileName As String)
    FileName = IO.Path.Combine(RootFolder, FileName & ".vssettings")
    DTE.ExecuteCommand("Tools.ImportandExportSettings", "-import:""" & FileName & """")
End Sub
 
Public Sub ImportActiveSettings()
    ImportSettingsFile(ActiveSettingsFile)
End Sub
 


After saving this macro module, flip back over to Visual Studio.  Click on Tools\Macros\Macro Explorer.  Within that explorer, expand Settings and then double-click on “ExportActiveSettings.”  You might want to check the folder you used to make sure your active settings file was created.

Once you have saved this export, make changes to your settings and then export whatever other settings files you want.  One example is to change your fonts to a presentation mode where you use large fonts and few tool windows.  In the dialog for exporting settings, you can export the subsets of settings affected by your changes.  For my own use, I have creates vssettings files for the following:

  • Handley.Active.Colors.vssettings – uses my active settings, but the file only contains an export of my fonts/colors.  This would allow me to re-import my fonts/colors only, while leaving other settings alone.
  • Default.Colors.vssettings – I reset the settings to the default color scheme and then exported the fonts/colors.  I’ll use this when sending code snippets to people in email, pasting into design documents, or using other media where my dark background theme doesn’t work as well.
  • Presentation.vssettings – This uses my dark theme but large font sizes in the Text Editor, and I’ve made sure that all tools windows are auto-hidden.  I exported only the Options\Environment node when choosing settings to export.
  • Formatting.Allman.vssettings – This has the C# code formatting set to the Allman style brackets and indentation settings.  I exported only these settings using the Options\Text Editor\C# Editor node when choosing settings to export.
  • Formatting.KnR.vssettings – Just like Allman but with the settings switched to use KnR style code formatting.

I ended up opening the Macro Explorer and double-clicking my “ImportActiveSettings” macro a couple of times during this process.

Now, let’s add new macros for each specialized settings file we created.  Here’s the end result of our Settings module:

Imports System
 
Public Module Settings
 
  Private RootFolder As String = _
  "D:\Users\jeffhand\Documents\Visual Studio 2010\Settings\"
 
  Private ActiveSettingsFile As String = "Handley.Active"
 
  Public Sub ExportActiveSettings()
  Dim FileName As String = IO.Path.Combine(RootFolder, ActiveSettingsFile & ".vssettings")
  Dim Backup As String = DateTime.Today.ToString("yyyyMMdd")
 
  ' Don't overwrite the backup file -- keep it as it is the record
  ' of what the settings were before today, otherwise, a 2nd save
  ' will lose the backup.
  If System.IO.File.Exists(FileName) _
  AndAlso Not System.IO.File.Exists(FileName & "." & Backup) Then
            System.IO.File.Copy(FileName, FileName & "." & Backup)
  End If
 
        DTE.ExecuteCommand("Tools.ImportandExportSettings", "-export:""" & FileName & """")
  End Sub
 
  Private Sub ImportSettingsFile(ByVal FileName As String)
        FileName = IO.Path.Combine(RootFolder, FileName & ".vssettings")
        DTE.ExecuteCommand("Tools.ImportandExportSettings", "-import:""" & FileName & """")
  End Sub
 
  Public Sub ImportActiveSettings()
        ImportSettingsFile(ActiveSettingsFile)
  End Sub
 
  Public Sub ImportActiveColors()
        ImportSettingsFile("Handley.Active.Colors")
  End Sub
 
  Public Sub ImportDefaultColors()
        ImportSettingsFile("Default.Colors")
  End Sub
 
  Public Sub ImportPresentationSettings()
        ImportSettingsFile("Presentation")
  End Sub
 
  Public Sub ApplyAllmanFormatting()
        ImportSettingsFile("Formatting.Allman")
        DTE.ExecuteCommand("Edit.FormatDocument")
  End Sub
 
  Public Sub ApplyKnRFormatting()
        ImportSettingsFile("Formatting.KnR")
        DTE.ExecuteCommand("Edit.FormatDocument")
  End Sub
 
End Module

Notice that for ApplyAllmanFormatting and ApplyKnRFormatting, I went ahead and made the macro format the active document, because every time I’ve needed to switch between the 2 formats, I need to reformat the file I’m working on.

You’ll see that the Macro Explorer now shows all of the new macros you’ve added.  You can double-click on the macros and watch your settings get quickly imported without having to step through any dialogs.  Be careful not to accidentally run ExportActiveSettings when you have goofy settings applied.

The only thing left to do now is to integrate these macros into your Visual Studio menu bar.  This is what ultimately makes these macros so useful.  As I mentioned on my older post on this subject, Sara Ford taught us how to integrate macros into our menu/toolbar.  With Visual Studio 2010, I’ve found that we can no longer copy/paste/edit icons for our custom items, which is a bummer; instead we’ll just create a traditional menu for our needs.

  1. Right-click on the menubar or toolbar in VS and choose “Customize…”
  2. Click on the Commands tab
  3. Select the “Menu bar” radio button and then select “Menu Bar” in the dropdown
  4. Scroll down and select the location where you’d like to insert your new menu, and click on “Add New Menu”
    • You’ll get a new menu option called “New Menu”
  5. Click on Modify Selection and change the Name to something like “Settings”
  6. Up where “Menu Bar” is selected, choose “Settings” in the dropdown
    • I find this rather unintuitive, but you get used to it
  7. Now, for each of the macros that you created, you’ll run the following steps:
    1. Click on “Add Command…”
    2. Select “Macros” as the category on the left
    3. Select the macro as the command on the right
    4. Click OK
    5. Click on Modify Selection and change the name to something concise
  8. When finished, click OK

Here’s what I ended up with:

My Settings Menu

Now, with 2 clicks, you can quickly switch your Visual Studio theme from black to white or to presentation mode.  You should set up different macros for any subset of settings that you use occasionally.  No longer stress over flipping your machine into presentation mode as you’re scrambling to finish preparations for a demo; just click your menu option and you’re done.