[ACCEPTED]-Visual Studio, per solution indentation settings-indentation

Accepted answer
Score: 98

UPDATE: VS 2017 supports EditorConfig natively: https://blogs.msdn.microsoft.com/dotnet/2016/12/15/code-style-configuration-in-the-vs2017-rc-update/

In 4 VS 2010 and above, there's an extension 3 that sets the indentation based on .editorconfig file 2 in the solution/project root:


There's also 1 similar extension for Visual Studio Code.

Score: 20

Here is one (admittedly hacky) way to achieve 8 what you are looking for:

1) create a macro 7 that changes the indentation (source)

 Sub Set-Indent(indent As integer)
     Dim props As EnvDTE.Properties = DTE.Properties("TextEditor", "C/C++")
     Dim ts As EnvDTE.Property = props.Item("TabSize")
     Dim ins As EnvDTE.Property = props.Item("IndentSize")
     ts.Value = indent 
     ins.Value = indent 
 End Sub

2) Hook that 6 up with your solution loading: In the macro 5 explorer, choose EnvironmentEvents, select SolutionEvents in the first 4 drop-down, Opened in the second. You now have 3 a macro that will trigger every time you 2 open a solution. You just need to map your 1 solutions to the required indentation.

Score: 7

There's also another Add-in called "Rebracer" now... link 1 is here: http://visualstudiogallery.msdn.microsoft.com/410e9b9f-65f3-4495-b68e-15567e543c58

Score: 4

From the VS extension EditorConfig (http://visualstudiogallery.msdn.microsoft.com/c8bccfe2-650c-4b42-bc5c-845e21f96328) website:

The 11 EditorConfig project defines a simple file 10 format for configuring common text-editor 9 options such as indentation sizes. These configuration files are 8 designed to sit alongside a project's source code, allowing 7 text editors to use the right options on 6 a file-by-file basis. The EditorConfig 5 project provides plugins for many common 4 text editors, making the format fully 3 cross-platform.

(emphasis mine)

Full Disclosure: I have not 2 been able to personally test it (because 1 ReSharper takes over).

Score: 2

VS2017 RC added support for .editorconfig 1 format.

Score: 0

You can set the Insert Tabs/Spaces setting 1 with props.Item("InsertTabs") = bool

Check this out: https://github.com/jamesfoster/VS-Whitespace-Macros

Score: 0

UPDATE: seems like indentation is not managed by 14 Resharper, so this answer does not actually 13 address the specific question. However, it 12 does apply in general terms; i.e. "Visual Studio, per solution settings" in case 11 anyone stumbles here looking for that.

If 10 you are using Resharper, you can save formatting 9 settings (really, any settings) for just 8 the solution. You can further specify if 7 just for you (i.e. not committed to source-control) or 6 shared solution-specific settings (i.e. commit 5 to source-control).

Resharper > Options > Code Editing > C# (or whatever language) > Formatting Style > Braces Layout

Then at the bottom of 4 the window, under Save To choose "Solution 3 XYZ personal" (or shared, depending 2 on your purpose).

This creates an xml file 1 YOURSOLUTION.sln.DotSettings.user with values like:

<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/ANONYMOUS_METHOD_DECLARATION_BRACES/@EntryValue">NEXT_LINE</s:String>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/CASE_BLOCK_BRACES/@EntryValue">NEXT_LINE</s:String>

Resharper Settings - Save for Solution Only

Score: 0

So, the accepted solution says "You 23 just need to map your solutions to the required 22 indentation", which is great, except 21 how do you do that? After wasting a lot 20 of time trying to figure that out, I found 19 a method I prefer here. This method loads any 18 number of exported settings that can be 17 different for every solution. Settings 16 can include anything, from indents to colors 15 or even window layout (I think).

  • In Visual Studio 2008, open Tools > Macros > Macro explorer
  • Double click MyMacros > Module1 (if you don't have Module1, right click MyMacros and choose 'New module...' to create it).
  • In the macro explorer window, double click 'EnvironmentEvents' on the left.
  • In the upper-left dropdown, choose 'SolutionEvents'.
  • In the upper-right dropdown, choose 'Opened'.
  • Code to 14 handle the SolutionEvents.Opened event is 13 automatically added. Change that code to 12 the following:

    Private Sub SolutionEvents_Opened() Handles SolutionEvents.Opened
        Dim item As ProjectItem = DTE.Solution.FindProjectItem("solution.vssettings")
        If Not item Is Nothing Then
            'MsgBox("Uncomment this to see a message when settings are loaded")
            Dim name = item.FileNames(1)
            DTE.ExecuteCommand("Tools.ImportandExportSettings", "/import:""" & name & """")
        End If
    End Sub
  • Save the macro.

  • Use Tools > Options to change your UI to have the tab settings and whatever else you want set.
  • Tools > Import and Export Settings... > Export selected environment settings > Next
  • To just export tab settings, uncheck 'All Settings' then check Options > Text Editor > C/C++
  • Click Next, then type 'solution.vssettings' as 'What do you want to name your settings file?' and save the file to wherever you have a solution you want to use those settings.
  • Drag solution.vssettings to any location in the Solution Explorer window.
  • Quit Visual Studio and next time you open a solution containing solution.vssettings, it should load those settings.

If the macro 11 doesn't run, it could be because of a MS 10 security patch. Based on this, add the following 9 string:

<AllowDComReflection enabled="true"/>

Below the <runtime> tag in 8 the following three files:

"C:\Program Files (x86)\Common Files\Microsoft Shared\VSA\9.0\VsaEnv\vsmsvr.exe.config"
"C:\Program Files (x86)\Common Files\Microsoft Shared\VSA\9.0\VsaEnv\vsaenv.exe.config"
"C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\devenv.exe.config"

Be sure to start 7 the editor you use to edit those files with 6 Administrator privileges (ie right click 5 Notepad and choose 'Run as Administrator').

I 4 also found a plug-in that's supposed to do the same 3 thing as the macro suggested above but for 2 some reason it didn't do anything after 1 I ran its installer.

Score: 0

clang-format provides quite sophisticated 5 options for formatting your source code.

The 4 Visual Studio plugin is quite rudimentary, i.e. it 3 does not run automatically, but it does 2 its job well (when manually invoked, that 1 is).


More Related questions