A lightweight, high-performance SQL formatter plugin for Neovim that leverages sqlparse
for optimal formatting results with a Lua fallback for basic formatting.
- π High Performance: Uses external
sqlparse
formatter for speed - π§ Configurable: Extensive customization options
- π Format on Save: Automatic formatting when saving files
- β¨οΈ Key Bindings: Convenient shortcuts for formatting
- π― Multi-dialect: Support for PostgreSQL, MySQL, SQLite, and more
- π Fallback: Pure Lua formatter when external tools unavailable
- π Range Formatting: Format selected text only
- Neovim >= 0.8.0
- Optional:
sqlparse
for optimal performance (pip install sqlparse
)
For optimal performance, install sqlparse
:
pip install sqlparse
{
"andev0x/sql-formatter.nvim",
ft = { "sql", "mysql", "plsql", "pgsql" },
config = function()
vim.g.sqlformat_command = "sqlformat"
vim.g.sqlformat_options = "-r -k upper"
vim.g.sqlformat_prog = "sqlformat"
end,
},
use {
"andev0x/sql-formatter.nvim",
ft = { "sql", "mysql", "plsql", "pgsql" },
config = function()
require("sql-formatter").setup()
end,
}
Plug 'andev0x/sql-formatter.nvim'
require("sql-formatter").setup()
require("sql-formatter").setup({
-- Core settings
format_on_save = true,
dialect = "postgresql",
-- Indentation
indent = " ",
tab_width = 2,
use_tabs = false,
-- Case formatting
uppercase = true,
identifier_case = "lower",
function_case = "upper",
datatype_case = "upper",
-- Layout
lines_between_queries = 2,
max_column_length = 80,
comma_start = false,
operator_padding = true,
-- File types
filetypes = { "sql", "mysql", "plsql", "pgsql" },
-- Key bindings
keybindings = {
format_buffer = "<leader>sf",
format_selection = "<leader>ss",
toggle_format_on_save = "<leader>st",
},
-- External formatter (sqlparse)
external_formatter = {
enabled = true,
command = "sqlformat",
args = {
"--reindent",
"--keywords", "upper",
"--identifiers", "lower",
"--strip-comments",
"-"
}
},
-- Notifications
notify = {
enabled = true,
level = "info",
timeout = 2000,
},
})
:SQLFormat
- Format entire buffer:SQLFormatRange
- Format selected lines (visual mode):SQLFormatToggle
- Toggle format-on-save for current buffer:SQLFormatInfo
- Show formatter information
<leader>sf
- Format buffer<leader>ss
- Format selection (visual mode)<leader>st
- Toggle format-on-save
Before:
select u.id,u.name,p.title from users u left join posts p on u.id=p.user_id where u.active=1 and p.published=true order by u.created_at desc;
After:
SELECT
u.id,
u.name,
p.title
FROM users u
LEFT JOIN posts p
ON u.id = p.user_id
WHERE u.active = 1
AND p.published = TRUE
ORDER BY u.created_at DESC;
This plugin prioritizes performance by:
- External Formatter: Uses
sqlparse
(Python) for heavy lifting - Lua Fallback: Lightweight Lua formatter when external tools unavailable
- Lazy Loading: Only loads for SQL file types
- Minimal Dependencies: Pure Lua implementation with optional external tools
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'feat: add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
MIT License - see LICENSE file for details.
anvndev (@andev0x)
If you find this plugin useful, consider sponsoring the development.