30

I have enable the "editor.formatOnSave" setting but in php files when the code is formate, all the open brackets are going to a new line. i just want them to stay in the same line ex

function test{

}

not

function test
{

}

I am looking for a solution all day but i cant find anything

6

4 Answers 4

58

If you want to have your php code formatted like this

function test {

}

then use intelephense with the following settings.

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

Edit

As @Pinonirvana said in his answer, you can now do this through the GUI.
You'll find this information under du user settings:

Short key -> Ctrl+,

or

File > Preferences > Settings

enter image description here

5
  • 4
    Can't believe how long it took me to get to this solution. Thanks so much!!!
    – Rahi
    Aug 31, 2020 at 20:46
  • 3
    Thanks for this, this option was introduced in vscode-intelephense in 2020 ( github.com/bmewburn/vscode-intelephense/issues/730 ) and is simpler than Ovsyanka's solution so it should be the new accepted answer!
    – Edu Wass
    Sep 5, 2020 at 6:39
  • 1
    The screenshot at stackoverflow.com/a/66115402/470749 helped.
    – Ryan
    Aug 26, 2021 at 16:10
  • even slightly quicker: menu File > Preferences > Settings and type "intelephense format braces" in the "Search settings" input to get straight to the "Intelephense > Format: Braces" setting
    – Kai Carver
    Aug 17, 2022 at 12:15
  • You saved someone yay! Oct 21, 2022 at 17:47
10

The new versions of VScode include this feature already.

You just need to search for "intelephense" in the VScode settings, and look for a setting called "Braces".

VScode Intelephense braces setting

3
  • It's so nice! Easy switch!
    – Rocky Kev
    Feb 20, 2021 at 19:10
  • this still doesn't work for me :-( ... I have VSCode 1.62.3 and I have changed the format of the braces to k&r, yet the brace still jumps to the next line when I make a class. Any idea what I am doing wrong?
    – Jon
    Nov 24, 2021 at 17:27
  • Did you figure it out Jon? I'm also getting the same issue... Feb 28 at 3:53
10

There is default formatter in the intelephence and it doesen't have many options to customize. You can disable it in the php language-specific settings:

~/.config/Code - OSS/User/settings.json:
----------
...
"[php]": {
    // "editor.defaultFormatter": "bmewburn.vscode-intelephense-client"
    "editor.defaultFormatter": "kokororin.vscode-phpfmt"
},
...

Then you can use some another formatter that support customization, phpfmt for example. Here is my settings:

"phpfmt.exclude": [
    "AllmanStyleBraces"
],
"phpfmt.smart_linebreak_after_curly": true,
"phpfmt.psr2": false,
"phpfmt.detect_indent": true
1
  • Great! Works even better after replacing "detect_indent" with "phpfmt.indent_with_space": 4
    – Teson
    Dec 3, 2019 at 8:47
3

Easy Step, please follow below step -

  1. enter image description here

  2. enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.