116

I created a new file, set the C# language, and wrote some code. Then I pressed Ctrl + Shift + F (or F1Format Document). And got I the error

Sorry, but there is no formatter for 'csharp'-files installed.

Also, I installed C# Extension, but it didn't help. The Visual Studio Code version is 1.18.0.

3

22 Answers 22

94

If you have prettier as the default formatter as I do, you should do this:

  1. Open your vscode settings with these shortcut: ctrl + ,, or enter image description here

  2. Then click to "open settings (JSON)": enter image description here

  3. This is where you should paste the below snippet.

This is my config:

{
  "editor.defaultFormatter": "esbenp.prettier-vscode",
  "[csharp]": {
    "editor.defaultFormatter": "ms-dotnettools.csharp"
  }
}

For you to apply this configuration you need c# extension.

8
  • 1
    Just curious, how did you find out the information regarding ms-dotnettools.csharp
    – Orson
    Commented May 16, 2021 at 9:43
  • 1
    Basically just scrolling through vscode-intellisense (suggestions), and just tried it out and worked.
    – ezhupa99
    Commented May 16, 2021 at 11:49
  • 1
    Basically you tell the formatter to use ms-dotnettools.csharp for C# and esbenp.prettier-vscode for any other language. I guess prettier has no C# formatter currently. There might be plugins around...
    – knnhcn
    Commented May 18, 2021 at 7:55
  • 1
    Thank you, i was facing the same issue, prettier was trying to format it by default
    – jvbs
    Commented Aug 7, 2021 at 19:36
  • 1
    you should also add the "editor.formatOnSave": true that it formats the code onsave Commented Oct 11, 2022 at 3:51
37

This works for me.

Note: If it is true then clear the checkbox and set it to true again. After that, you must restart Visual Studio Code.

Menu FilePreferencesSettings

Enter image description here

4
  • 26
    It is Shift+Alt+F on windows, Ctrl + Shift + I on ubuntu
    – James L.
    Commented Mar 14, 2019 at 15:23
  • Thank you - I use the Visual Studio extension that makes the Format hotkeys the same as the big brother IDE. I recommend it. The C# FixFormat extension is only for C# ~3/4/5 unity engine projects on mac
    – Chazt3n
    Commented Jul 10, 2019 at 5:24
  • 3
    Thanks for pointing out (requires restart). I totally missed it. Commented Apr 5, 2021 at 17:51
  • 2
    does not work for me, after the restart still asks me for the formatter for csharp files
    – serge
    Commented Jun 3, 2022 at 10:28
25

Visual Studio Code with OmniSharp doesn't format C# code without a .csproj file.

You can create a new project with dotnet new console with the .NET Core SDK.

2
  • 2
    File names also have to be TitleCased as well.
    – Chase
    Commented Dec 24, 2020 at 7:20
  • 1
    what do you mean "without", what if a open a folder, not a sln or csproj file, but the edited cs file does belong finally to a csproj
    – serge
    Commented Jun 3, 2022 at 10:35
10

Try to create an OmniSharp config for your project that will allow you to customize the formatting of C# code.
Create the omnisharp.json in the project root (near the .sln file)

{
  "FormattingOptions": {
    "newLine": "\n",
    "useTabs": false,
    "tabSize": 4,
    "indentationSize": 4
  }
}

NOTE: to confirm updates in omnisharp.json you should reload vscode (ctrl+shift+p -> Developer: Reload Window)

More info about OmniSharp config: https://www.strathweb.com/2017/01/c-code-formatting-settings-in-vs-code-and-omnisharp/

2
  • Here is a good full omnisharp.json, example and other info.
    – not2qubit
    Commented Nov 4, 2022 at 19:21
  • This is by far the best and easiest solution. Just one thought though, I edit the omnisharp.json file in my %USERPROFILE%/.omnisharp/ directory so the settings are available to all my sessions, not just the current one. You can find the official documentation here.
    – Trasd
    Commented Feb 13, 2023 at 18:53
