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+

Reformatting Code in Visual Studio

When developing software, it is good practise to use consistent formatting of the underlying source code. On longer projects, particularly when cutting and pasting code, the formatting can slip. Luckily, Visual Studio can reapply it automatically.

Reformat Selection

The reformat selection command is available in Visual Studio 2003 and later. This command reformats the currently selected code. The new format ensures standardised use of indentations and spaces. It also consistently adds and removes lines near the brace characters that delimit code blocks.

The command is executed by highlighting the code to be processed and opening the Edit menu. Within this menu is a submenu named "Advanced" that contains the "Format Selection" command. The command can also be executed via the keyboard by holding the Ctrl key whilst pressing K, then F.

NB: The shortcut keys may differ according to the version and configuration of Visual Studio

In Visual Studio 2003, the formatting is applied in stages so the key combination may need to be repeated several times to complete the process.

Reformat Document

If you are using Visual Studio 2005 or later, a second reformatting command is available. This is named "Reformat Document" and is found in the Advanced section of the Edit menu. This performs the same formatting tasks as the previous command but for the entire open file, rather than just the selected text. The standard shortcut key combination for this action is Ctrl-K, D.

Although the second command is not available in Visual Studio 2003, the effect can be replicated by holding Ctrl whilst pressing A, then K, and finally F. The Ctrl-A combination selects the entire document before applying the formatting to the selection.

9 June 2008