40

Any clues on why my VS Code "Format Code" command doesn't work? I used Shift+Ctrl+P and entered Format Code or used the shortcut Shift + Alt + F. None seem to be working. Any clue?

4
  • Which file type / language do you want to format? Maybe code formatting is not supported for that language. Commented Sep 29, 2016 at 20:42
  • Just plain html. The file is saved as such and HTML is showing at the info bar. I was expecting that this command would format elements, breaking lines and indenting. Commented Sep 30, 2016 at 14:53
  • @MarcosSilva i think you should change the accepted answer mark to one which is relevant so that people can find it on top. I have added one ans that you can check in the thread. Commented Jun 26, 2021 at 13:28
  • Thank you @Shubhamkumar. For me, this issue was resolved when I first reported it and seemed to be related to a bug. I'm not sure if the solutions provided more recently applied to the original issue. But they may be valid for others. Commented Jun 27, 2021 at 14:10

16 Answers 16

67

I guess you are asking for MacOs because by default shortcut for code formatting is different for different Os. Now there might be an issue with the keyboard layout.

See the screenshot

For example Option+Shift+F does not work in ABC-Indian layout, you need to change that. Go to System-prefrences->Keyboard->Input Sources-> Add layout "ABC" and delete the old one.

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

1 Comment

If one gets a character in active editing document when one uses the shortcut key. I knew this had to be a keyboard thing. Here I found supporting answers, went ahead and tried that worked.
21

For me, CTRL SHIFT i worked on Ubuntu 20.04

3 Comments

Doesn't work for me as well on Ubuntu 20.04. Were you able to fix this?
I am using 20.04 as well. Didn't find the root cause.
Here you go, I fixed it. In my case, I misunderstood how the command actually works. I thought that I could just keep pressing Shift + Alt + I and expect cursors at the end of the lines starting from the current line. However, first, you've to select the paragraph where you want the cursors and then press Shift + Alt + I.
16

You need to set the default formatter in your VS Code.

Click Cmd+Shift+P, and choose "Format Document With..."

enter image description here

Then, instead of choosing a formatter, choose "Configure Default Formatter..."

enter image description here

And then choose your default formatter that works for you

enter image description here

It will now work and format the document when you click SHIFT+OPTION+F (or Shift+Alt+F in Windows). My default formatter was configured to Prettier ESlint instead of Prettier - Code Formatter and it didn't work. The shortcut worked only after changing the default formatter to Prettier - Code Formatter.

You can install it here: https://prettier.io/docs/en/install.html

Comments

6

i'm having the same issue - it's looks like a bug.

See:

Comments

5

If you have an error in your code, the formatting will not allow you to change the layout. Fix any bugs you have (if you have any of course) then try shift + alt + f.

Comments

3

The bug is partially fixed in v1.6.0.

You may still face this issue as the patch is not for all keyboard layouts. Switching keyboard layout to English(US) solved the problem for me.

The issue is open on github.

1 Comment

I had this issue in my latest Mac, I wasn't able to resolve this. I had keyboard selected as ABC- India and changed to English US. This resolved my formatting issue. Thanks
3

I had the Same Issue and Found a working solution.

  1. Right click on the file which you want to format.
  2. Select "Format With" option.
  3. Choose Last option i.e 'Select Default Formatter'
  4. Then select you preferred Formatter option. Voila ! you are all Done. it should be working fine again on ALT+SHIFT+F.

Note : i tried all way by setting default to reinstall formatter and still it didn't work until this above solution. Thanks me Later !!

Comments

2

I faced the same issue (on ubuntu 18 - windows didn't seem to have this issue).

You can resolve this by changing the keybinding.

Go to File -> Preferences -> Keyboard shortcuts

Search for Format Document to find out which keybinding is currently able to format your code.

You can choose to continue with the default keybinding or change it by clicking on the edit icon on the left of the command you're interested in changing.

In this case, you would simply hit Alt + Shift + F and that's it.

Comments

2

For me it was an extension that greedily used that shortcut

  1. Go to Code > Preferences > Keyboard shortcuts
  2. Find the shortcut that doesn't work
  3. Right-click > 'Show same keybindings'
  4. Right click to remove any that are obviously clashing that you don't need For me the issue was with command + shift + f which clashed with an extension that came with the Vue Volar extension pack. A different issue but it may solve the original issue

Comments

0

Make sure you changed your language to the type of file. (Ctrl+K,M or Ctrl+Shift+P -> Change Language Mode)

Comments

0

I also faced the same problem. My issue was that in my VScode's settings.json (ctrl + ,), I had multiple 'defaultFormatter' selected. I commented out one of them and it started working. enter image description here

Comments

0

Install vs-code extension called "Prettier - Code formatter". Then set it as default formatter. The popup should guide you for this. The Option+Shift+F in Mac should work as expected to do auto formatting.

Comments

0

Maybe you haven't install package autopep8.
You can use the command to install it, it works for me.

conda install autopep8 or pip install autopep8

Comments

0

If there is an error in the code, the shortcut key will not work.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

enter image description here

There is some default keybinding, due to which "option + some key" results in adding some sign like a ~ or ^.

In my condition I was able to format using command palette or right click on document and then selecting Format document , but not by using keyboard.

Just change the keyboard shortcuts( code > settings> keyboard shortcuts), and then search format document , on left side on hovering a pencil(edit button) clickon that and then change the keys to. command + shift + f instead of option + shift+ f and hit enter.

Now, you will be able to format document using keys-> cmd+ shift + f

1 Comment

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

Newer answer to an older question, but it might be useful for someone who's looking for this subject: In VS Code (and Azure Data Studio) for your Keyboard Shortcuts, you can edit the expression for the When condition.

This takes into account when your Shortcut keybinding should apply.

There could be something wrong with the expression mentioned there, or the situation when you press your combo might not apply exactly as you would think.

For mor info on how the When clause should be built and which contexts are available etc, see here.

Example of Keyboard Shortcuts with When clauses

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.