Labrador
Labrador

Reputation: 497

Why won't the key combination Ctrl-K Ctrl-F work for php visual code?

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

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

Upvotes: 12

Views: 17548

Answers (4)

cristian ayala
cristian ayala

Reputation: 125

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.

Upvotes: 0

auspicious99
auspicious99

Reputation: 4331

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!

Upvotes: 0

hellorayza
hellorayza

Reputation: 75

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

Upvotes: 2

Jimmix
Jimmix

Reputation: 6556

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.

Upvotes: 4

Related Questions