Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1107459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:56:23+00:00 2026-05-17T01:56:23+00:00

Visual Studio keeps trying to indent the code inside namespaces. For example: namespace Foo

  • 0

Visual Studio keeps trying to indent the code inside namespaces.

For example:

namespace Foo
{
   void Bar();

   void Bar()
   {

   }

}

Now, if I un-indent it manually then it stays that way. But unfortunately if I add something right before void Bar(); – such as a comment – VS will keep trying to indent it.

This is so annoying that basically because of this only reason I almost never use namespaces in C++. I can’t understand why it tries to indent them (what’s the point in indenting 1 or even 5 tabs the whole file?), or how to make it stop.

Is there a way to stop this behavior? A config option, an add-in, a registry setting, hell even a hack that modifies devenv.exe directly.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T01:56:24+00:00Added an answer on May 17, 2026 at 1:56 am

    Here is a macro that could help you. It will remove indentation if it detects that you are currently creating a namespace. It is not perfect but seems to work so far.

    Public Sub aftekeypress(ByVal key As String, ByVal sel As TextSelection, ByVal completion As Boolean) _
            Handles TextDocumentKeyPressEvents.AfterKeyPress
        If (Not completion And key = vbCr) Then
            'Only perform this if we are using smart indent
            If DTE.Properties("TextEditor", "C/C++").Item("IndentStyle").Value = 2 Then
                Dim textDocument As TextDocument = DTE.ActiveDocument.Object("TextDocument")
                Dim startPoint As EditPoint = sel.ActivePoint.CreateEditPoint()
                Dim matchPoint As EditPoint = sel.ActivePoint.CreateEditPoint()
                Dim findOptions As Integer = vsFindOptions.vsFindOptionsMatchCase + vsFindOptions.vsFindOptionsMatchWholeWord + vsFindOptions.vsFindOptionsBackwards
                If startPoint.FindPattern("namespace", findOptions, matchPoint) Then
                    Dim lines = matchPoint.GetLines(matchPoint.Line, sel.ActivePoint.Line)
                    ' Make sure we are still in the namespace {} but nothing has been typed
                    If System.Text.RegularExpressions.Regex.IsMatch(lines, "^[\s]*(namespace[\s\w]+)?[\s\{]+$") Then
                        sel.Unindent()
                    End If
                End If
            End If
        End If
    End Sub
    

    Since it is running all the time, you need to make sure you are installing the macro inside in your EnvironmentEvents project item inside MyMacros. You can only access this module in the Macro Explorer (Tools->Macros->Macro Explorer).

    One note, it does not currently support “packed” namespaces such as

    namespace A { namespace B {
    ...
    }
    }
    

    EDIT

    To support “packed” namespaces such as the example above and/or support comments after the namespace, such as namespace A { /* Example */, you can try to use the following line instead:

     If System.Text.RegularExpressions.Regex.IsMatch(lines, "^[\s]*(namespace.+)?[\s\{]+$") Then
    

    I haven’t had the chance to test it a lot yet, but it seems to be working.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to Visual Studio, and I'm just trying to write code as efficiently
I am trying to build an ASP.NET project, but Visual Studio keeps giving me
In Visual Studio, you can expand and collapse code without using regions, for example
I'm trying to keep Visual Studio from attempting to serialize the datasource of drop
when i try to delete the entity Visual Studio keeps crashing on the line
Visual Studio 2010, I was able to view xaml in designer. Now every xaml
Visual Studio 2010 C# code: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using
Visual studio seems intent that my javascript code is bad, mostly because it has
I am trying to find out in Visual Studio 2010 Unit Testing how to
I'm trying to install service which I created in visual studio 2008 on windows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.