views:

666

answers:

3

Some of our projects call for default Visual Studio tab options (width 4; keep tabs); some call for width 3; use spaces. Don't ask.

Rather than set these globally, is there anyway in which I could set this on a per-solution or per-project or even (emacs-style) per-file?

Visual Studio 2005 and 2008.

+1  A: 

I have a similar problem: my new project needs to be set up with keep tabs, while my other projects are developed with insert spaces option.

Since this is a strictly Visual Studio setting, i didn't expect to find any per-project information that will empower this (either if one use a specific add-in for that purpose).

So i ended up having eclipse-like setup: having two shortcuts to Visual Studio with different settings each.

According to MSDN, one can use /ResetSettings switch to change the Visual Studio settings upon startup. What you need now is two shortcuts with this format:

devenv.exe /ResetSettings "d:\your-settings.vssettings"

The starting time is like 5-10 seconds longer (since it applies the change settings on every startup) but it's more convenient and less cumbersome than doing it manually, every time.

HTH

ljubomir
+2  A: 

The most convenient solution I know is to create a set of Visual Studio macros to switch to the settings you want.

Go to Tools > Macros > Macros IDE. There, in the tree on the left, right-click MyMacros and choose Add > Add Module. Give the module a name such as TabSize. Within this module, create subs to change the settings you want. For instance:

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics

Public Module TabSize

    Sub Tab3()
        SetTabAndIndentation(3)
    End Sub

    Sub Tab4()
        SetTabAndIndentation(4)
    End Sub

    Function SetTabAndIndentation(ByVal value As Integer)
        DTE.Properties("TextEditor", "AllLanguages").Item("TabSize").Value = value
        DTE.Properties("TextEditor", "AllLanguages").Item("IndentSize").Value = value
    End Function

End Module

There is no useful documentation I know of for the string parameters. If you need to set other options, such as "Keep Tabs", the easiest approach is to make these changes manually (unter Tools > Options). Then, using Tools > Import and Export Settings, save these settings as a vssettings file. This creates an XML file whose structure is the same as that needed for the method calls.

Finally, you can link these macros to command buttons or keyboard shortcuts via Tools > Customize. Giving each macro a keyboard shortcut allows you to quickly toggle between settings.

Daniel Wolf
This could be useful, combined with handling project load events and looking in the project for a marker property or file to control the settings. Hmmm...
Roger Lipscombe
Ah, that "export settings" tip is great! Now if only I could get all my coworkers to auto-import it... :)
Protector one
A: 

You can also use these properties to complete Daniel's example :

DTE.Properties("TextEditor", "AllLanguages").Item("InsertTabs").Value
DTE.Properties("TextEditor", "AllLanguages").Item("IndentStyle").Value
Jonathan Schmidt