Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tips for making spaces work like tabs in Visual Studio

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!

like image 463
oɔɯǝɹ Avatar asked May 28 '13 21:05

oɔɯǝɹ


People also ask

How many spaces is a tab in Visual Studio?

Sets the distance in spaces between tab stops. The default is four spaces.

Which is better tabs or spaces?

Conclusion. So, at the end of the day, tabs versus spaces is truly a matter of preference, however the tab is still the character specifically designed for indentation, and using one tab character per indentation level instead of 2 or 4 spaces will use less disk space / memory / compiler resources and the like.


2 Answers

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!

like image 84
Ivan Aksamentov - Drop Avatar answered Sep 19 '22 20:09

Ivan Aksamentov - Drop


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.

like image 21
Jim Mischel Avatar answered Sep 21 '22 20:09

Jim Mischel