52

When using the Format Document command I'd like to change how the code formats. I'm completely new to VSCode and I'm still having trouble navigating the settings, so easy to understand replies would be very helpful. Currently the code is formatting like this:

void start ()
{
//Do stuff here
}

I want it to look like:

void start () {
//Do stuff here
}
Gordoxgrey
  • 521
  • 1
  • 4
  • 3
  • 1
    Possible duplicate of [Visual Studio 2005/2012: How to keep first curly brace on same line?](https://stackoverflow.com/questions/39561/visual-studio-2005-2012-how-to-keep-first-curly-brace-on-same-line) – Matthew Schlachter Mar 06 '18 at 16:52
  • I think [this](https://stackoverflow.com/questions/39561/visual-studio-2005-2012-how-to-keep-first-curly-brace-on-same-line) is what you're looking for – Matthew Schlachter Mar 06 '18 at 16:52
  • 5
    @MatthewSchlachter VS Code is not the same as Visual Studio – DavidG Mar 06 '18 at 16:52
  • 1
    This might be helpful https://github.com/OmniSharp/omnisharp-vscode/issues/243 – Salah Akbari Mar 06 '18 at 16:53
  • Worth noting that common C# convention is to have the braces on a new line so you may be better off getting used to it the way it is now. – DavidG Mar 06 '18 at 16:54
  • 2
    I don't understand what's going on in that post, as I stated in the post, I'm new to VSCode, and I dont know how to access the json files. I've been using curly brackets on the same line for 2 years now, it's not something that I'll change cause to me it looks way neater. – Gordoxgrey Mar 06 '18 at 16:58
  • 5
    No problem you sticking to whatever you prefer, that's fine. I'm just suggesting that most people do it the other way and if you work in a team of developers, it's vital everyone has a similar style. My guess is that you work on your own? – DavidG Mar 06 '18 at 17:02
  • 4
    Even if you work on your own *now*, if you think that you may well work on a team doing C# work in the future, I'd suggest it's worth getting used to more common formatting conventions (and naming conventions - `Start` rather than `start`). – Jon Skeet Mar 06 '18 at 17:20
  • 2
    Could I just get help with the question that I've asked instead of being told how to format my work that would be great. @JonSkeet I know how to use naming conventions, for the sake of an example, I didn't bother using proper naming conventions – Gordoxgrey Mar 06 '18 at 17:24
  • 3
    You've [already been given help](https://stackoverflow.com/questions/49136015/vscode-format-curly-brackets-on-the-same-line-c-sharp#comment85277009_49136015), these comments are additional suggestions that you can do with as you please. There's really no need to get upset about it. We're all friends here. – DavidG Mar 06 '18 at 17:27
  • 2
    @Gordoxgrey: Given that you're going against the normal formatting conventions, it's hardly much of a stretch to think you might be going against naming conventions too. The lesson to learn is that when you're writing a question, it's worth following the normal conventions you would in your code, to avoid causing unnecessary distractions. – Jon Skeet Mar 06 '18 at 17:38
  • Ok a few things from that link that I was sent. 1. How do I install Omnisharp? 2. Where is a .json file located? 3. How do I edit a .json file? 4. If Omnisharp is built into VSCode, how do I edit the settings for it? – Gordoxgrey Mar 06 '18 at 17:41
  • So that's a whole bunch of questions, and I'm afraid you're going to have to spend some time learning that for yourself. If you've been debugging C# code though, chances are that you already have OmniSharp installed as it's just an extension. – DavidG Mar 06 '18 at 17:44
  • Did you read the blog post linked from the GitHub issue? https://www.strathweb.com/2017/01/c-code-formatting-settings-in-vs-code-and-omnisharp/ – Jon Skeet Mar 06 '18 at 18:08
  • @JonSkeet Yes I read through that blog, but it's out of date, I can't find any of the files or folders relating to omnisharp on my system. – Gordoxgrey Mar 06 '18 at 18:15

4 Answers4

67

I have found this simple solution for VScode !

Just create a file called omnisharp.json at the root of your project and paste the following JSON :

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

I found the solution here: https://medium.com/@wearetherock/visual-studio-code-c-put-the-opening-brace-on-the-same-line-as-the-statement-a98c552a544b

Pini Cheyni
  • 5,073
  • 2
  • 40
  • 58
  • 7
    Optionally, for mac users you can make this a global setting for vs code by putting the config in a file at %USERPROFILE%/.omnisharp/omnisharp.json. More info here - https://www.strathweb.com/2017/01/c-code-formatting-settings-in-vs-code-and-omnisharp/ – scottsandersdev Nov 21 '18 at 20:36
  • 6
    This also works for GNU/Linux users (file located at `~/.omnisharp/omnisharp.json`, same content). Thanks! – oscarah May 16 '19 at 15:52
  • 8
    And also on Windows in %USERPROFILE%/.omnisharp/omnisharp.json – Karlsson Apr 23 '21 at 09:12
  • 4
    Don't forget to restart omnisharp after otherwise it won't work right away. – Aaron Record Aug 14 '21 at 15:50
  • 1
    This should have been the default from the get go :) – kfir124 Oct 27 '22 at 14:34
  • 9
    Note: For vscode version 1.25.1+ `./omnisharp.json` is overridden by configuration in `.editorconfig`. This can be disabled by adding `"omnisharp.enableEditorConfigSupport": false` to `./.vscode/settings.json`. – adc Nov 11 '22 at 21:33
  • 1
    @adc If you have an .editorconfig that defines that, why do you want to disable .editorconfig support? Someone, either yourself or your teammates, defined that configuration. It sounds silly to go against your own configuration - just change the appropriate .editorconfig configuration. – Camilo Terevinto Nov 26 '22 at 18:14
  • any solution described above doesn't work for me on `VsCode 1.74.3` – WonderLand Jan 29 '23 at 20:39
  • 1
    @CamiloTerevinto I was configuring a unity project I maintain for the c# code style I prefer. This was the only way I found to get the editor to behave as I intended. Thought it might help other people in the same boat. – adc Feb 02 '23 at 21:44
3

For Windows + VSCode 1.25.1+

  1. Open a folder and paste %USERPROFILE%/.omnisharp/ in the address bar to enter the current user's folder.

  2. Create the omnisharp.json file with the contents mentioned above.

  3. In VSCode open the Command Pallet by Ctrl+Shift+p

  4. Search for Open User Settings (JSON)

  5. Add below line and save the file

    "omnisharp.enableEditorConfigSupport": false

  6. Again open the Command Pallet by Ctrl+Shift+p

  7. Search for Restart OmniSharp

Thanks to Pini Cheyni, Karlsson, and adc

Reza Taba
  • 1,151
  • 11
  • 15
  • Why would you disable editorConfig support? Sounds much smarter to just edit the file to your preferences. – Camilo Terevinto Nov 26 '22 at 18:15
  • 1
    Thanks for the feedback Camilo. I personally don't prefer that solution since it's project-based and the .editorConfig is a project file not a global VSCode settings. Since we're working on multiple projects, we prefer to set these configs globally through the VSCode settings so it applies to all my working projects on my machine. – Reza Taba Nov 26 '22 at 19:25
  • 1
    Mac users can also follow from step 2 if omnisharp.json is not working for them – Rohit Vishwakarma Dec 03 '22 at 04:31
0

If you don't want to define an omnisharp.json for every C# project that you have, a good alternative is this VS Code extension: C# Curly Formatter.

It was the solution for me that didn't want to define such config files for every C# based project that I had.

Raphael Setin
  • 557
  • 5
  • 10
0

This worked for me:

  • Create a .editorconfig file in the project root with the following content:
#  CSharp formatting rules:
[*.cs]
csharp_new_line_before_open_brace = none
  • Restart Omnisharp.
svprdga
  • 2,171
  • 1
  • 28
  • 51