62

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?

1
  • I’ve been using SQLF (sqlf.app) myself — it’s super easy to use, completely free, and ad-free. It really meets my needs perfectly. Commented Aug 26, 2025 at 0:49

6 Answers 6

62

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
Sign up to request clarification or add additional context in comments.

4 Comments

Tip: add the following lines to the settings.json to format file automatically when saving ... "[sql]": { "editor.formatOnSave": true, "editor.defaultFormatter": "mtxr.sqltools" }
formatting with SQLTools changes strings by adding spaces, anyway to disable that?
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.
I’ve been using SQLF (sqlf.app) myself — it’s super easy to use, completely free, and ad-free. It really meets my needs perfectly.
23

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

9 Comments

Ctrl+K M + SQL > press enter saves the day everytime :-)
The extension you recommend hasn't been updated since 2018-01 so people should look to the other answers.
@Tom, so the extension I recommend is not correct(I see one of my two links is not working so I will delete it or try to replace it)? If I am looking at the other answer's upvoted correctly I believe people do look for other answers too.
@Tom it was updated 11/6 2024
@theking2 It says last updated 2018-01-29.
|
14

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

Comments

7

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


Update

3 Comments

Thanks for mentioning pgFormatter. My favourite formatter so far.
Tbh, the best one I found so far, is not Vscode extension, but Jetbrains es Datagrip .
The one you recommended is popular, but seems to be dead and replaced by github.com/sql-formatter-org/sql-formatter-vscode, which is from the same people as the underlying library that both use.
4

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

Comments

3

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.