A VS Code extension for formatting SQL files and embedded SQL queries. Supports multiple SQL dialects, dynamic string templates (Python f-strings, JavaScript template literals, Jinja2, etc.), and provides context menu integration for both the editor and file explorer.
Features
- Format .sql files using the standard Format Document shortcut (Shift+Alt+F) or via right-click context menu.
- Format selected SQL in any file type -- select SQL text, right-click, and choose "Format with SQL Formatter".
- Embedded SQL support -- formats SQL inside Python triple-quoted strings, JavaScript template literals, C# interpolated strings, and more, preserving the string delimiters.
- Template placeholder preservation -- handles dynamic string building patterns without throwing parse errors:
- Python f-strings:
f"SELECT * FROM {table_name}"
- Python format strings:
"SELECT * FROM {}".format(table)
- JavaScript template literals:
`SELECT * FROM ${tableName}`
- Jinja2/Mustache templates:
{{ variable }}, {% block %}
- C-style format specifiers:
%s, %d, %(name)s
- Ruby/Perl interpolation:
#{variable}
- String concatenation handling -- basic support for SQL split across concatenated strings.
- 20+ SQL dialects -- MySQL, PostgreSQL, SQLite, SQL Server (T-SQL), PL/SQL, BigQuery, Snowflake, and many more.
- Keyboard shortcut -- Ctrl+Shift+Alt+F (Cmd+Shift+Alt+F on macOS) to format SQL.
- Explorer context menu -- right-click any .sql file in the file explorer to format it.
Usage
- Open a
.sql file.
- Right-click in the editor and select Format with SQL Formatter, or use the keyboard shortcut Ctrl+Shift+Alt+F.
- Alternatively, use VS Code's built-in Format Document (Shift+Alt+F) -- this extension registers as the default formatter for SQL files.
- Right-click a
.sql file in the Explorer panel.
- Select Format with SQL Formatter.
- The file will be opened, formatted, and displayed.
- Select the SQL text in any file (Python, JavaScript, Java, etc.).
- Right-click and choose Format with SQL Formatter.
- The selected SQL will be formatted in place. If the selection includes string delimiters (like
"""...""" or backticks), they will be preserved.
Select the full string including delimiters. For example, in a Python file:
query = f"""SELECT * FROM {schema}.{table} WHERE created_at > {start_date} AND status = %s"""
Select the f"""...""" portion, right-click, and format. The result:
query = f"""
SELECT
*
FROM
{schema}.{table}
WHERE
created_at > {start_date}
AND status = %s
"""
Configuration
All settings are under the sqlFormatter namespace. Open VS Code Settings (Ctrl+,) and search for "SQL Formatter".
| Setting |
Default |
Description |
sqlFormatter.dialect |
sql |
SQL dialect. Options: sql, mysql, postgresql, sqlite, transactsql, plsql, bigquery, snowflake, mariadb, db2, hive, redshift, spark, trino, clickhouse, duckdb, n1ql, singlestoredb, tidb, db2i, tsql |
sqlFormatter.keywordCase |
upper |
Keyword casing: upper, lower, or preserve |
sqlFormatter.dataTypeCase |
preserve |
Data type casing: upper, lower, or preserve |
sqlFormatter.functionCase |
preserve |
Function name casing: upper, lower, or preserve |
sqlFormatter.tabWidth |
2 |
Spaces per indentation level (1-8) |
sqlFormatter.useTabs |
false |
Use tabs instead of spaces |
sqlFormatter.linesBetweenQueries |
2 |
Blank lines between SQL statements (0-5) |
sqlFormatter.expressionWidth |
50 |
Max characters in parenthesized expressions before line-breaking |
sqlFormatter.logicalOperatorNewline |
before |
Place newline before or after AND/OR operators |
sqlFormatter.denseOperators |
false |
Remove spaces around operators |
sqlFormatter.newlineBeforeSemicolon |
false |
Place semicolons on separate lines |
Example settings.json
{
"sqlFormatter.dialect": "postgresql",
"sqlFormatter.keywordCase": "upper",
"sqlFormatter.tabWidth": 4,
"sqlFormatter.linesBetweenQueries": 1
}
To use this extension as the default formatter for SQL files and enable format-on-save:
{
"[sql]": {
"editor.defaultFormatter": "sqltools.sql-formatter-vscode",
"editor.formatOnSave": true
}
}
Supported SQL Dialects
- Standard SQL
- MySQL / MariaDB / TiDB
- PostgreSQL
- SQLite
- SQL Server (Transact-SQL)
- Oracle PL/SQL
- Google BigQuery
- Snowflake
- Amazon Redshift
- Apache Hive / Spark
- IBM DB2 / DB2i
- Couchbase N1QL
- SingleStoreDB
- Trino / Presto
- ClickHouse
- DuckDB
Edge Cases Handled
- Dynamic string templates: Python f-strings,
.format() placeholders, JS ${} expressions, Jinja2 {{ }} and {% %} blocks, C-style %s/%d specifiers, Ruby #{} interpolation.
- String delimiters: Triple-quoted strings (
""", '''), backticks, regular quotes -- all are detected, stripped for formatting, and restored afterward.
- String prefixes: Python
f, r, b, u prefixes and their combinations are preserved.
- Graceful fallback: If the primary formatter fails on unusual input, a fallback mechanism manually protects placeholders and retries. If all else fails, the original text is returned unchanged.
- Multi-statement SQL: Multiple semicolon-separated statements are formatted with configurable spacing between them.
- Comments: SQL comments (
-- line comments and /* */ block comments) are preserved during formatting.
Commands
| Command |
Description |
Format with SQL Formatter |
Format the current selection or entire .sql document |
Keyboard Shortcuts
| Shortcut |
Description |
| Ctrl+Shift+Alt+F (Cmd+Shift+Alt+F on Mac) |
Format SQL |
| Shift+Alt+F |
Format Document (standard VS Code, uses this extension for .sql files) |
Requirements
License
MIT
| |