> ## Documentation Index
> Fetch the complete documentation index at: https://ngquct-refactor-theme-settings-panes.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Vim Mode

> Vim motions, text objects, registers, marks, and search inside the SQL editor

Vim mode is scoped to the SQL editor. The data grid, the find field and every other control in the window keep their own keys, and a `Cmd` chord passes through in any mode, so `Cmd+Enter` still runs the query.

Turn it on in **Settings > Editor > Vim mode**, which is also the only way to turn it off: there is no `:set`. A badge next to the editor shows the current mode.

<Frame caption="Mode badge in the SQL editor">
  <img className="block dark:hidden" src="https://mintcdn.com/ngquct-refactor-theme-settings-panes/dyqusW_0EzzZl7ZX/images/vim-mode-badge.png?fit=max&auto=format&n=dyqusW_0EzzZl7ZX&q=85&s=395e0c852895bacba2f11e64374e302f" alt="Vim mode badge" width="1560" height="960" data-path="images/vim-mode-badge.png" />

  <img className="hidden dark:block" src="https://mintcdn.com/ngquct-refactor-theme-settings-panes/dyqusW_0EzzZl7ZX/images/vim-mode-badge-dark.png?fit=max&auto=format&n=dyqusW_0EzzZl7ZX&q=85&s=63b61f3de5f02f794c6ad0a4f1c81482" alt="Vim mode badge" width="1560" height="960" data-path="images/vim-mode-badge-dark.png" />
</Frame>

## Modes

| Mode         | How to enter                                           |
| ------------ | ------------------------------------------------------ |
| Normal       | `Esc`, `Ctrl+[`                                        |
| Insert       | `i`, `I`, `a`, `A`, `o`, `O`, `s`, `S`, `c`, `C`, `gi` |
| Replace      | `R`                                                    |
| Visual       | `v`                                                    |
| Visual Line  | `V`                                                    |
| Command line | `:`, `/`, `?`                                          |

`gi` puts you back where you last left insert.

## Move the cursor

### Characters

| Key             | Goes to                                           |
| --------------- | ------------------------------------------------- |
| `h` `j` `k` `l` | Left, down, up, right. The arrow keys do the same |
| `0`             | Start of line                                     |
| `^` `_`         | First non-blank on line                           |
| `$`             | End of line                                       |

### Words

| Key         | Goes to                             |
| ----------- | ----------------------------------- |
| `w` `b` `e` | Next word, prev word, word end      |
| `W` `B` `E` | Same, whitespace-only word boundary |
| `ge` `gE`   | Prev word end, prev WORD end        |

### Lines

| Key         | Goes to                                                                      |
| ----------- | ---------------------------------------------------------------------------- |
| `gg`        | First line                                                                   |
| `G`         | Last line                                                                    |
| `5G`        | Line 5                                                                       |
| `H` `M` `L` | Top, middle, bottom of view                                                  |
| `gj` `gk`   | The same lines as `j` `k`. Wrapped display lines have no motion of their own |
| `%`         | Matching `()`, `[]`, `{}`                                                    |

### Find a character

| Key     | Goes to                             |
| ------- | ----------------------------------- |
| `f{c}`  | Next `{c}` on this line             |
| `F{c}`  | Prev `{c}` on this line             |
| `t{c}`  | Just before next `{c}`              |
| `T{c}`  | Just after prev `{c}`               |
| `;` `,` | Repeat last find, reverse last find |

### Sentences and paragraphs

| Key       | Goes to                              |
| --------- | ------------------------------------ |
| `(` `)`   | Prev sentence, next sentence         |
| `{` `}`   | Prev blank line, next blank line     |
| `[[` `]]` | Prev `{`-at-col-0, next `{`-at-col-0 |

Counts work: `3w` moves three words, `5j` moves five lines down.

## Edit

