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

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 asked Dec 1, 2019 at 7:50 Giulio Caccin Giulio Caccin 816 1 1 gold badge 9 9 silver badges 25 25 bronze badges

2 Answers 2

In the latest VS Code (v1.47):

The detected file type is printed on the lower bar of the window, and by default it'll likely say Plain Text for new, unsaved, files.

Screenshot of visual studio code lower-right menu-bar

Click on Plain Text , that'll bring up a box where you can select other formats VS Code knows about. Select the one you want or start typing what you want in the search, such as JSON , and that'll convert the detected file-type over.
Now you can use the standard Format Document command from the command palette, so long as VS Code has a formatter for that file-type.

816 1 1 gold badge 9 9 silver badges 25 25 bronze badges answered Jul 17, 2020 at 0:51 179 1 1 silver badge 3 3 bronze badges

UPDATE 2 Now the new tab shows directly a selection mode that says:

Select a language to get started. Start typing to dismiss, or don't show this again.

So you just need to press Select a language

visual studio code selection mode

UPDATE 1
In the bottom right corner is available the Select Language Mode functionality that now let you select a formatting option, the default one is Plain Text . The same functionality can be used with Ctrl + K M or by selecting Change Language Mode from the command palette.

Select Language Mode in Visual Studio Code

OLD ANSWER
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:

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":