This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Visual Studio
VS 2003+

Visual Studio Tab Settings

Indentation is essential to ensure that source code is easy to read. Visual Studio provides several configuration options that control the insertion of tab and space characters. This article describes the available options and their usage.

Tab Settings

Sensible use of indentation in source code can greatly improve the readability of the code. 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. In some languages white space is significant and can change the meaning of the code if used incorrectly. In other languages tabs and spaces can be used freely to align code for readability. However, as the width of a tab can vary according to the user's preferences, indentation may appear differently when code is viewed by two developers. You may elect to use only spaces to remove this problem but this can unnecessarily increase the file size, especially with languages that use lots of indentation, such as XML.

Visual Studio helps to address these problems by allowing you to configure the manner in which text is indented. You can use different settings for each of the supported languages. To access the tab settings, open the Options dialog box by selecting Options from the Tools menu. Expand the Text Editor section in the tree and then the language for which you wish to change settings. Finally, select the Tabs branch. The image below shows the settings for the C# programming language in Visual Studio 2005. Other languages and Visual Studio versions will appear similar.

Visual Tab Settings

Indenting Settings

The first group of tab settings allow you to change the way that indentation is added automatically when you press Enter within a code editor. Three options are available:

  • None. If you select None, automatic indentation is disabled for the selected language. When you press the Enter key, the cursor is moved to the first character position of the next line.
  • Block. With the Block option selected, Visual Studio provides simple indenting. When Enter is pressed, the cursor moves to the next line and uses the same initial indentation as the line it just left.
  • Smart. The Smart setting instructs Visual Studio to apply indentation according to the code on the previous line. In most cases this is similar to block indentation. However, if the previous line started a new code block, closed an existing block or was a code structure such as a loop or a conditional statement, the indentation is modified to highlight that structure. The actual indentation changes according to the language in use and its typical coding conventions.

Tab Settings

Indentation is achieved in text files by adding tab or space characters. Each has its advantages and drawbacks as described earlier. The tab section of the tab settings allows you to modify the behaviour of the tab key and automatic indentation.

The first two settings allow you to specify the standard size of tabs and indents. The values provided specifies the number of spaces that will be used in each case. The radio buttons allow you to specify whether pressing the tab key inserts only spaces or that the tab character should be retained if possible. When keeping tabs, Visual Studio may still insert some spaces to achieve the correct alignments.

24 July 2010