108

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
andoral
  • 1,141
  • 2
  • 8
  • 9

20 Answers20

79

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.

ezhupa99
  • 1,838
  • 13
  • 18
  • 1
    Just curious, how did you find out the information regarding ms-dotnettools.csharp – Orson May 16 '21 at 09:43
  • 1
    Basically just scrolling through vscode-intellisense (suggestions), and just tried it out and worked. – ezhupa99 May 16 '21 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 May 18 '21 at 07:55
  • 1
    Thank you, i was facing the same issue, prettier was trying to format it by default – jvbs Aug 07 '21 at 19:36
  • If you still have an issue with indent, check the @Samuel Ridings answer bellow – Maxim Zhukov Dec 01 '21 at 04:48
  • you should also add the "editor.formatOnSave": true that it formats the code onsave – Tobias Koller Oct 11 '22 at 03:51
  • I was pasting some c# code from another system into a new file (without saving) and formatting wasn't working. I eventually got it working by saving the code into a file called CSharpFileForFormatting.cs and saving that in a folder. Open that folder in VS code and the formatting worked. – Brett Manners Jun 08 '23 at 03:37
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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sampath
  • 63,341
  • 64
  • 307
  • 441
  • 26
    It is Shift+Alt+F on windows, Ctrl + Shift + I on ubuntu – James L. Mar 14 '19 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 Jul 10 '19 at 05:24
  • 3
    Thanks for pointing out `(requires restart)`. I totally missed it. – software_writer Apr 05 '21 at 17:51
  • 2
    does not work for me, after the restart still asks me for the formatter for csharp files – serge Jun 03 '22 at 10:28
26

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pavel Levchuk
  • 757
  • 8
  • 11
  • 2
    File names also have to be TitleCased as well. – Chase Dec 24 '20 at 07: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 Jun 03 '22 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/

Yurii Brusentsov
  • 539
  • 4
  • 12
  • [Here](https://www.strathweb.com/2017/01/c-code-formatting-settings-in-vs-code-and-omnisharp/) is a good full `omnisharp.json`, example and other info. – not2qubit Nov 04 '22 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](https://github.com/OmniSharp/omnisharp-roslyn/wiki/Configuration-Options). – Trasd Feb 13 '23 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Andrew B
  • 364
  • 5
  • 6
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Peter
  • 1,361
  • 15
  • 19
  • i just found it too. turns out, that this is the only option. – andoral Nov 17 '17 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: https://stackoverflow.com/a/59154222/565985 – Yaniv Dec 03 '19 at 09:48
  • @Yaniv how do you prevent formatters like prettier from interfering with OmniSharp's formatter? – Jacques Apr 14 '20 at 10:49
  • 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 Jun 03 '20 at 09:55
  • 2
    This extension is now unpublished from Marketplace. – Aizzat Suhardi Jan 06 '21 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. – granadaCoder Mar 03 '21 at 21:43
5

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

"omnisharp.useEditorFormattingSettings": true

Setting this to false fixed indenting for me.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 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 Nov 19 '20 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 Mar 30 '21 at 01:10
  • This also worked for me, thank you – Maxim Zhukov Dec 01 '21 at 04: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).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ali Dehqan
  • 461
  • 1
  • 8
  • 18
  • 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 Mar 29 '18 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 Apr 21 '19 at 08: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).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Yaniv
  • 966
  • 12
  • 20
  • 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. – Micah Zoltu Feb 17 '20 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. – David Ferenczy Rogožan Mar 23 '21 at 21:19
  • what do you mean "behave nicely"?? – serge Jun 03 '22 at 10:46
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.

Sen
  • 21
  • 1
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#.

YanMax
  • 75
  • 4
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.

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
SSD
  • 21
  • 2
  • 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 Aug 07 '22 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.

Manabu Nakazawa
  • 1,983
  • 22
  • 23
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

Mr. YANO
  • 11
  • 1
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

Trevor Daniels
  • 1,119
  • 8
  • 5
1

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

poloC
  • 537
  • 2
  • 6
  • 16
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.

Anas Einea
  • 61
  • 1
  • 3
0

Working fine in Windows:

--> Shift + Alt + F
0

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!

masterxilo
  • 2,503
  • 1
  • 30
  • 35
-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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
napi15
  • 2,354
  • 2
  • 31
  • 55
  • @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 Nov 17 '17 at 14:34