Skip to content

vim: normal/visual mode consume every unmapped key instead of typing it - #654

Merged
GoKubar merged 1 commit into
mainfrom
agent/kabir-claude/vim-dispatch-policy
Sep 15, 2026
Merged

GoKubar merged 1 commit into
mainfrom
agent/kabir-claude/vim-dispatch-policy

Conversation

@GoKubar

@GoKubar GoKubar commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

What

Fixes the root cause of the owner-reported "vim mode is broadly broken — _ and - type characters rather than doing their vim actions" (and ~56 more keys like them).

handleNormalKey/handleVisualKey returned false for any key they did not map, and CompletingTextView.keyDown then ran super.keyDown — so the key edited the document instead of acting:

  • unmapped printables typed themselves: _ - + | # & ! = @ q K Q R U Z [ ] \ (18 keys)
  • Enter inserted a newline, Backspace deleted a character, Tab inserted an indent
  • unmapped ⌃-chords ran the editor's Cocoa bindings: ⌃K killed to end of line, ⌃O opened a line, ⌃T transposed, ⌃H/⌃W deleted, ⌃Y yanked the kill buffer
  • in visual mode the fallthrough replaced the entire selection with the typed character — including u, U, r, D, C, S, X, I, A, which are all real vim visual commands (~34 keys)

Policy fix

Normal, visual and pending modes now consume every key that reaches the state machine, exactly like Vim: an unmapped key is a no-op that clears pending state. One dispatch-policy change kills all ~58 corruption cases.

