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

8

150

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.

Strickman answered 2/9, 2008 at 13:52 Comment(5)
Why you would want to do this? You're swimming upstream, MS guidelines for .NET style are the current auto-format style.Gradeigh
@AnthonyMastrean: I don't think there are any MS guidelines for any things. Maybe, this guy is working mostly on java and they are used to this name convention. And at least, css is not something .net related :)Veasey
I tried to use this format but gave up fighting VS. If someone else looks at the code in their version then at least its all standard.Ethnic
Do not misuse the word standard. Not everyone wants braces in the same fashion.Sailesh
bit late to the party but its much easier for me to read my code when the braces are on the same row. Also you can fit more code on the same screenspace, win win for me :/Celesta
R
268

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

Rhettrhetta answered 2/9, 2008 at 13:55 Comment(3)
For css in Visual Studio 2013 you can change the value to something else, other than the default, in Tools > Options > Text Editor > Css > Advanced > Brace positionsJunk
For Visual Studio Code go File -> Preferences -> Settings and under Extensions choose the language you want to change properties. Search for Clang_format_fallback Style (e.g.: for C++ C_Cpp: Clang_format_fallback Style) and change the value from Visual Studio to GoogleMagdalenamagdalene
For Visual Studio 2017 : Tools > Options > Text Editor > C# > Code Style > Formatting > New LinesPita
S
31

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

Stromboli answered 2/9, 2008 at 13:55 Comment(0)
S
11

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.

Schaub answered 2/9, 2008 at 14:1 Comment(1)
Could you please provide a reference for this @Seb? I found a MS Guidelines document from 2005 that states the opposite convention for curly braces (next-line style): blogs.msdn.com/b/brada/archive/2005/01/26/361363.aspx. I myself prefer the "egyptian" style (same-line opening brace), and was hoping that C# followed this convention...Tyrannosaur
A
6

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

Options screen

Arelus answered 9/7, 2013 at 20:39 Comment(1)
This option looks to have disappeared in 2013 :(Glyptic
M
4

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

Macon answered 2/9, 2008 at 13:57 Comment(0)
P
4

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:

Palter answered 25/11, 2019 at 20:58 Comment(0)
D
2

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

Dotted answered 12/12, 2010 at 7:31 Comment(0)
G
1

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

Glyptic answered 10/12, 2014 at 11:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.