43

I downloaded Visual Studio Code thinking it would be nice to write T-SQL scripts and have the pretty auto-formatting happen as write.

I've already installed the mssql extension.

When creating a new file, the file is a simple plaintext with no extension 'Untitled-1'.

How in the world do I tell VS Code this is a SQL script so it formats correctly?

If I hit Shift + ALT + F, I simply receive the error "There is no formatter for 'plaintext' installed."

If I save the file with the extension '.sql', the format is applied.

However, I don't want to have to save the file... I want it to just be a scratchpad.

Is there any way in VS Code to tell the editor the file should be formatted SQL other than saving the file as an SQL file?

0

6 Answers 6

49

This is what I did to format SQL queries:

  1. Install the SQLTools extension
  2. Open your query
  3. Press Ctrl+Shift+P
  4. Select "Format Document With"
  5. Select SQLTools
3
  • 6
    Tip: add the following lines to the settings.json to format file automatically when saving ... "[sql]": { "editor.formatOnSave": true, "editor.defaultFormatter": "mtxr.sqltools" } Commented Oct 6, 2021 at 19:57
  • formatting with SQLTools changes strings by adding spaces, anyway to disable that?
    – dshrikant
    Commented Oct 11, 2021 at 6:28
  • This is a cool hack, but this doesn't work when there are other interferring SQL extensions installed like Snowflake, I had to uninstall that for this to work. Commented Feb 9 at 9:09
22

I hope this will be of some help to you.

If I understood you correctly, you can use first this shortcut:

Win: Ctrl+Shift+P / Mac: Cmd+Shift+p

And then type "Change Language Mode".

Also try and visit this link for additional info: https://marketplace.visualstudio.com/items?itemName=cymonk.sql-formatter

Also you can visit this link: https://code.visualstudio.com/docs/languages/overview where you can find additional info about that, for example:

Changing the language for the selected file

In VS Code, we default the language support for a file based on its filename extension. However, at times you may wish to change language modes, to do this click on the language indicator - which is located on the right hand of the Status Bar. This will bring up the Select Language Mode drop-down where you can select another language for the current file.

Tip: You can get the same drop-down by running the Change Language Mode command (Ctrl+K M).

1
  • 1
    Ctrl+K M + SQL > press enter saves the day everytime :-)
    – 321X
    Commented Nov 4, 2020 at 8:58
9

The other methods work, but the easy button is to click on Plain Text on the status bar Click on Plain Text in status bar

and then type in "SQL" to use your favorite installed SQL extension

Set SQL

3

To format your SQL code in Visual Studio Code, you can follow these steps:

Go to "Settings" by either clicking on the gear icon in the lower left corner and selecting "Settings" or by using the shortcut a Ctrl + ,.

In the search bar at the top of the Settings pane, type "Format" and select "Editor: Default Formatter".

Choose "SQL Formatter" from the dropdown menu.

Install the VS Code SQL Formatter extension from the Visual Studio Code Marketplace.

Once the extension is installed, you can format your SQL code using the following steps:

Open the SQL file you want to format. Go to "View" > "Command Palette" or simply use the shortcut Ctrl + Shift + P. Type "Formatter" and select "Format Document". Choose "SQL Formatter".

2

When you have no option "Format Document Wiht..." or no SQL in Command Palette enter image description here

or no SQLTools enter image description here

you just need to install a SQLTools formatter: write in extensions filter:
@popular category:formatters sql

and install one (I installed SQLTools)

enter image description here

2

To Add
some Extensions that looks good to me.
(but these options on formatting are still not as many as I expected)

2
  • Thanks for mentioning pgFormatter. My favourite formatter so far. Commented Oct 21, 2023 at 2:53
  • Tbh, the best one I found so far, is not Vscode extension, but Jetbrains es Datagrip .
    – Nor.Z
    Commented May 29 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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