Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set up VSCode to put curly braces on a new line in C# and C++ while typing?

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

like image 982
Mikhail Alexeev Avatar asked Jul 08 '19 11:07

Mikhail Alexeev


2 Answers

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
    }
}
like image 81
thornebrandt Avatar answered Oct 09 '22 13:10

thornebrandt


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.

like image 35
Ben Butterworth Avatar answered Oct 09 '22 12:10

Ben Butterworth