Skip to content

Fix(vim): Ctrl+[ leaves insert mode where Mod is Ctrl - #1100

Open
2648782102 wants to merge 1 commit into
ZenNotes:mainfrom
2648782102:codex/fix-vim-ctrl-bracket-escape
Open

2648782102 wants to merge 1 commit into
ZenNotes:mainfrom
2648782102:codex/fix-vim-ctrl-bracket-escape

Conversation

@2648782102

@2648782102 2648782102 commented Nov 20, 2026 •

Copy link
Copy Markdown

Summary

Closes #1059.

This follows the diagnosis and public patch attached to #1059 by @phildubach. defaultKeymap binds Mod-[ to indentLess. On Linux and Windows, Mod is Ctrl, so that binding ran before codemirror-vim saw the key. indentLess reports the key as handled even when there is nothing to dedent, so Ctrl+[ never reached Vim's <C-[> → <Esc> and insert mode stayed active.

The fix defers Mod-[ to the Vim plugin in every Vim mode, using the existing Vim-aware wrappers from #993 (Mod-a) and #803 (Escape), and sets preventDefault: false so the deferred key can fall through to Vim.

Behavior

Platform / mode Mod-[ behavior
Linux / Windows, Vim on Defers to Vim; Ctrl+[ performs <Esc> in insert, normal, and visual modes
macOS, Vim on Cmd-[ remains indentLess; this change does not touch it
Vim off, any platform Remains indentLess; no change

Mod-] decision

I did not change Mod-]. Vim's standard mapping set uses <C-[> for Escape, while Ctrl+] has no corresponding action in this editor. Mod-] is bound to indentMore, so deferring it would remove the symmetric indent shortcut without enabling a Vim action. The asymmetry is intentional: only the chord that collides with Vim is deferred.

Tab / Shift-Tab remain the Vim-friendly indent path, and Mod-] continues to work for indent with Vim on and off.

Tests

  • Added regression tests that mount vimAwareDefaultKeymap before vim(), matching the real editor dispatch order.
  • On unmodified main, the new Ctrl+[ assertions fail with insert mode still active, confirming the reproduction; with the fix they pass.
  • Coverage includes:
    • Ctrl+[ ending insert mode in the dispatched chord test.
    • Mod-[ deferring in normal and visual modes.
    • Cmd-[ still running indentLess on macOS.
    • Ctrl+[ still running indentLess with Vim off.
    • Mod-] still running indentMore with Vim on.

Commands run:

npm run test:run --workspace @zennotes/app-core -- src/lib/cm-vim-default-keymap.test.ts
  → 29 passed

npm run typecheck --workspace @zennotes/app-core
  → passed

npm run build --workspace @zennotes/app-core
  → passed

npm run test:run --workspace @zennotes/app-core
  → 4,944 passed, 8 failed, 1 skipped

The full app-core suite's 8 failures are unrelated to this change: 7 Harper tests cannot load node_modules/harper.js/dist/harper_wasm_bg.wasm, and one CloudSettings assertion fails. I reproduced both by running those two files after temporarily removing this change; they fail identically on untouched main.

There is no separate lint step: CONTRIBUTING.md says so. I checked formatting with the repository's available Prettier, but did not run --write across these files because that reformats the whole file and would obscure this focused patch; the surrounding style is preserved.

Attribution

Risk

The change only affects the Mod-[ binding in Vim mode on platforms where Mod is Ctrl. A user relying on Ctrl+[ as indent while Vim is enabled would lose that shortcut, but that combination conflicts with Vim's expected Escape behavior; Mod-] and Tab still provide indent controls. No merge performed.

@2648782102

Copy link
Copy Markdown
Author

Verification note so nobody has to re-derive it: run the test from the package, cd packages/app-core && npx vitest run src/lib/cm-vim-default-keymap.test.ts (29 passed). Running it from the repo root fails on the @shared/* alias, which is a repo-root config gap and not this branch.

The full packages/app-core suite has 8 unrelated failures on this machine: harper-lint.test.ts cannot find node_modules/harper.js/dist/harper_wasm_bg.wasm, and one CloudSettings backup assertion differs; both reproduce without this change.

This branch has not been deployed

No deployments
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.

Vim mode does not allow using Ctrl-[ for escape

1 participant