120

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

24 Answers 24

99

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.

Sign up to request clarification or add additional context in comments.

8 Comments

Just curious, how did you find out the information regarding ms-dotnettools.csharp
Basically just scrolling through vscode-intellisense (suggestions), and just tried it out and worked.
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...
Thank you, i was facing the same issue, prettier was trying to format it by default
you should also add the "editor.formatOnSave": true that it formats the code onsave
|
38

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 Comments

It is Shift+Alt+F on windows, Ctrl + Shift + I on ubuntu
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
Thanks for pointing out (requires restart). I totally missed it.
does not work for me, after the restart still asks me for the formatter for csharp files
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 Comments

File names also have to be TitleCased as well.
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
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.

2 Comments

Here is a good full omnisharp.json, example and other info.
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.
7
  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 Comment

that was not asked by OP, to enable the format on type
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 Comments

i just found it too. turns out, that this is the only option.
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
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.
This extension is now unpublished from Marketplace. 🙁
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.
|
6

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

"omnisharp.useEditorFormattingSettings": true

Setting this to false fixed indenting for me.

2 Comments

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
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
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 Comments

"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.
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.
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 Comments

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.
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.
what do you mean "behave nicely"??
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 Comment

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?
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#.

Comments

2

Open VS Code 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.

1 Comment

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!
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 in this link:
https://dotnet.microsoft.com/en-us/download/dotnet/6.0

Comments

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.

Comments

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

Comments

1

Download the extension called CSharpier. It is the official extension from Microsoft for vscode. It helped me solve the problem. Configure it as the default formatter for cshrap files.

2 Comments

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
It's not an official extension from Microsoft. But it's probably the only that DOES the job.
1

: Shift+Alt+F
: Ctrl+Shift+I

Comments

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 Comment

Do you know what setting influences this behaviour?
1

For 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.

Comments

0

If you're using VS Code in :
Shift+Option+F.

Comments

0

For me it was due to VS Code's restricted mode

There was this small banner I did not notice at first.

Restricted Mode is intended for safe code browsing. Trust this windows to enable all features image showing restricted mode banner

When I was using the usual Ctrl+K Ctrl+F, it was saying:

They key combination (Ctrl+K Ctrl+F) is not a command

After losing some precious time on this issue, I finally activated the trusted mode and it worked as expected!

showing the workspace trust window

The main problem in my opinion is it should say the formatting is blocked by the restricted mode instead of having you guess it.

Comments

0

Even after setting tab/space size in VS Code 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:
C# code formatting settings in VS Code and OmniSharp

Comments

-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 Comment

@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
-1

Working fine in :
Shift+Alt+F

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.