How to make Visual Studio not put { on a new line?

57,490

Solution 1

Go to Tools -> Options -> Text Editor -> C# -> Code Style -> Formatting-> New Lines

Here there are a lot of options. Remove the check on all options here to never put the open bracket on a new line.

EDIT

The section New Line Options for expressions does not relate to placement of code in conjunction with brackets, so those you don't need to touch.

Solution 2

Bear in mind that if you're using Resharper the above procedure won't help, since Resharper overrides the native VS behavior. If that's your case go to Resharper > Options, Code Editing > C# > Formatting Style > Braces Layout, set all top "Braces Layout" options to "At end of line (K&R style)".

Took me a while to figure this out, so I hope it helps someone.

Solution 3

For all the Mac Users out there, this is how I solved it:

Preferences -> Source Code -> Code Formatting -> C# source code -> C# Format -> Edit

There you can change it under the Category: New Lines

image example

Solution 4

You can change that in the options of your Visual Studio. Go to Tools -> Options -> Text Editor -> C# -> formatting -> new lines (or something similar, I only have the german version of visual studio). You can then change where you want to put the brackets into the next line and where you don't. Personally, I removed all the options, but if you want to have a new line e.g. in loops, there are many options to customize this.

Solution 5

Have a look at Tools -> Options then Text Editor -> C# -> Formatting -> New Lines

Share:
57,490

Related videos on Youtube

IAdapter
Author by

IAdapter

Updated on July 08, 2022

Comments

  • IAdapter
    IAdapter almost 2 years

    If I have code like this

    if(true){
    

    and I add

    }
    

    it is transformed into

    if (true)
    {
    }
    

    but I would like it to stay in format

    if (true) {
    }
    

    Even if I copy code that has if like this, it is transformed into the longer version.

    P.S. I understand that it's a C++ and C# standard to use the longer version, however I do come from a different standard, and it's easier for me to use the shorter version.

    I bet there is some formatter option that can be turned off or changed.

  • Reversed Engineer
    Reversed Engineer over 8 years
    +1 thank you. However, when I turn those off, and then deliberately put the brace on the new line, it moves it back to the same line :( Is there a ways to stop it meddling in things I didn't ask it to fiddle with?
  • Fueled By Coffee
    Fueled By Coffee almost 6 years
    What monster would enable new lines by default?
  • Tom Duckering
    Tom Duckering about 5 years
    I'm using VS2019 preview, and I had to make the change and then exit Visual Studio completely and reopen it in order for the change to stick. If I just changed it then tried to reformat the document, it would just reset to the previous settings.
  • Riccardo
    Riccardo about 5 years
    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 Google
  • SacWebDeveloper
    SacWebDeveloper over 4 years
    Thank you! This is one of the most rage inducing features of Visual Studio for Unity.
  • kamgman
    kamgman about 2 years
    To be clear, the options that effect the if statement are... "Place brace on a new line for CONTROL BLOCKS" and "Place else on a new line"