18
votes

At work we have the convention on using 4 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 4 spaces, but I have some issues adjusting to using spaces.

For example:

  • How can I easily un-indent code? with tab chararaters, I only needed to use backspace one time, with spaces I need to use backspace 4 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?

Idealy, I would like these 4-spaces for indentation to work equally to tab characters.

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

Any tips are welcome!

2
Unindent code with Shift-Tab at the start of a line. Use "Reformat Document" to correct indentation, or make VS reformat a block when it's closed, or use a plugin that shows you indentation guides in the editor that will make it easier to spot when things don't align to them.millimoose
I'd also suggest pouring over the Options dialog to see how much of this is configurable or not, might be some of what I mentioned has to be enabled.millimoose
Ctrl + Arrow left key/Arrow right Key will take you to the next non white space character. That's faster than just arrow left/right!default
There really needs to be an add-in that just converts spaces to tabs on open, and then back to spaces on save (or even on download/check-in).BrainSlugs83
Also, [Home] toggles between the beginning of a line and the first non whitespace character. This is usually faster than navigating with the cursor keys.IInspectable

2 Answers

13
votes

Such a shame that Visual Studio does not implement smart unindent. You can find this feature it even in the most simple free notepad-like editors.

Fortunately, to simplify auto-unindent with Backspace and Delete, there is an awesome plugin, TabSanity by jedmao (Jed Hunsaker).

This package causes the Visual Studio text editor to treat tabs-as-spaces as if they were actually tabs. That is, the backspace and delete keys, arrow key navigation and mouse cursor text selection (coming soon) will not allow the caret to land within the spaces that form a tab.

Note that it requires as dependency another cool plugin, EditorConfig .

Both are highly recommended for "tabs-as-spaces" text maniacs.

Happy coding!

11
votes

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.