How can I format documents that are not saved in visual studio code?

0

If i Shift + Alt + F (or use the command from the palette) on a Untitled tab on vscode the following command appear:

There is no formatter for 'plaintext' files installed.

Is there any way to specify a different format for the unsaved file to enable the formatter of choice?

This need usually arise when I usually copy/paste some json/xml.

Giulio Caccin

Posted 2019-12-01T07:50:24.873

Reputation: 272

Answers

0

I found two solutions to this specific problem, even if they seems more a workaround than a proper solution:

  1. Using a specific extension to format particular code (json or xml)
  2. Trick VSCode into thinking default unsaved files are json

1 Extensions that format as specific code

Install specific extension that add language specific commands:

  • XML Tools
    default keybinding Ctrl + Shift + Alt + B
  • JSON Tools
    default keybinding Ctrl + Alt + M

PRO

  • no default settings are changed
  • specific format can be used on any selection in any file
  • it's the same solution that notepad++ implement

CONS

  • additional extensions to download may look overkill only to format something
  • more keybindings and additional non standard behaviours

2 VSCode default unsaved files trick

Add a configuration to settings.json that specify json as the type of document for untitled files:

"files.associations": {
    "Untitled-*" : "json"
}

PRO

  • default Format document is used
  • no additional download is required

CONS

  • only one format can be specified
  • locale-dependant (in languages other than English it should not work)

Giulio Caccin

Posted 2019-12-01T07:50:24.873

Reputation: 272