Marcos Silva
Marcos Silva

Reputation: 2019

VS code Format Code command and shortcut Shift+Alt+F doesn't work

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?

Upvotes: 39

Views: 89010

Answers (16)

Thomas Mulder
Thomas Mulder

Reputation: 779

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

Upvotes: 0

zikadow
zikadow

Reputation: 1

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

Upvotes: 0

codycustard
codycustard

Reputation: 548

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

Upvotes: 1

windyxh
windyxh

Reputation: 1

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

Upvotes: 0

yoga333
yoga333

Reputation: 11

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

Upvotes: 0

manjurul Islam
manjurul Islam

Reputation: 73

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.

Upvotes: 0

Shubham kumar
Shubham kumar

Reputation: 812

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.

Upvotes: 64

Sameer
Sameer

Reputation: 61

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

Upvotes: 0

Cepheus
Cepheus

Reputation: 4903

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.

Upvotes: 2

Lorraine Ram-El
Lorraine Ram-El

Reputation: 2755

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

Upvotes: 15

Omar Shabab
Omar Shabab

Reputation: 521

For me, CTRL SHIFT i worked on Ubuntu 20.04

Upvotes: 21

Nikhil kothari
Nikhil kothari

Reputation: 31

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

Upvotes: 3

Paawan Kohli
Paawan Kohli

Reputation: 41

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.

Upvotes: 3

William Parrish
William Parrish

Reputation: 49

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.

Upvotes: 5

st0le
st0le

Reputation: 33545

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

Upvotes: 0

Related Questions