2

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.

8

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.

4

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 + KM 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:

  • 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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.