9

I'm intending to format the selection (indentation) for the PHP code, but it does not work.

  • I already made sure that there aren't duplicate shortcuts.
  • I also disabled all extensions.
  • I changed the keyboard shortcut from Ctrl+K Ctrl+f to Ctrl+k Ctrl+y.

None of these helped.

Is the only language that does not work for me to format selection Eye. It's not because I'm missing the closing tag (?>).

enter image description here

Pika Supports Ukraine
  • 3,612
  • 10
  • 26
  • 42
Labrador
  • 417
  • 2
  • 5
  • 18
  • Doesn't seem to work for html.erb files either (even after [installing a formatter](https://stackoverflow.com/a/72851330/5783745). Seems to be a [closed issue](https://github.com/microsoft/vscode/issues/36126) but it's been locked so it's difficult to tell what solution they came to. – stevec Jul 04 '22 at 05:19

4 Answers4

2

To see menu bar if not present press

Left Alt

then go:

Preferences > Keyboard Shortcuts

type in the search bar

ctrl+k ctrl+f

you should see

enter image description here

perhaps you have a collision and other command has the same shortcut defined or your shortcut is not defined at all. You can double click on shortcut to edit it.

Note at the picture When this is when the command works because one shortcut may work only if you are currently editing document and other when you are browsing files so once you set a shortcut make sure your checking it in different places of editor to see if its working or not.

If you use shortcut:

Ctrl+Shift+P

and select command:

enter image description here

You'll see whole bunch of shortcuts and there should be there one you are missing:

{ "key": "ctrl+k ctrl+f",         "command": "editor.action.formatSelection",
                                     "when": "editorHasDocumentSelectionFormattingProvider && editorHasDocumentSelectionFormattingProvider && editorTextFocus && !editorReadonly" },

I think that you can just copy the one above, paste to your file if it is not present and save that file, restart your Code and all should be working. Remember that the file is JSON so keep its format - look how other keys are presented there and your pasting should not make JSON invalid.

Jimmix
  • 5,644
  • 6
  • 44
  • 71
2

.vue file doesn't have formate selection. This function depends on your file type.

hellorayza
  • 49
  • 1
  • 7
  • 1
    I'm using vetur and in their docs says: > Vetur only has a "whole document formatter" and cannot format arbitrary ranges. > As a result, only the Format Document command is available. > The **Format Selection** command does not work. More [here](https://vuejs.github.io/vetur/guide/formatting.html#formatters) – cristian ayala Jan 12 '22 at 14:30
0

I checked that this key binding was indeed still specified, there was no duplicate key binding, etc.; still, Visual Studio Code refused to recognize the key combination. Then I quit visual studio code and restarted it, and the key combination started working again.

Sometimes, the basic quit-and-restart is the answer!

auspicious99
  • 3,902
  • 1
  • 44
  • 58
0

If nothing is working you can create your own "format selection" with multiple commands. You would need a code formatter and a macro extension to run multiple commands from one keybinding,I'm using "prettier" and "multi-command" extension.

You can use this keybinding in your keybindings.json (Click File -> Preferences -> Keyboard shortcuts. Use the tab that opens up to edit and find available key bindings) with the multi-command extension - no need for anything in settings.json:

{
    "key": "Shift+Alt+A", // or whatever keybinding you wish
    "command": "extension.multiCommand.execute",
    "args": {
      "sequence": [
        "editor.action.formatDocument",
        "editor.action.clipboardCopyAction",
        "undo",
        "editor.action.clipboardPasteAction",
      ]
    },
    "when": "editorHasDocumentFormattingProvider && editorTextFocus && !editorReadonly" 
  }

I use it because "format selection" is not working with "*.vue" files.