| Key                     | Action                                                            |
| ----------------------- | ----------------------------------------------------------------- |
| `d{m}`                  | Delete (with motion)                                              |
| `c{m}`                  | Change (delete then enter insert)                                 |
| `y{m}`                  | Yank                                                              |
| `dd` `cc` `yy`          | Same on the whole line                                            |
| `D` `C` `Y`             | Same as `d$`, `c$`, `yy`                                          |
| `x` `X`                 | Delete one char under, one before cursor. `Forward Delete` is `x` |
| `s` `S`                 | Replace one char or whole line, then insert                       |
| `r{c}`                  | Replace one char with `{c}`, stay in normal                       |
| `~`                     | Toggle case under cursor                                          |
| `g~{m}` `gu{m}` `gU{m}` | Toggle, lower, upper with motion                                  |
| `g~~` `guu` `gUU`       | Same on the whole line                                            |
| `>>` `<<`               | Indent, outdent line                                              |
| `>{m}` `<{m}`           | Indent, outdent with motion                                       |
| `J`                     | Join next line with a space                                       |
| `gJ`                    | Join next line, no space                                          |

`2d3w` deletes six words. `3yy` yanks three lines. `5>>` indents five lines.

Yank, delete, and change all write to the system clipboard.

## Text objects

Use after `d`, `c`, `y`, or in visual mode.

| Object                 | Inside             | Around             |
| ---------------------- | ------------------ | ------------------ |
| Word                   | `iw`               | `aw`               |
| WORD                   | `iW`               | `aW`               |
| `"` `'` `` ` `` string | `i"` `i'` `` i` `` | `a"` `a'` `` a` `` |
| `(` `)` parens         | `i(` `ib`          | `a(` `ab`          |
| `{` `}` braces         | `i{` `iB`          | `a{` `aB`          |
| `[` `]` brackets       | `i[`               | `a[`               |
| `<` `>` angles         | `i<`               | `a<`               |
| HTML tag               | `it`               | `at`               |
| Paragraph              | `ip`               | `ap`               |

`ciw` rewrites the current word. `da"` removes a quoted string and its trailing space. `yip` copies the current paragraph.

## Paste

| Key  | Action                                             |
| ---- | -------------------------------------------------- |
| `p`  | After cursor (or below the line for linewise yank) |
| `P`  | Before cursor (or above the line)                  |
| `3p` | Paste three times                                  |

In visual mode, `p` and `P` replace the selection with the register.

## Registers

Prefix with `"{a-z}` to pick a register.

| Register    | Holds                               |
| ----------- | ----------------------------------- |
| `"`         | Last yank or delete                 |
| `"a` … `"z` | Named, overwrite                    |
| `"A` … `"Z` | Same, append                        |
| `"0`        | Last yank only                      |
| `"1` … `"9` | Delete ring, rotates on each delete |

`"ayy` stores a line in `a`. `"ap` pastes it back later.

## Marks

| Key               | Action                                     |
| ----------------- | ------------------------------------------ |
| `m{a-z}`          | Set mark                                   |
| `` `{a-z} ``      | Jump to exact mark                         |
| `'{a-z}`          | Jump to first non-blank of the mark's line |
| `` `< `` `` `> `` | Start, end of last visual selection        |
| `''` ` ` \`\`     | Back to position before the last jump      |

Marks shift when you insert or delete text before them.

## Search

| Key                | Action                                    |
| ------------------ | ----------------------------------------- |
| `/pattern` `Enter` | Forward search                            |
| `?pattern` `Enter` | Backward search                           |
| `n` `N`            | Next, previous match                      |
| `*` `#`            | Search word under cursor, forward or back |

The pattern is a literal, case-sensitive substring, not a regex. Searches wrap around.

## Repeat and undo

| Key      | Action                      |
| -------- | --------------------------- |
| `.`      | Repeat last change          |
| `u`      | Undo                        |
| `U`      | Undo all edits on this line |
| `Ctrl+R` | Redo                        |

`5.` repeats the last change five times.

## Macros

