1

Is it possible to make the VS Code move the curly brace to the next line automatically?

E.g. in this code:

namespace MyNamesp
{
    public class Configuration {

    }
}

I would like the code to be formatted like this:

namespace MyNamesp
{
    public class Configuration 
    {

    }
}

ASAP as I typed this:

namespace MyNamesp
{
    public class Configuration {}
}

And pressed (or not pressed, I do not care about this exactly) the Enter while being between the {}.

Right now in order to achieve this kind of formatting I have to select the whole document (Ctrl + A) and click Alt + Shift + 'F'.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
qqqqqqq
  • 1,831
  • 1
  • 18
  • 48
  • 1
    Does this answer your question? [VSCode format curly brackets on the same line c#](https://stackoverflow.com/questions/49136015/vscode-format-curly-brackets-on-the-same-line-c-sharp) – Jim G. Dec 31 '19 at 18:28
  • 1
    Does this answer your question? https://stackoverflow.com/q/56934072/109941 – Jim G. Dec 31 '19 at 18:29
  • @JimG., the stackoverflow.com/q/56934072/109941 answer do not help. Thank you. – qqqqqqq Dec 31 '19 at 18:47
  • @JimG., [the](https://stackoverflow.com/questions/49136015/vscode-format-curly-brackets-on-the-same-line-c-sharp) solution does not work either. Thank you. – qqqqqqq Dec 31 '19 at 18:50
  • 2
    @JimG.'s duplicate is valid. After you have created the `omnisharp json`, click `Alt`+`shift` + `F`. Works! – panoskarajohn Dec 31 '19 at 19:59
  • @panoskarajohn As noted in the question, the OP doesn't want to have to type `Alt`+`Shift`+`F` but have it happen automatically. – NetMage Dec 31 '19 at 20:43
  • 1
    Did you create the `omnisharp json` with everything set to `true`? – NetMage Dec 31 '19 at 20:44
  • 1
    Do you have the Text Editor settings for Format on Type and Format on Paste enabled? – NetMage Dec 31 '19 at 20:48
  • I would recommend you type in `namespace MyNamesp` then press `Enter`, then press `{` then press `Enter` to get what you want. You should not be typing space after e.g. `Configuration` if you want the open brace on a newline, you should be typing `Enter`. You can also enable Format on Save to have it reformat every time you save a file. – NetMage Dec 31 '19 at 20:57
  • @NetMage, over [here](https://stackoverflow.com/questions/49136015/vscode-format-curly-brackets-on-the-same-line-c-sharp) the opposite is recommended. It is recommended to set everything to `false`. But ok, let me try to set everything to `true`. – qqqqqqq Dec 31 '19 at 21:02
  • @NetMage, it does not work with the `omnisharp` even when all the properties are set to `true`. – qqqqqqq Dec 31 '19 at 21:03
  • @NetMage, hm. The solution to press the `Enter` first seems very clever in this case. Could you, please, post it as an answer? – qqqqqqq Dec 31 '19 at 21:05
  • That question is asking the opposite of yours, the answer is how to prevent a newline before braces. – NetMage Dec 31 '19 at 21:42

1 Answers1

2

I think it would help if you turned on the Text Editor settings for Format on Type and Format on Paste. You could also turn on Format on Save if desired.

I would recommend you type in namespace MyNamesp then press Enter, then press { then press Enter to get what you want. You should not be typing space after e.g. Configuration if you want the open brace on a newline, you should be typing Enter.

NetMage
  • 26,163
  • 3
  • 34
  • 55