Visual Studio 2005/2012: How to keep first curly brace on same line?

Visual Studio

Visual Studio Problem Overview


Trying to get my css / C# functions to look like this:

body {
    color:#222;
}

instead of this:

body 
{
    color:#222;
}

when I auto-format the code.

Visual Studio Solutions


Solution 1 - Visual Studio

C#

  1. In the Tools Menu click Options
  2. Click Show all Parameters (checkbox at the bottom left) (Show all settings in VS 2010)
  3. Text Editor
  4. C#
  5. Formatting
  6. New lines

And there check when you want new lines with brackets

Css:

almost the same, but fewer options

  1. In the Tools Menu click Options
  2. Click Show all Parameters (checkbox at the bottom left) (Show all settings in VS 2010)
  3. Text Editor
  4. CSS
  5. Format

And than you select the formatting you want (in your case second radio button)

For Visual Studio 2015:

Tools → Options

In the sidebar, go to Text Editor → C# → Formatting → New Lines

and uncheck every checkbox in the section "New line options for braces"

enter image description here

For Mac OS users:
Preferences → Source Code → Code Formatting → choose what ever you want to change (like C# source code) → C# Format → Edit -→ New Lines

Solution 2 - Visual Studio

Tools -> Options -> Text Editor -> C# -> Formatting -> New Lines -> New Line Options for braces -> Uncheck all boxes.

Solution 3 - Visual Studio

The official MS guidelines (at the time in 2008) tells you to have the curly brace on the same line as the method/property/class and many other things which are not enforced in Visual Studio.

You can change all these auto-text settings under:
Tools -> Options -> Text Editor -> [The language you want to change]

UPDATE: This was based on the book "Framework Design Guidelines" written by some of the core-people from the .NET-team. If you look at the source-code for the likes of ASP.NET MVC, this is no longer accurate.

Solution 4 - Visual Studio

Go to Tools -> Options -> Text Editor -> CSS -> Formatting. Click "Semi-expanded," which matches the style you defined.

Options screen

Solution 5 - Visual Studio

For CSS you'll need the 'Semi Expanded' option.

Solution 6 - Visual Studio

For Visual Studio Mac OS (Community edition) version 8.3 you need to do the following:

Preferences -> Source Code (in left menu) -> Code Formatting -> C# source code -> C# Format -> Press Edit

enter image description here

Select New Lines from the Category dropdown menu:

New Lines Category

Deselect each option in the New line options for braces:

Solution 7 - Visual Studio

There is a specific formatting setting in VS 2008/2010 to keep the open brace on the same line:

Click Tools->Options
Select 'CSS' within 'Text Editor' tree node
Select 'Formatting' under 'CSS' node
Click 'Semi-expanded' radio button

You will see a preview what the various radio buttons avail will do to the formatting

Solution 8 - Visual Studio

If you're looking for this option within Visual Studio 2014, then it's under advanced and is now a 'Brace positions' drop down box:

enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionShawnView Question on Stackoverflow
Solution 1 - Visual StudioStormenetView Answer on Stackoverflow
Solution 2 - Visual StudioFlySwatView Answer on Stackoverflow
Solution 3 - Visual StudioSeb NilssonView Answer on Stackoverflow
Solution 4 - Visual StudioGabriel NahmiasView Answer on Stackoverflow
Solution 5 - Visual StudioHuppieView Answer on Stackoverflow
Solution 6 - Visual StudioReitenatorView Answer on Stackoverflow
Solution 7 - Visual StudiostoofyView Answer on Stackoverflow
Solution 8 - Visual StudioLukeView Answer on Stackoverflow