Deliberate passthroughs are unchanged:

  • ⌘- and ⌥-chords never reach the state machine (Key.init(event:) declines them), so menu key equivalents — Save/Open/Compile — are untouched; they also ride performKeyEquivalent, which this PR does not touch.
  • Arrows, Home/End, Page keys: still the editor's own.
  • Insert mode still intercepts nothing but Esc/⌃[ (IME, completion, snippets untouched).
  • Tab becomes a real Key so normal/visual can consume it; insert mode still returns false for it, so indent/snippet-placeholder Tab keeps working (regression-tested).
  • Newline characters arriving under a non-Return key code now map to Enter instead of slipping past Vim and inserting.

Missing actions behind these keys (-/+/_ motions, #, ge, macros, registers fixes, …) are follow-up PRs — this one is deliberately the minimal landable policy change.

Tests (additions only — no existing test edited)

Table-driven sweeps in VimModeTests.swift, driven through the real keyDown like the rest of the file:

  • every printable ASCII key in normal mode → buffer unchanged unless the key is a documented single-key editing action (x X s S D C o O J ~ p P)
  • the same sweep over a visual selection (allowlist d x c s J ~ > < p P)
  • all ⌃a–⌃z chords in both modes → buffer unchanged
  • Enter/Backspace/Tab in both modes → buffer unchanged
  • insert-mode Tab still reaches the editor (passthrough regression)

Full Mac gate (/tmp/macgate2.sh, Rust helpers wired) result will be posted as a comment; baseline is 1330 tests / 0 failures.

Coordination note: PR #653 (appearance-overhaul) touches only completion-popup styling in Completion.swift (lines ~1818/1921/1992); this PR's only Completion.swift change is a 3-line comment refresh at ~2831 — no conflict.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Xd5Hmwh5GHNTiAmHUJ1MZu

Normal and visual mode returned false for any key they did not map, and
the editor's keyDown then ran super.keyDown — so ~58 (mode, key) pairs
edited the document instead of acting: unmapped printables (_ - + | # &
! = @ q K Q R U Z [ ] \) typed themselves, Enter inserted a newline,
Backspace deleted, Tab indented, and unmapped ⌃-chords ran Cocoa
bindings (⌃K killed the line, ⌃O opened one, ⌃T transposed, ⌃H/⌃W
deleted). In visual mode the typed character replaced the whole
selection (u, r, U, D, C…).

Policy fix: normal/visual/pending modes now consume every key that
reaches the state machine, as Vim does; unmapped keys are no-ops that
clear pending state. Deliberate passthroughs are unchanged: ⌘- and
⌥-chords, arrows, Home/End/Page never reach the machine (menu shortcuts
keep working), and insert mode still owns nothing but Esc. Tab becomes
a real Key so normal/visual can consume it; insert mode still hands it
to the editor (indent, snippets). Newline characters arriving under
another key code map to Enter instead of slipping past Vim.

Tests: table-driven sweeps over every printable ASCII key in normal and
visual mode, all ⌃a–⌃z chords in both modes, Enter/BS/Tab in both
modes, plus an insert-mode Tab passthrough regression. Only additions
to VimModeTests.swift; no existing test changed.

Implementation-Agent: claude-fable-5 (Claude Code subagent)
Commit-Executor: claude-fable-5 (Claude Code subagent)
Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Xd5Hmwh5GHNTiAmHUJ1MZu
@GoKubar

GoKubar commented Sep 15, 2026

Copy link
Copy Markdown
Contributor Author

Full Mac gate (/tmp/macgate2.sh /private/tmp/vim-dispatch-wt vim-dispatch /private/tmp/ftgate-rc, Rust helpers wired), commit 1974fc8:

### build exit: 0
### test exit: 0
Executed 1335 tests, with 12 tests skipped and 0 failures (0 unexpected) in 253.150s

Baseline is 1330/0; the +5 are this PR's new sweep tests. No existing test edited.

@GoKubar
GoKubar merged commit 7e61f65 into main Sep 15, 2026
20 of 21 checks passed
@GoKubar
GoKubar deleted the agent/kabir-claude/vim-dispatch-policy branch September 15, 2026 22:39
@GoKubar
GoKubar restored the agent/kabir-claude/vim-dispatch-policy branch September 15, 2026 22:42
@GoKubar
GoKubar deleted the agent/kabir-claude/vim-dispatch-policy branch September 15, 2026 22:43
GoKubar added a commit that referenced this pull request Sep 15, 2026
GoKubar added a commit that referenced this pull request Sep 15, 2026
GoKubar added a commit that referenced this pull request Sep 15, 2026
GoKubar added a commit that referenced this pull request Sep 16, 2026
column-aligned paste, $ ragged-right, gu/gU/g~

The last item from the Vim-parity gap list. A new Mode case with its own
key handler (handleVisualBlockKey) rather than threading block-awareness
into the existing charwise/linewise handleVisualKey — the two selection
models are different enough (a single NSRange vs. a rectangle) that
keeping them separate was safer than risking the well-tested existing
modes. Turned out not to need any changes in Completion.swift after
all: nothing there switches exhaustively over VimMode.Mode, and the
block/line caret width logic already generalizes via `mode != .insert`.

- Entry: C-v from normal mode or from visual/visualLine (toggling
  between the three, like v/V already do); Esc or C-v again exits.
- The block's bounds are computed live from `visualAnchor` and `caret`
  (never from `textView.selectedRange()`, which only reports the first
  of the block's several row ranges) — rendered via
  `setSelectedRanges`, one NSRange per touched line.
- `I`/`A`: open an insert session on the block's first line at its left
  column / right column + 1 (padding short lines with spaces for `A`),
  then replicate what was typed onto every other line once the session
  closes. `I` skips a line too short to reach the column; `A` pads it.
- `d`/`x`/`y`/`c`/`s`: delete and/or yank each line's column span into a
  new blockwise register (`Register.blockLines`); `c`/`s` chain into a
  block insert at the (now empty) left column.
- `p`/`P`: a blockwise register pastes column-aligned, one register line
  per buffer line, padding short lines and extending past the end of
  the buffer as needed — from both block mode's own p/P and normal-
  mode p/P (a block yank naturally followed by leaving to paste
  elsewhere is the common case, not pasting back over the same
  selection).
- `$`: ragged-right — every line extends to its own end instead of a
  fixed right column, for `d`/`y`/`A`/etc.
- `gu`/`gU`/`g~`: case-map each line's column span.

Known simplifications, documented at their definitions:
- Block-mode `p`/`P` pastes alongside the current selection rather than
  replacing it first (unlike Vim's own visual-mode paste).
- `:`, `/`, `?`, marks-jump (`` ` ``/`'`) aren't wired from block mode;
  they fall through to the same safe no-op every other unmapped key
  gets, rather than corrupting anything.
- Blockwise delete/yank don't participate in the numbered-register
  chain (`"1`-`"9`/`"-`) the way charwise/linewise deletes do.

Two real bugs found and fixed while building this (both while tracing
through the exact index math for the tests, not caught by "it compiles"):
- `moveBlockHead` cleared `preferredColumn` on every call, unlike
  `move()` (which only clears it for non-vertical motions) — would have
  broken column preservation extending a block down through a short
  line and back out to a longer one.
- `leaveInsert`'s final caret placement read the *live* caret after
  `replicateBlockInsert` ran, which moves the text view's selection to
  wherever it last inserted (a later line of the block) — captured the
  return position before replication instead, since the block session's
  own (first) line is never itself touched by the replication.

Tests: entry/exit and mode toggling, a rectangular delete, yank then a
column-aligned normal-mode paste, block I with replication, block A
with short-line padding, $ ragged-right delete, and a full-ASCII sweep
(mirroring the normal/visual-mode ones from #654) proving every
unmapped key in block mode is a safe no-op rather than an edit or an
escape.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Xd5Hmwh5GHNTiAmHUJ1MZu
GoKubar added a commit that referenced this pull request Sep 16, 2026
column-aligned paste, $ ragged-right, gu/gU/g~

The last item from the Vim-parity gap list. A new Mode case with its own
key handler (handleVisualBlockKey) rather than threading block-awareness
into the existing charwise/linewise handleVisualKey — the two selection
models are different enough (a single NSRange vs. a rectangle) that
keeping them separate was safer than risking the well-tested existing
modes. Turned out not to need any changes in Completion.swift after
all: nothing there switches exhaustively over VimMode.Mode, and the
block/line caret width logic already generalizes via `mode != .insert`.

- Entry: C-v from normal mode or from visual/visualLine (toggling
  between the three, like v/V already do); Esc or C-v again exits.
- The block's bounds are computed live from `visualAnchor` and `caret`
  (never from `textView.selectedRange()`, which only reports the first
  of the block's several row ranges) — rendered via
  `setSelectedRanges`, one NSRange per touched line.
- `I`/`A`: open an insert session on the block's first line at its left
  column / right column + 1 (padding short lines with spaces for `A`),
  then replicate what was typed onto every other line once the session
  closes. `I` skips a line too short to reach the column; `A` pads it.
- `d`/`x`/`y`/`c`/`s`: delete and/or yank each line's column span into a
  new blockwise register (`Register.blockLines`); `c`/`s` chain into a
  block insert at the (now empty) left column.
- `p`/`P`: a blockwise register pastes column-aligned, one register line
  per buffer line, padding short lines and extending past the end of
  the buffer as needed — from both block mode's own p/P and normal-
  mode p/P (a block yank naturally followed by leaving to paste
  elsewhere is the common case, not pasting back over the same
  selection).
- `$`: ragged-right — every line extends to its own end instead of a
  fixed right column, for `d`/`y`/`A`/etc.
- `gu`/`gU`/`g~`: case-map each line's column span.

Known simplifications, documented at their definitions:
- Block-mode `p`/`P` pastes alongside the current selection rather than
  replacing it first (unlike Vim's own visual-mode paste).
- `:`, `/`, `?`, marks-jump (`` ` ``/`'`) aren't wired from block mode;
  they fall through to the same safe no-op every other unmapped key
  gets, rather than corrupting anything.
- Blockwise delete/yank don't participate in the numbered-register
  chain (`"1`-`"9`/`"-`) the way charwise/linewise deletes do.

Two real bugs found and fixed while building this (both while tracing
through the exact index math for the tests, not caught by "it compiles"):
- `moveBlockHead` cleared `preferredColumn` on every call, unlike
  `move()` (which only clears it for non-vertical motions) — would have
  broken column preservation extending a block down through a short
  line and back out to a longer one.
- `leaveInsert`'s final caret placement read the *live* caret after
  `replicateBlockInsert` ran, which moves the text view's selection to
  wherever it last inserted (a later line of the block) — captured the
  return position before replication instead, since the block session's
  own (first) line is never itself touched by the replication.

Tests: entry/exit and mode toggling, a rectangular delete, yank then a
column-aligned normal-mode paste, block I with replication, block A
with short-line padding, $ ragged-right delete, and a full-ASCII sweep
(mirroring the normal/visual-mode ones from #654) proving every
unmapped key in block mode is a safe no-op rather than an edit or an
escape.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Xd5Hmwh5GHNTiAmHUJ1MZu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant