Repository navigation
vim: normal/visual mode consume every unmapped key instead of typing it - #654
Merged
Merged
Conversation
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
Contributor
Author
|
Full Mac gate ( Baseline is 1330/0; the +5 are this PR's new sweep tests. No existing test edited. |
This was referenced Sep 15, 2026
This was referenced 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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/handleVisualKeyreturnedfalsefor any key they did not map, andCompletingTextView.keyDownthen ransuper.keyDown— so the key edited the document instead of acting:_ - + | # & ! = @ q K Q R U Z [ ] \(18 keys)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:
Key.init(event:)declines them), so menu key equivalents — Save/Open/Compile — are untouched; they also rideperformKeyEquivalent, which this PR does not touch.Keyso normal/visual can consume it; insert mode still returns false for it, so indent/snippet-placeholder Tab keeps working (regression-tested).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 realkeyDownlike the rest of the file:x X s S D C o O J ~ p P)d x c s J ~ > < p P)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 inCompletion.swift(lines ~1818/1921/1992); this PR's onlyCompletion.swiftchange is a 3-line comment refresh at ~2831 — no conflict.🤖 Generated with Claude Code
https://claude.ai/code/session_01Xd5Hmwh5GHNTiAmHUJ1MZu