Files
dbx/docs/content/docs/sql-snippets.mdx
T

79 lines
3.4 KiB
Plaintext

---
title: SQL Snippets
description: Manage reusable SQL templates, trigger prefixes, and tab-stop placeholders for editor completion.
---
SQL snippets appear as editor completion candidates. Accept a matching trigger to insert the template at the cursor, then use `Tab` and `Shift+Tab` to move through any editable placeholders.
## Manage Snippets
Open **Settings → Snippets**. Each snippet contains:
| Field | Purpose |
| --- | --- |
| Label | Name shown in the completion list. DBX uses the prefix when the label is empty. |
| Prefix | Text that matches the completion candidate. It is required and must be unique in the current snippet list. |
| Status | Enables or disables the snippet without deleting it. |
| SQL Body | Template inserted into the editor when you accept the completion. |
You can add, edit, enable, disable, or delete snippets. **Restore Defaults** replaces the current draft with DBX's built-in snippets; click **Apply** in the settings dialog to persist your changes.
<Callout type="info">
DBX includes common snippets such as `sel`, `ins`, `upd`, `cte`, and `join`. Some built-in templates adapt to the active database, for example by choosing the appropriate row-limit or `ALTER TABLE` syntax. Custom snippets are inserted exactly as written.
</Callout>
## Expand a Snippet
1. Type a prefix such as `sel` in the SQL editor.
2. Confirm the label and body preview in the completion list.
3. Press `Tab`, `Enter`, or click the candidate to accept it.
4. When the template has placeholders, press `Tab` to move forward and `Shift+Tab` to move back.
If no candidate appears, confirm that the snippet is enabled, the prefix matches the current text, and editor autocomplete is enabled.
## Placeholder Syntax
DBX uses CodeMirror snippet syntax:
| Syntax | Purpose |
| --- | --- |
| `${}` | Creates an empty editable field. |
| `${name}` | Creates a field whose default text is `name`; repeated names are edited together. |
| `${1}` | Sets an explicit field order. |
| `${1:defaultText}` | Sets the order and provides default text. |
| `#{name}` | Alternative form of `${name}`. |
Numbered fields follow numeric order. Unnumbered fields follow their textual order in the template. Escape a literal opening or closing brace with a backslash when it must not be parsed as a placeholder.
For example:
```sql
SELECT ${1:columns}
FROM ${2:table}
WHERE ${3:condition}
ORDER BY ${4:column};
```
Reuse a field to edit multiple locations together:
```sql
SELECT ${1:column}, COUNT(*) AS cnt
FROM ${2:table}
GROUP BY ${1:column};
```
## Storage, Sync, and Safety
Snippets are editor settings stored in the current DBX runtime. On Desktop, [Configuration Cloud Sync](/en/docs/cloud-sync) can synchronize snippets with the other editor settings. [Connection Config Migration](/en/docs/config-export) only moves database connections and does not include snippets.
A snippet only generates SQL; it does not bypass database permissions or DBX production protection. Review the fully expanded statement before running templates that contain writes or DDL, and follow the [Production Safety](/en/docs/production-safety) guidance.
<Cards>
<Card title="Query Editor" href="/en/docs/query-editor">
Learn how completion, execution scope, history, and SQL files work together.
</Card>
<Card title="Keyboard Shortcuts" href="/en/docs/keyboard-shortcuts">
Check the current shortcuts for accepting completion, formatting, and execution.
</Card>
</Cards>