| Key      | Action                          |
| -------- | ------------------------------- |
| `q{a-z}` | Start recording into a register |
| `q`      | Stop recording                  |
| `@{a-z}` | Play back                       |
| `@@`     | Play back the last macro        |
| `3@a`    | Play three times                |

A macro that calls itself stops after 50 rounds.

## Control keys

Outside insert mode, a `Ctrl` letter goes to vim, never to the editor's own binding:

| Keys                       | Normal mode                                                                  | Visual mode                                           |
| -------------------------- | ---------------------------------------------------------------------------- | ----------------------------------------------------- |
| `Ctrl+D` `Ctrl+U`          | Down, up half the visible lines. With a count, that many lines               | Moves the end of the selection half the visible lines |
| `Ctrl+F` `Ctrl+B`          | Down, up one screen                                                          | Moves the end of the selection one screen             |
| `Ctrl+A` `Ctrl+X`          | Add, subtract 1 from the number under or after the cursor. `5 Ctrl+A` adds 5 | Nothing                                               |
| `Ctrl+R`                   | Redo                                                                         | Nothing                                               |
| `Ctrl+H` `Ctrl+N` `Ctrl+P` | Same as `h`, `j`, `k`                                                        | Same as `h`, `j`, `k`                                 |

Any other `Ctrl` letter does nothing. That includes `Ctrl+E` and `Ctrl+Y`, which like `zt`, `zz` and `zb` are read and discarded: the view scrolls only to follow the cursor. `Ctrl+Space` still opens completions, and `Ctrl+Tab` and `Ctrl+Shift+Tab` still move focus to the next and previous control. A shortcut of `Ctrl` and a letter assigned in **Settings > Keyboard** does not fire from the SQL editor outside insert mode.

In insert and replace mode, vim takes these and leaves every other `Ctrl` combination to the editor:

| Keys              | Action                               |
| ----------------- | ------------------------------------ |
| `Esc` `Ctrl+[`    | Back to normal mode                  |
| `Ctrl+W`          | Delete the word before the cursor    |
| `Ctrl+U`          | Delete back to the start of the line |
| `Ctrl+T` `Ctrl+D` | Indent, outdent the line             |

`Ctrl+H` is the editor's own backspace in insert mode, so it deletes a selection the way `Delete` does. In replace mode vim takes it, and like `Delete` it restores the character you overwrote.

## Option keys

An `Option` chord outside insert mode is read as the character it types. On a keyboard layout where `Option` types `[` or `@`, such as German, those work as vim commands, and a character vim has no use for, such as the non-breaking space from `Option+Space`, does nothing. A dead key such as `Option+E` on the US layout does nothing outside insert mode.

In insert mode, `Option` types as usual. In replace mode, the character it types overwrites the one under the cursor.

## Visual mode

Motions extend the selection. Then:

| Key         | Action                             |
| ----------- | ---------------------------------- |
| `d` `x`     | Delete                             |
| `c`         | Delete and insert                  |
| `y`         | Yank                               |
| `J` `gJ`    | Join lines                         |
| `~` `u` `U` | Toggle, lower, upper case          |
| `r{c}`      | Replace each char with `{c}`       |
| `o`         | Move cursor to the other end       |
| `I` `A`     | Insert at start, end of selection  |
| `gv`        | Reselect the last visual selection |

Indent and outdent work in normal mode only (`>>`, `<<`, `>{m}`, `<{m}`).

## Insert mode

`Esc` or `Ctrl+[` goes back to normal mode. Apart from the insert-mode [control keys](#control-keys), what you type goes to the editor as usual.

## Command line

| Command | Action                |
| ------- | --------------------- |
| `:w`    | Run the current query |
| `:q`    | Close the tab         |

`/` and `?` are search. Every other `:` command is parsed and ignored, `:wq`, `:x` and `:set` among them. `:10` does not jump to line 10 either; use `10G` or `10gg`.

`Delete` and `Ctrl+H` erase the last character of the command. The arrow keys and the other `Ctrl` letters do nothing while you type one.
