Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

Require `Shift+Escape` to enter command mode with vim keybindings (#5815)

Trevor Manz29e3f768566d
diff --git a/frontend/src/components/editor/controls/keyboard-shortcuts.tsx b/frontend/src/components/editor/controls/keyboard-shortcuts.tsx
index e7c07495fe..08baed70db 100644
--- a/frontend/src/components/editor/controls/keyboard-shortcuts.tsx
+++ b/frontend/src/components/editor/controls/keyboard-shortcuts.tsx
@@ -252,7 +252,15 @@ export const KeyboardShortcuts: React.FC = () => {
renderGroup(
group,
<p className="text-xs text-muted-foreground">
- Press <kbd>Esc</kbd> in a cell to enter command mode
+ Press{" "}
+ {config.keymap.preset === "vim" ? (
+ <>
+ <kbd>Shift</kbd>+<kbd>Esc</kbd>
+ </>
+ ) : (
+ <kbd>Esc</kbd>
+ )}{" "}
+ in a cell to enter command mode
</p>,
);
diff --git a/frontend/src/components/editor/navigation/__tests__/navigation.test.ts b/frontend/src/components/editor/navigation/__tests__/navigation.test.ts
index b824445112..97baf95cd7 100644
--- a/frontend/src/components/editor/navigation/__tests__/navigation.test.ts
+++ b/frontend/src/components/editor/navigation/__tests__/navigation.test.ts
@@ -1561,6 +1561,9 @@ describe("useCellNavigationProps", () => {
describe("useCellEditorNavigationProps", () => {
beforeEach(() => {
vi.clearAllMocks();
+
+ // Reset config overrides
+ store.set(configOverridesAtom, {});
});
describe("keyboard shortcuts", () => {
@@ -1594,4 +1597,45 @@ describe("useCellEditorNavigationProps", () => {
expect(mockEvent.continuePropagation).not.toHaveBeenCalled();
});
});
+
+ describe("vim mode", () => {
+ beforeEach(() => {
+ // Set up vim mode in store
+ store.set(configOverridesAtom, {
+ keymap: {
+ preset: "vim",
+ },
+ });
+ });
+
+ it("should focus cell when Shift+Escape is pressed in vim mode", () => {
+ const { result } = renderWithProvider(() =>
+ useCellEditorNavigationProps(mockCellId),
+ );
+
+ const mockEvent = Mocks.keyboardEvent({ key: "Escape", shiftKey: true });
+
+ act(() => {
+ result.current.onKeyDown?.(mockEvent);
+ });
+
+ expect(focusCell).toHaveBeenCalledWith(mockCellId);
+ expect(mockEvent.continuePropagation).not.toHaveBeenCalled();
+ });
+
+ it("should not focus cell when Escape (without Shift) is pressed in vim mode", () => {
+ const { result } = renderWithProvider(() =>
+ useCellEditorNavigationProps(mockCellId),
+ );
+
+ const mockEvent = Mocks.keyboardEvent({ key: "Escape" });
+
+ act(() => {
+ result.current.onKeyDown?.(mockEvent);
+ });
+
+ expect(focusCell).not.toHaveBeenCalled();
+ expect(mockEvent.continuePropagation).not.toHaveBeenCalled();
+ });
+ });
});
diff --git a/frontend/src/components/editor/navigation/navigation.ts b/frontend/src/components/editor/navigation/navigation.ts
index b64a085dbf..a66654a24d 100644
--- a/frontend/src/components/editor/navigation/navigation.ts
+++ b/frontend/src/components/editor/navigation/navigation.ts
@@ -602,12 +602,22 @@ export function useCellNavigationProps(
*/
export function useCellEditorNavigationProps(cellId: CellId) {
const setTemporarilyShownCode = useSetAtom(temporarilyShownCodeAtom);
+ const keymapPreset = useAtomValue(keymapPresetAtom);
const { keyboardProps } = useKeyboard({
onKeyDown: (evt) => {
- if (evt.key === "Escape") {
- setTemporarilyShownCode(false);
- focusCell(cellId);
+ // For vim mode, require Shift+Escape to exit to command mode
+ if (keymapPreset === "vim") {
+ if (evt.key === "Escape" && evt.shiftKey) {
+ setTemporarilyShownCode(false);
+ focusCell(cellId);
+ }
+ } else {
+ // For non-vim mode, regular Escape exits to command mode
+ if (evt.key === "Escape") {
+ setTemporarilyShownCode(false);
+ focusCell(cellId);
+ }
}
evt.continuePropagation();
diff --git a/frontend/src/core/codemirror/keymaps/vim.ts b/frontend/src/core/codemirror/keymaps/vim.ts
index 52a97e7774..8795ce8ea5 100644
--- a/frontend/src/core/codemirror/keymaps/vim.ts
+++ b/frontend/src/core/codemirror/keymaps/vim.ts
@@ -1,7 +1,7 @@
/* Copyright 2024 Marimo. All rights reserved. */
import type { Extension } from "@codemirror/state";
-import { EditorView, keymap, ViewPlugin } from "@codemirror/view";
+import { type EditorView, keymap, ViewPlugin } from "@codemirror/view";
import {
type CodeMirror,
type CodeMirrorV,
@@ -84,31 +84,6 @@ export function vimKeymapExtension(): Extension[] {
},
},
]),
- // Propagate Ctrl+[ to Escape for vim mode
- EditorView.domEventHandlers({
- keydown: (event, view) => {
- if (
- event.ctrlKey &&
- event.key === "[" &&
- !event.shiftKey &&
- !event.altKey &&
- !event.metaKey
- ) {
- event.preventDefault();
- // Dispatch a synthetic Escape event to the editor
- view.contentDOM.dispatchEvent(
- new KeyboardEvent("keydown", {
- key: "Escape",
- code: "Escape",
- bubbles: true,
- cancelable: true,
- }),
- );
- return true;
- }
- return false;
- },
- }),
ViewPlugin.define((view) => {
// Wait for the next animation frame so the CodeMirror instance is ready
requestAnimationFrame(() => {