How do I set up VSCode to put curly braces on a new line?

JavascriptFormattingCode FormattingVisual Studio-Code

Javascript Problem Overview


Let's say I type in the following code and format it.

if (condition) { /* Hello! */ }

If this is C# code, it is formatted like this:

if (condition)
{
    // Hello!
}

If it is JavaScript, VSCode formats it like this:

if (condition) {
    // Hello!
}

So how can I use the first formatting style (curly braces on new lines) for all languages? I can't find a setting or something similar. Suggestions?

Javascript Solutions


Solution 1 - Javascript

Follow the steps below to make Visual Studio Code format opening curly braces on a new line for Java Script and Type Script.

In Visual Studio Code (v1.20.0)

  1. Go to File\Preferences\Settings

  2. Add the following lines in 'User Settings' (in the right side pane)

    "javascript.format.placeOpenBraceOnNewLineForControlBlocks": true, 
    "javascript.format.placeOpenBraceOnNewLineForFunctions": true,
    
    "typescript.format.placeOpenBraceOnNewLineForControlBlocks": true,
    "typescript.format.placeOpenBraceOnNewLineForFunctions": true,
    
  3. Save 'User Settings' and you are done!

Solution 2 - Javascript

Go to File\Preferences\Settings and search for 'brace'.

Enable the settings illustrated below.

This allows me to auto-format code with curly braces on the following line for function definitions and control blocks.

File\Preferences\Settings

Tested with Visual Studio Code 1.30.2

Solution 3 - Javascript

By default VS code don't support customization in formatting. But you could do your format customization using js-beautify extension. You can find the free version on VS code Marketplace (https://marketplace.visualstudio.com/items?itemName=HookyQR.beautify).

For your requirement of curly braces on new line can be setup by creating a '.jsbeautifyrc' config file on your project root folder and define a following line.

{
     "brace_style": "expand"
}

For more formatting options you can find from the following link: https://github.com/HookyQR/VSCodeBeautify/blob/master/Settings.md

Solution 4 - Javascript

Those who need the solution for PHP, you need to install PHP Intelephense Extension and update the settings.json file.

"intelephense.format.braces": "k&r" 

By default it was psr12.

Solution 5 - Javascript

VSCode>File>Preferences>Settings> and uncheck CSharpFixFormat>Style>Braces>On Same Line enter image description here

Solution 6 - Javascript

Just for reference: if it is for Java. File\preferences\settings Extensions\Java\Code Generation: Use Blocks.Java Code Blocks

Solution 7 - Javascript

Add these lines in settings.json file, open it by type ctrl+,

  // Brackets on a new line
  "javascript.format.placeOpenBraceOnNewLineForControlBlocks": true,
  "javascript.format.placeOpenBraceOnNewLineForFunctions": true,
  "typescript.format.placeOpenBraceOnNewLineForControlBlocks": true,
  "typescript.format.placeOpenBraceOnNewLineForFunctions": true,

Or from the settings search for function new line and check the two boxes, open it by type ctrl+shift+p and search for open settings (json)

enter image description here

Solution 8 - Javascript

In 2021 the default behavior seems to be what OP wanted. To get curly braces on same line in c# (vscode 1.63 with omnisharp) you have to create a omnisharp.json file in project root with proper settings as described at https://nosuchstudio.medium.com/formatting-curly-braces-on-the-same-line-in-c-in-vscode-c4937e1c215f . e.g.

{
"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
}

}

Solution 9 - Javascript

C_Cpp: Clang_format_fallback Style

Google

Solution 10 - Javascript

The following instruction apply to VS Pro 2012...

  1. On the menu bar choose Tools.
  2. Choose Options...
  3. Expand the Text Editor list.
  4. Expand the JavaScript list.
  5. Expand the Formatting list.
  6. Choose New Lines.
  7. Choose Place open brace on new line for control blocks.

I hope this is helpful. Feel free to reply if you have any questions.

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
QuestionM. FatihView Question on Stackoverflow
Solution 1 - JavascriptAdvait BaxiView Answer on Stackoverflow
Solution 2 - JavascriptCharley RammView Answer on Stackoverflow
Solution 3 - JavascriptSridharKrithaView Answer on Stackoverflow
Solution 4 - JavascriptRobinView Answer on Stackoverflow
Solution 5 - JavascriptZen Of KursatView Answer on Stackoverflow
Solution 6 - Javascriptuser1546559View Answer on Stackoverflow
Solution 7 - JavascriptAhmad MoghaziView Answer on Stackoverflow
Solution 8 - JavascriptSpikolynnView Answer on Stackoverflow
Solution 9 - Javascripta442509097View Answer on Stackoverflow
Solution 10 - JavascripttooniceView Answer on Stackoverflow