How do I set up VSCode to put curly braces on a new line?
Solution 1
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)
- Go to File\Preferences\Settings
-
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,
Save 'User Settings' and you are done!
Solution 2
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.
Tested with Visual Studio Code 1.30.2
Solution 3
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
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
VSCode>File>Preferences>Settings> <type "brace" (without quotas)>
and uncheck CSharpFixFormat>Style>Braces>On Same Line
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Admin
Updated on July 08, 2022Comments
-
Admin almost 2 years
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?