Editing & Formatting SQL¶
This section covers the experimental formatting capabilities and the tree-sitter based editing features available in the plugin.
Formatting¶
The plugin provides built-in SQL formatting functionality via Tree-sitter.
:[N]TDF: Reformat the current statement plus the nextN-1statements.:TDFF: Reformat all statements in the current buffer.
Note on experimental status:
:TDF/:TDFFare experimental. They work well for common DML/DDL but may reorder comments or adjust indentation on complex constructs. Always inspect diffs before committing.
Formatting Rules¶
The formatter adheres to the following conventions to ensure consistency:
- UPPERCASE: Database names, table names, field names, and field aliases.
- lowercase: All other SQL keywords and syntax.
Operations on Parse Elements (OPE)¶
"OPE" refers to the suite of node-aware editing functions exposed by vim-teradata. They target a Tree-sitter node type and act on the smallest enclosing node of that type around the cursor.
Available Functions¶
| Function | Description |
|---|---|
comment_node({type}) |
Comment out the node (-- prefix per line) |
uncomment_node() |
Remove -- prefixes inside the node under the cursor |
delete_node({type}) |
Delete the smallest enclosing node of {type} |
copy_node({type}) |
Yank the smallest enclosing node of {type} to the unnamed register |
jump_to_next({type}) |
Move cursor to the start of the next node of {type} |
jump_to_prev({type}) |
Move cursor to the start of the previous node of {type} |
format_current_statement() |
Formats statement under cursor (equivalent to :TDF) |
format_all_statements() |
Formats all statements in buffer (equivalent to :TDFF) |
If a targeted node type does not exist at the cursor position, the operation is a safe no-op and emits a warning via vim.notify().
Common Node Types¶
| Node Type | Matches |
|---|---|
statement |
One complete SQL statement |
select_expression |
The SELECT projection list |
where |
The WHERE clause |
term |
A single field or expression in a list |
binary_expression |
An a OP b comparison / predicate |
cte |
A single Common Table Expression in a WITH clause |
Finding Node Types
To inspect the Tree-sitter AST and discover the exact node type under your cursor, run the built-in Neovim command: :InspectTree
Configuration¶
Define your preferred keybindings in keymaps within setup():
keymaps = {
["gcu"] = "uncomment_node",
["gcq"] = { "comment_node", { "statement" } },
["gcs"] = { "comment_node", { "select_expression" } },
["gcw"] = { "comment_node", { "where" } },
["gcf"] = { "comment_node", { "term" } },
["gcb"] = { "comment_node", { "binary_expression" } },
["gdq"] = { "delete_node", { "statement" } },
["gds"] = { "delete_node", { "select_expression" } },
["gdw"] = { "delete_node", { "where" } },
["gdf"] = { "delete_node", { "term" } },
["gdb"] = { "delete_node", { "binary_expression" } },
["grs"] = { "copy_node", { "select_expression" } },
["]s"] = { "jump_to_next", { "statement" } },
["[s"] = { "jump_to_prev", { "statement" } },
}