Tab Or Space Visual Studio
The culture wars continue. The country is divided with no end in sight. Tabs or spaces? There's even an insane (IMHO) assertion that the spaces people make more money.
Jul 24, 2010 Indentation in Visual Studio is controlled with the use of the tab key, which can be used to insert tab characters or spaces into the code or to indent or unindent selected lines. The type of indentation used for a language can have varied effects. Visual Studio 2012 and newer, C/C. Specify additional characters that accept entries in the options dialog of Visual Studio. Add a space to the default list to make Visual Assist behave as it does in older IDEs.
I'm going with Gina Trapani on this one. I choose working code.
@ftrain I choose the 3rd option: working code
— Gina Trapani ️ (@ginatrapani) December 27, 2015Teams can fight but the problem of formatting code across teams is solved by EditorConfig. I'm surprised more people don't know about it and use it, so this blog post is my small way of getting the word out. TELL THE PEOPLE.
Take a project and make a new .editorconfig file and put this in it. I'll use a dotnet new console example hello world app.
I've set mine in this example to just *.cs, but you could also say [*.{cs,js}] or just [*] if you like, as well as have multiple sections.
You'll check this file in WITH your project so that everyone on the team shares the team's values.
Here in Notepad2 we can see someone has used spaces for whitespace, like a savage. Whitespace appears as pale dots in this editor.
I'll open this project in Visual Studio 2017 which supports the EditorConfig file natively. Notice the warning at the bottom where VS lets me know that this project has conventions that are different than my own.
VS Format Document commands will use tabs rather than spaces for this project. Here is the same doc reformatted in VS:
At this point I'm comforted that the spaces have been defeated and that cooler heads have prevailed - at least for this project.
.NET Extensions to EditorConfig
Even better, if your editor supports it, you can include 'EditorConfig Extensions' for specific files or languages. This way your team can keep things consistent across projects. If you're familiar with FxCop and StyleCop, this is like those.
There's a ton of great .NET EditorConfig options you can set to ensure the team uses consistent Language Conventions, Naming Conventions, and Formatting Rules.
- Language Conventions are rules pertaining to the C# or Visual Basic language, for example,
var
/explicit type, use expression-bodied member. - Formatting Rules are rules regarding the layout and structure of your code in order to make it easier to read, for example, Allman braces, spaces in control blocks.
- Naming Conventions are rules respecting the way objects are named, for example,
async
methods must end in 'Async'.
You can also set the importance of these rules with things like 'suggestion,' or 'warning,' or even 'error.'
As an example, I'll set that my team wants predefined types for locals:
dotnet_style_predefined_type_for_locals_parameters_members = true:error
Visual Studio here puts up a lightbulb and the suggested fix because my team would rather I use 'string' than the full 'System.String.
The excellent editorconfig for .NET docs have a LOT of great options you can use or ignore. Here's just a FEW (controversial) examples:
- csharp_new_line_before_open_brace - Do we put open braces at the end of a line, or on their own new line?
- csharp_new_line_before_members_in_object_initializers - Do we allow A = 3, B = 4, for insist on a new line for each?
- csharp_indent_case_contents - Do we freakishly line up all our switch/case statements, or do we indent each case like the creator intended?
- You can even decide on how you Want To Case Things And Oddly Do Sentence Case:
pascal_case
,camel_case
,first_word_upper
,all_upper
,all_lower
If you're using Visual Studios 2010, 2012, 2013, or 2015, fear not. There's at least a basic EditorConfig free extension for you that enforces the basic rules. There is also an extension for Visual Studio Code to support EditorConfig files that takes just seconds to install although I don't see a C# one for now, just one for whitespace.
Sponsor: Check out JetBrains Rider: a new cross-platform .NET IDE. Edit, refactor, test and debug ASP.NET, .NET Framework, .NET Core, Xamarin or Unity applications. Learn more and download a 30-day trial!
About Scott
Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.
AboutNewsletter
Note
VS Code have option to convert tabs to spaces by default in 0.10.10 (see official docs).So, this extention is not much help.
Visual Studio: Show Whitespace And Configure Spaces Instead ...
Description
TabSpacer is an extension for Visual Studio Code (VS Code).
It have 2 features, toggle the insertSpaces
option and convert existing tabs to spaces.
The extension is registered on the Visual Studio Marketplace.
Usage
Toggle insertSpaces option
Press the F1
key and type toggle insertSpaces option
or use the Ctrl+Shift+Z
key binding.
This command toggles the insertSpaces option:
- If
insertSpaces
istrue
, then its value is set tofalse
. - If
insertSpaces
isfalse
, then its value is set totrue
.
Note: The tabSize
option is unaffected.
Convert tabs to space
Press the F1
key and type convert tabs to space
or use the Ctrl+Shift+T
key binding.
This command convert all tabs to spaces.
Actual tab size is read from the tabSize
VS Code option.
Install
Press Ctrl+P
and type ext install TabSpacer
.
You can also copy its files into your local extensions folder:
- Windows:
%USERPROFILE%.vscodeextensions
- Linux or Mac:
$HOME/.vscode/extensions
Changelog
Version 1.0.2
Change README.
Version 1.0.1
The extension no more displays Congratulations, your extension 'TabConverter' is now active!
when activated.
Visual Studio 2017 Tabs Spaces
Version 1.0.0
TabSpacer can now convert spaces to tabs by respecting the insertSpace
VS Code option.
You can use this feature by either pressing the F1
key and typing convert spaces to tab
, or using the Ctrl+Shift+S
key binding.