8
  1. Browse the Visual Studio Code extension library, and make sure you've got the C# extension installed ms-dotnettools.csharp

    VS Code extensions pane showing C# OmniSharp extension

  2. Press Ctrl + , to open the Settings panel. Change the editor.formatOnType setting to be enabled. This wasn't enabled by default in my Visual Studio Code.

    VS Code settings window with editor.formatOnType setting

  3. Restart Visual Studio Code. It should now make code auto-format when you complete the line with a semicolon, or when you close the outer brace of a scope.

Note: this is a global setting for the editor, so it may enable auto-formatting for other languages and not just C#.

This was tested on Visual Studio Code version 1.43.1.

1
  • 2
    that was not asked by OP, to enable the format on type
    – serge
    Commented Jun 3, 2022 at 10:43
6

The C# extension powered by Omnisharp doesn't have a formatter included (as far as I know).

You can install C# FixFormat. That does the trick for me, but the formatting is not as good as in Visual Studio IDE.

6
  • i just found it too. turns out, that this is the only option.
    – andoral
    Commented Nov 17, 2017 at 14:32
  • 3
    Omnisharp does include a C# formatter. Just make sure to open the file from withing a folder - please see my answer: stackoverflow.com/a/59154222/565985
    – Yaniv
    Commented Dec 3, 2019 at 9:48
  • 1
    It's 2020 and FixFormat is as bad as it was. I hoped I could really use VS Code to develop Blazor projects, but not really. One of the main issues is really bad formatting provided. No good plugins, Blazor and Razor files treated as they was invented yesterday. But the worst thing in FixFormat is really bad english used to describe the options. A lots of "Spaces or tabs: yes or no" kind of nonsenses. So VS Community. Still many, many bugs, but at least formatting is almost fully configurable.
    – Harry
    Commented Jun 3, 2020 at 9:55
  • 2
    This extension is now unpublished from Marketplace. 🙁 Commented Jan 6, 2021 at 17:44
  • 1
    This extension is now unpublished from Marketplace. You can choose to uninstall it. Yeah, same message for future readers. I'm sad I missed this one. Commented Mar 3, 2021 at 21:43
6

I found there was another setting interfering with formatting C#.

"omnisharp.useEditorFormattingSettings": true

Setting this to false fixed indenting for me.

3
  • 4
    I couldn't find this particular setting, however I discovered that if chose the "Omnisharp: Select Project" followed by selecting my project then my formatting was sorted
    – Tim Newton
    Commented Nov 19, 2020 at 17:22
  • 1
    worked for me! after trying every other solution, turned out when I switched to another project from the cmd, vscode didn't actually change the current project
    – tintin
    Commented Mar 30, 2021 at 1:10
  • This also worked for me, thank you Commented Dec 1, 2021 at 4:46
4

It is resolved after updating to Visual Studio Code 1.20.1 and re-enabling OmniSharp.

And just set "csharp.format.enable" to "true" in Workspace Settings (if it was true and not working yet, change it to false and then to true).

2
  • 1
    "csharp.format.enable": true didn't work for me, and I have to install c# FixFormat, but then it asked me to turn off csharp.format.enable, which has no effect anyway, weird.
    – rraallvv
    Commented Mar 29, 2018 at 22:01
  • In my case changing tab size params caused losing formatter and only setting "csharp.format.enable": true fixed it. I have only C# (by Omnisharp) installed.
    – Serg
    Commented Apr 21, 2019 at 8:46
3

If you are still hitting this issue, please note that the OmniSharp extension will not behave nicely on .cs files that was not opened from inside a folder (from here).

In order to workaround this - make sure you open the desired file from within a folder (Ctrl + K, Ctrl + O):

Enter image description here

Then the formatting will work (as well as other OmniSharp features).

