β‘ Context-Aware SQL Completion & Execution for Neovim
Reuses the powerful completion and execution engines of pgcli, litecli, and mycli from the dbcli ecosystem.
demo.mp4
- Context-aware completion: Table, column, alias,
JOINcondition, and function suggestions powered bydbcli. blink.cmpintegration: Custom completion source for blink.cmp.- Query execution: Run buffer or visual selections with formatted tabular output (
psql,markdown,vertical, etc.). - Header configuration: Set connection URI and output format directly via file comments (e.g.
-- db: ./app.db).
- Neovim >= 0.10
- Python 3 with one or more of the following backends:
# macOS (Homebrew) β install the backends you need brew install pgcli # PostgreSQL brew install litecli # SQLite brew install mycli # MySQL / MariaDB # or pip / pipx pip install pgcli litecli mycli prompt_toolkit cli_helpers
Using lazy.nvim
{
'widdsun/dbcli.nvim',
ft = 'sql',
opts = {},
}Add to your init.lua:
vim.pack.add('https://github.com/widdsun/dbcli.nvim')
require('dbcli').setup {}To load lazily on SQL files only:
vim.api.nvim_create_autocmd('FileType', {
pattern = 'sql',
once = true,
callback = function()
vim.pack.add('https://github.com/widdsun/dbcli.nvim')
require('dbcli').setup {}
end,
})Add dbcli to your blink.cmp sources:
require('blink.cmp').setup {
sources = {
per_filetype = {
sql = { 'dbcli' },
},
providers = {
dbcli = {
name = 'dbcli',
module = 'dbcli.blink',
score_offset = 100,
},
},
},
}Add a comment at the top (first 15 lines) of your .sql file:
-- db: /path/to/app.sqlite3
SELECT * FROM users;Or for PostgreSQL:
-- db: postgresql://user:password@localhost:5432/mydb
SELECT * FROM orders;:DBConnect /path/to/app.sqlite3
:DBConnect postgresql://user:pass@localhost:5432/mydb| Mode | Keymap / Command | Description |
|---|---|---|
| Normal | <space><enter> |
Execute entire SQL buffer |
| Visual | <space><enter> |
Execute visually selected statements |
| Command | :DBExecute |
Execute entire buffer or range (:'<,'>DBExecute) |
| Command | :DBExecute SELECT 1; |
Execute inline query |
| Command | :DBClose |
Close results window |
| Normal | q |
Close results window |
Switch styles on the fly:
:DBFormat fancy_grid " Tab completion supported!
:DBFormat markdown " Standard Markdown table (alias: github)
:DBFormat vertical " Similar to \G (great for wide rows)
:DBFormat psql " Classic ASCII table (default)require('dbcli').setup {
-- Output table formatting style for query results
table_format = 'psql',
-- Results split window direction ('horizontal' or 'vertical')
split_direction = 'horizontal',
-- Size of the results split window (height in rows or width in columns)
split_size = 15,
-- Automatically bind default keymaps (<space><enter>) in SQL buffers
default_keymaps = true,
-- Fallback database URI/path if no buffer-level database is defined
default_db = nil,
}All 34 formats supported by cli_helpers / dbcli (all available in :DBFormat Tab-completion):
- Database Styles:
psql(default),psql_unicode,mysql,mysql_unicode,mysql_heavy,vertical(expanded \G record view) - Markdown & Markup:
markdown(alias:github),pipe,orgtbl,html,latex,latex_booktabs,rst,mediawiki,textile,moinmoin,jira - Data & Delimited:
csv,csv-noheader,csv-tab,csv-tab-noheader,tsv,tsv_noheader,jsonl,jsonl_escaped - Grids & ASCII:
fancy_grid,grid,double,simple,minimal,plain,ascii,ascii_escaped
dbcli.nvim automatically detects the database type based on the provided path or URI:
| Database Type | Supported URI / Path Patterns | Examples |
|---|---|---|
| SQLite | File path (relative or absolute), sqlite://, sqlite:, :memory: |
app.db./data/test.sqlite3sqlite:///var/data/app.db:memory: |
| PostgreSQL | postgresql:// or postgres:// connection string |
postgresql://user:pass@localhost:5432/mydbpostgres://postgres@127.0.0.1/productionpostgresql://user:pass@remote-host:5432/dbname?sslmode=require |
| MySQL / MariaDB | mysql://, mysql2://, or mariadb:// connection string |
mysql://user:pass@localhost:3306/mydbmariadb://user:pass@localhost:3306/mydb |
dbcli.nvim resolves database connections and table formats in the following order of precedence (highest to lowest):
- Buffer-local variable:
vim.b.db(set via:DBConnect <uri>or Luavim.b.db = ...) vim-dadbod-uibinding:vim.b.db_ui_db_key_name- File Header Comments: First 15 lines of SQL buffer:
-- db: <uri>-- db = <uri>-- database: <uri>-- DB: <uri>-- :DB <uri>
- Global Defaults:
vim.g.dbcli_default_dborvim.g.dboropts.default_db
- Buffer-local variable:
vim.b.dbcli_format(set via:DBFormat <format>) - File Header Comments: First 15 lines of SQL buffer:
-- format: <format>-- mode: <format>-- table_format: <format>
- Global / Plugin Config:
vim.g.dbcli_table_formatoropts.table_format(defaults to'psql')
| Command | Arguments | Description |
|---|---|---|
:DBExecute [query] |
Optional SQL statement | Executes query in argument, selected range (:'<,'>DBExecute), or entire buffer. |
:DBClose |
None | Closes the SQL results window if currently open. |
:DBConnect <uri/path> |
Database URI or path | Binds the current buffer to a SQLite file or PostgreSQL connection. |
:DBDisconnect |
None | Disconnects the current buffer from its bound database. |
:DBFormat [format] |
Optional format name | Gets or sets the table output style for the current buffer (with tab-completion). |
:DBRefresh |
None | Forces a metadata reload (tables, columns, functions) for the current database. |
:DBStatus |
None | Displays active database connections, current buffer bindings, and table format. |
MIT License.