11

I want VS Code to put curly braces on a new line in C# and C++

How it works now How it works now

How it should look How it should look

Tried C# FixFormat extension, but it works only after I push CTRL+K+F but I want VS Code to make curly braces on new lines while I'm coding, without additional steps like hotkeys and such

Stefan
  • 17,448
  • 11
  • 60
  • 79
Mikhail Alexeev
  • 143
  • 1
  • 1
  • 6

7 Answers7

8

Now that C#FixFormat has been removed, try putting an omnisharp.json file in the root of your project with the following.

{
    "FormattingOptions": {
        "NewLinesForBracesInLambdaExpressionBody": false,
        "NewLinesForBracesInAnonymousMethods": false,
        "NewLinesForBracesInAnonymousTypes": false,
        "NewLinesForBracesInControlBlocks": false,
        "NewLinesForBracesInTypes": false,
        "NewLinesForBracesInMethods": false,
        "NewLinesForBracesInProperties": false,
        "NewLinesForBracesInObjectCollectionArrayInitializers": false,
        "NewLinesForBracesInAccessors": false,
        "NewLineForElse": false,
        "NewLineForCatch": false,
        "NewLineForFinally": false
    }
}
thornebrandt
  • 109
  • 2
  • 5
  • Note that as of Omnisharp 1.39.2 (in VS Code C# extension 1.25.1) an `omnisharp.json` file may not be honored unless you go to the VS Code settings and set `"omnisharp.enableEditorConfigSupport"` to false. Either that, or use a `.editorConfig` file instead. https://github.com/OmniSharp/omnisharp-vscode/issues/5446#issuecomment-1308655891 – fenix.shadow Dec 21 '22 at 00:07
3

In my case, I was using the C# extension by Microsoft (3.5 stars). By default, this extension uses its default C# formatter. Disable the formatting option as shown below, and you won't get any for formatting, including the annoying NewLinesForBracesInMethods. Alternatively you can try to tweak the C# extension formatter. More info here.

So I replaced it with the C# FixFormat (5 stars) extension. This seems to work straight of the box for me.

But then I realised, I wasn't getting autocomplete, so I reinstalled the C# extension by microsoft, and kept the FixFormat extension. And it works great, no new lines for braces.

Ben Butterworth
  • 22,056
  • 10
  • 114
  • 167
0

On VSCode, Go to File\Preferences\Settings and search for 'curly'. Enable all four typescripts as given in the image.

CurlyBraces

  • 2
    Tried this already, seems like it doesn't affect C# and C++ formatting – Mikhail Alexeev Jul 08 '19 at 12:14
  • Didnt change for me either, one of the many things that annoy me about vs code is that somethings seem frustrating to get the way you want.. It could be yet another extension is interfering with it. – BugFinder Jul 08 '19 at 13:26
  • @BugFinder it works fine for me but you are right, it could be an interference with another extension. –  Jul 08 '19 at 13:41
  • I tried this settings with all extensions except C# disabled, but still braces are on the same line – Mikhail Alexeev Jul 08 '19 at 13:45
  • 1
    I just got it to work, but not quite the way I wanted. I added the extension "c# fixformat" which doesnt change the default behavior of code completion, or at least not yet anyway, but if you say format document it will let you pick that and then it will put the { on the next line.. but what a polava – BugFinder Jul 08 '19 at 14:07
  • I just removed it and installed visual studio community version. I hate to say that Visual studio code is not good – Saad Anees Jul 09 '19 at 07:28
0

enter image description here

From Preferences/Settings, choose c# FixFormat and uncheck the checkbox (Braces: On Same Line).

Nalan Madheswaran
  • 10,136
  • 1
  • 57
  • 42
0

If you want to add this globally you could do the following:

  1. open the directory: $HOME/.omnisharp (or create the directory if it does not already exist)
  2. create a file in said directory called: omnisharp.json
  3. paste the content from @thornebrandt's answer and save the file
  4. go into VS Code and format the code with alt + shift + f
{
    "FormattingOptions": {
        "NewLinesForBracesInLambdaExpressionBody": false,
        "NewLinesForBracesInAnonymousMethods": false,
        "NewLinesForBracesInAnonymousTypes": false,
        "NewLinesForBracesInControlBlocks": false,
        "NewLinesForBracesInTypes": false,
        "NewLinesForBracesInMethods": false,
        "NewLinesForBracesInProperties": false,
        "NewLinesForBracesInObjectCollectionArrayInitializers": false,
        "NewLinesForBracesInAccessors": false,
        "NewLineForElse": false,
        "NewLineForCatch": false,
        "NewLineForFinally": false
    }
}
Reitenator
  • 1,075
  • 13
  • 13
0

Finish your code as it is, then Shift+Alt+F (or select from the menu by the right click) will auto-format everything. No need for anything else :-)

enter image description here

Phoenix
  • 11
  • 3
0

As of 2023, the C# Curly Formatter extension seems to work for me!

Darren Griffith
  • 3,290
  • 3
  • 28
  • 35
Saharsh
  • 39
  • 1
  • 12
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/34506189) – Jan Schultke Jun 08 '23 at 21:37