3
  • 6
    This doesn't work for me. I have the OmniSharp extension installed and up to date, and I have opened a folder in VSCode. I then open a .cs file by clicking on it in the left panel, but when I try to format it tells me there is no formatter installed. Commented Feb 17, 2020 at 11:28
  • What about when there's no file at all? I just open a new editor, paste some source code and I want to format it. That's what I often do with JSON data, for example, and it works without any issue. But with a C# code, it doesn't for some reason. I have the ms-dotnettools.csharp extension. Commented Mar 23, 2021 at 21:19
  • what do you mean "behave nicely"??
    – serge
    Commented Jun 3, 2022 at 10:46
3

my config is the following:

Inside my settings.json I have this setting:

"[csharp]": {
    "editor.defaultFormatter": "ms-dotnettools.csharp",
    "editor.formatOnSave": true
}

Now I have my code formated in visual studio code

class Person
{
    public void Saludar()
    {

    }

    public void Comer()
    {

    }
}
1
  • Thank you for your interest in contributing to the Stack Overflow community. This question already has quite a few answers—including one that has been extensively validated by the community. Are you certain your approach hasn’t been given previously? If so, it would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient. Can you kindly edit your answer to offer an explanation? Commented Feb 15 at 1:48
2

This issue happened to me when the visual studio released new update for 2022, I resolved this by downloading the new .Net 6.0 which you will find https://dotnet.microsoft.com/en-us/download/dotnet/6.0 in this link.

2

I solved that issue by simply pressing F1, writing "format", and hitting enter. In my case, there was no default formatter set in my environment so it prompted me options for formatters. I believe there was only one for C#.

2

Open vscode go to output >  the output of vs code

As you can see, the output is set to task. Change it to OmniSharp log and check if it says the dotnet version is not supported. If yes then update your dotnet version. Hope it helps.

1
  • It had an error saying that I was using an old version of .Net (~3.0.1), installed .Net SDK 6.0 and the error disappeared!
    – Redar
    Commented Aug 7, 2022 at 16:56
1

In my case, I previously installed both dotnet and dotnet-sdk via Homebrew and those seemed to cause a conflict of OmniSharp. So I removed dotnet by:

brew uninstall dotnet

Also, just in case, I re-installed dotnet-sdk by:

brew reinstall dotnet-sdk

Plus, I restarted my Mac, then it finally worked fine.

1

FOR MAC USERS

I solved the issue by setting

"omnisharp.useGlobalMono": "never"

in the VS code settings, it will also prompt you to restart Omnisharp and after that it should work with the shift + option + f key combination

1

Check the Omnisharp log to see if its giving a reason for the lack of formatting. In my case it was dotnet sdk needed updating.

enter image description here

1

I kept pressing Shift + Alt + F (Format Document), thinking my formatter is not working because it was not chaning something I though was misformatted:


namespace MyApp // Note: actual namespace depends on the project name.
{
    internal class Program
    {

        public static async Task Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            var app = PublicClientApplicationBuilder
    .Create(_clientId)
    .WithAuthority(AzureCloudInstance.AzurePublic, _tenantId)
    .WithRedirectUri("http://localhost")
    .Build();

            Console.WriteLine("Hello World!");
        }
    }
}

it turns out that with the default settings, the microsoft c# extension is ok with this formatting, it doesn't think there is something to fix. If I change the indentation of the first line it fixes it.

So: Your code might already be formatted!

1
  • Do you know what setting influences this behaviour?
    – jira
    Commented Mar 20 at 14:32
0

Even after setting tab/space size in VSCode Omnisharp still uses its own rules you can change them by having an "omnisharp.json" with custom rules. for more information on these rules and where you can add the file check this article.

0

Working fine in Windows:

--> Shift + Alt + F
0

windows : Shift+Alt+F ubuntu: : Ctrl + Shift + I

0

If you're using VS code in MacOS: Shift + option + F.

-1

Use the Synaptic package manager and mark the code package for 'Full Removal' and click apply.

Like here:

Delete ~/.vscode
Delete ~/.config
Reinstall

It worked for me.

1
  • @andoral Do what peter suggested with C# FixFormat , this will work if you're using ubuntu , sorry I didn't pay attention that you're using windows OS
    – napi15
    Commented Nov 17, 2017 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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