Search code examples
c#xmlvisual-studio-2010visual-studio

Tips for making spaces work like tabs in Visual Studio


At work we have the convention on using four spaces for code indentation. I'm accustomed to using tabs for indentation, but want to follow the convention.

Note: it is not my intention to start a discussion on spaces vs tabs here.

I adjusted my Visual Studio settings to replaces tabs with four spaces, but I have some issues adjusting to using spaces.

For example:

  • How can I easily un-indent code? with tab characters, I only needed to use backspace one time, with spaces I need to use backspace four times.

  • How can I make sure that there is always the correct amount of spaces (not three or five)?

  • How can I navigate through my code as fast as I could with tabs? (arrow left or right jumps to the next indentation with tabs, but moves only a single position with spaces)

  • How can I ignore whitespace changes when comparing files?

Ideally, I would like these four spaces for indentation to work equally to tab characters.

I work mainly with C# and XML-based files.


Solution

  • To un-indent, highlight the line and hit Shift+Tab. Or just position to the beginning of the line and hit Shift+Tab.

    Also, hitting Tab will enter the correct number of spaces to align at the next 4-space boundary.

    To make sure indentation is correct, you can highlight an area of code and select Edit -> Advanced -> Format Selection, or you can just go to the end of a block, remove the ending brace, and add it back. The IDE will reformat your code.

    So, if you have this:

    void foo()
    {
      f();
         int q = 32;
       for (; q > 0; --q)
         {
        // really messed up indentation
       }
      }
    

    Then deleting and re-adding that final '}' will reformat the entire method.