13

I have enabled the latest C# extension in my Visual Studio Code editor. Instead of formatting the code while saving or by applying the key combination Ctrl + K, Ctrl + F or Alt + Shift + F, I need to format the current line of code while hitting the Enter key. This feature is already available in Visual Studio, but not found in Visual Studio Code by default.

This is the sample code output I need to achieve:

Enter image description here

5
  • If your project has DBContext (usually at least a medium sized project) is VSCode, as opposed to VS Community, the right tool for you? Commented Mar 26, 2018 at 21:00
  • 1
    @Brad This is just a sample code. If I try any simple c# code I need to manually format the code in VSCode editor. eg adding a new property in a class -> public string property{get;set;} Here If I hit Enter Key or semicolon it won't get auto formatted as in Visual Studio. Commented Mar 26, 2018 at 21:14
  • 1
    Does [Ctrl+K Ctrl+F] or Alt+Shift+F work at all? Commented Mar 26, 2018 at 21:46
  • 1
    @Brad Yes, it works. But don't have on-the-go code formatting as we had in Visual Studio. There I don't need to worry about any code formatting manually. Commented Mar 26, 2018 at 21:54
  • Does anyone know why this is such an issue? In all honesty, it's the only reason I don't favour VSCode. I'm so much more productive in full VS when cutting code all day. It's a simple thing. Commented Nov 23, 2021 at 16:35

5 Answers 5

23

I have found an option which makes it easier to format code while typing.

I applied the below settings in workspace settings:

{
      "editor.formatOnSave": true,
      "editor.formatOnType": true
}

This works fine for me.

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

3 Comments

Your question is not exactly match with the solution you looked for. you mentioned when pressing an "enter key" indentation has to be changed, that is why all are try to help you to configure the key configs.
@errakeshpd you are correct. But I mentioned there as I need a similar functionality as in Visual Studio i.e., able to edit the current line of code without applying any shortcuts or key combination.
This is what I needed, thanks. Always wondered why VS Code could not format things automatically even when settings in UI claims they are.
16

Go to menu FilePreferenceSettings.

Search for format

Select the options you would like:

  • Format on Paste

  • Format on Save

  • Format on Type

Close the Settings window.

Enter image description here

You can also see it in your settings.json file:

Enter image description here

1 Comment

Egg-zactly what I was looking for. Should be on by default imo.
8

Go to menu FilePreferencesKeyboard Shortcut (Ctrl + K, Ctrl + S)

Click on the keybindings.json link:

Enter image description here

Enter the below binding for the Enter key. This binding will overwrite the defaults for current user.

{
  "key": "enter",
  "command": "editor.action.formatDocument",
  "when": "editorHasSelection"
}

Another alternative solution is to use macros extension - a custom macros support for Visual Studio Code, so you will be able to do more than one command in one key binding.

Add macros to User Settings:

"macros": {
    "formatWithEnter": [
        "editor.action.insertLineAfter",
        "editor.action.formatDocument"
    ]
}

And the below key binding to keybindings.json:

{
    "key": "enter",
    "command": "macros.formatWithEnter"
}

5 Comments

Thank you for your answer. This option formats the code but makes new issues. Now I cannot able to choose code intelligence while hitting the enter key. The other new issue is If I have open and close brace {} an I hit enter after the first {, It doesn't work as before. I need to use Shift+Enter to do this. Can you provide any alternative method?
Check updated answer will do the format when only there is a text selected, but still, you are overwriting the functionality of very common key enter.
This is pretty much better than the previous option. But still like explicitly applying a shortcut rather than formatting as in Visual Studio.
This is not exactly fulfilling my requirement but seems like better than the default option in VS Code. So I'm making this as the correct answer.
Thanks, Also check alternative solution on my answer.
3

Code formatters available on Visual Studio default as

  • On Windows: Shift + Alt + F
  • On Mac: Shift + Option + F

If you again wish to do it when pressing Enter you need to set up your workspace preferences and then configure the key bindings:

{ "key": "enter", "command": "editor.action.format" }

It now formats the whole document if nothing is selected, and else it formats the selection.

Also there is the beautify.onSave, editor.formatOnSave option. Please try that too to make the code pretty.

2 Comments

@errakseshpd Thank you for your comment. But after setting these option I'm getting some weird behavior. Please go through my comments on the other answers. I think there is no better solution is currently available for this.
-2

Edit: This doesn't actually work because this will suppress the regular Enter key behavior

To get this to work for me I had to install two extensions

Without the second extension I was getting the error visual studio code there is no formatter for 'csharp'-files installed

I also made sure my Visual Studio Code was up to date (menu* → HelpRestart and Update or Check for Updates)

Then I added a custom key binding. menu FilePreferencesKeyboard Shortcuts"For advanced customizations open and edit keybindings.json":

[{
    "key": "enter",
    "command": "editor.action.formatDocument",
    "when": "editorTextFocus"
}
]

I had to type in the word enter because the dialog to capture keys doesn't acknowledge it.

7 Comments

This option formats the code but makes new issues. Now I cannot able to choose code intelligence while hitting the enter key. The other new issue is If I have open and close brace {} an I hit enter after the first {, It doesn't work as before. I need to use Shift+Enter to do this. Can you provide any alternative method?
yep, I think you're right. This does the formatting BUT it suppresses the Enter key. You need it to do both obviously.
@JosephJojoJohn you might need a macro to do that because it might mean running multiple commands (the format then also the enter key).
You are right. But I was wondering why this option is not available by default in VS Code. I think most of the developers need this option.
That, I'm sorry to say, I cannot answer
|

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.