87

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,030
  • 21
  • 100
  • 124
andoral
  • 931
  • 1
  • 7
  • 9

17 Answers17

61

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,061
  • 9
  • 13
  • 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
34

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,030
  • 21
  • 100
  • 124
Sampath
  • 58,546
  • 53
  • 279
  • 406
  • 21
    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
  • 2
    Thanks for pointing out `(requires restart)`. I totally missed it. – software_writer Apr 05 '21 at 17:51
  • does not work for me, after the restart still asks me for the formatter for csharp files – serge Jun 03 '22 at 10:28
23

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,030
  • 21
  • 100
  • 124
Pavel Levchuk
  • 661
  • 8
  • 11
  • 2
    File names also have to be TitleCased as well. – Chase Dec 24 '20 at 07:20
  • 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
9
  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,030
  • 21
  • 100
  • 124
Andrew B
  • 366
  • 5
  • 6
7

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,030
  • 21
  • 100
  • 124
Peter
  • 1,315
  • 14
  • 18
  • 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
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,030
  • 21
  • 100
  • 124
Ali Dehqan
  • 362
  • 1
  • 6
  • 16
  • 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. – Sergei Apr 21 '19 at 08:46
4

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

"omnisharp.useEditorFormattingSettings": true

Setting this to false fixed indenting for me.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
2

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,030
  • 21
  • 100
  • 124
Yaniv
  • 896
  • 11
  • 19
  • 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
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
  • 1,320
  • 13
  • 18
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

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/

1

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
  • 11
  • 1
0

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,030
  • 21
  • 100
  • 124
napi15
  • 2,226
  • 2
  • 23
  • 48
  • @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
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
  • 51
  • 3
0

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
  • 921
  • 7
  • 4
0

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
  • 29
  • 4
-1

Working fine in Windows:

--> Shift + Alt + F