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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
Use `Shift+Escape` for Command Mode on Windows with vim bindings (#6004)
Trevor Manze732b745d512
diff --git a/docs/guides/editor_features/overview.md b/docs/guides/editor_features/overview.mdindex 6e304a3b1e..edbe076685 100644--- a/docs/guides/editor_features/overview.md+++ b/docs/guides/editor_features/overview.md@@ -44,7 +44,7 @@ editing their contents.**Enter/Exit:**-- Enter command mode: `Esc` (from cell editor) or `Ctrl+Esc`/`Cmd+Esc` (when vim keybindings are enabled)+- Enter command mode: `Esc` (from cell editor) or `Ctrl+Esc`/`Cmd+Esc` (when vim keybindings are enabled, `Shift+Esc` on Windows)- Exit command mode: `Enter` or click on a cell**Shortcuts:**@@ -63,7 +63,7 @@ When [vim keybindings](#vim-keybindings) are enabled, additional shortcuts are a### Vim keybindingsmarimo supports vim keybindings that extend to notebook editing. Within cells,-use standard vim modes. Press `Ctrl+Esc` (or `Cmd+Esc` on macOS) from normal mode to enter [command+use standard vim modes. Press `Ctrl+Esc` (or `Cmd+Esc` on macOS, `Shift+Esc` on Windows) from normal mode to enter [commandmode](#command-mode) for notebook navigation.**Cell editing additions:**@@ -96,7 +96,7 @@ vimrc = relative/path/.vimrc**Command mode additions:**-When vim keybindings are enabled, press `Ctrl+Esc` (or `Cmd+Esc` on macOS) from normal mode to enter+When vim keybindings are enabled, press `Ctrl+Esc` (or `Cmd+Esc` on macOS, `Shift+Esc` on Windows) from normal mode to enter[command mode](#command-mode) with additional vim-specific keybindings:- `j`/`k` - navigate cellsdiff --git a/frontend/src/components/editor/navigation/__tests__/navigation.test.ts b/frontend/src/components/editor/navigation/__tests__/navigation.test.tsindex 8d89bbc93e..8b2affef1d 100644--- a/frontend/src/components/editor/navigation/__tests__/navigation.test.ts+++ b/frontend/src/components/editor/navigation/__tests__/navigation.test.ts@@ -5,7 +5,7 @@ import type { EditorView } from "@codemirror/view";import { act, renderHook } from "@testing-library/react";import { Provider } from "jotai";import React, { createRef } from "react";-import { beforeEach, describe, expect, it, vi } from "vitest";+import { afterAll, beforeEach, describe, expect, it, vi } from "vitest";import { Mocks } from "@/__mocks__/common";import { MockNotebook } from "@/__mocks__/notebook";import { MockRequestClient } from "@/__mocks__/requests";@@ -64,6 +64,10 @@ const mockUseCellClipboard = vi.mocked(await import("../clipboard"),).useCellClipboard;+afterAll(() => {+ vi.resetAllMocks();+});+import { defaultUserConfig } from "@/core/config/config-schema";import { MultiColumn } from "@/utils/id-tree";import { focusCell, focusCellEditor } from "../focus-utils";@@ -1580,12 +1584,22 @@ describe("useCellNavigationProps", () => {});});+// Mock isPlatformWindows for testing+const mockIsPlatformWindows = vi.hoisted(() => vi.fn());+vi.mock("@/core/hotkeys/shortcuts", async (importOriginal) => ({+ ...(await importOriginal()),+ isPlatformWindows: mockIsPlatformWindows,+}));+describe("useCellEditorNavigationProps", () => {beforeEach(() => {vi.clearAllMocks();// Reset config overridesstore.set(configOverridesAtom, {});++ // Reset platform mock to default (non-Windows)+ mockIsPlatformWindows.mockReturnValue(false);});describe("keyboard shortcuts", () => {@@ -1692,52 +1706,115 @@ describe("useCellEditorNavigationProps", () => {});});- it("should focus cell when Ctrl+Escape is pressed in vim mode", () => {- const mockEditorView = { current: null };- const { result } = renderWithProvider(() =>- useCellEditorNavigationProps(mockCellId, mockEditorView),- );+ describe("non-Windows platforms", () => {+ beforeEach(() => {+ mockIsPlatformWindows.mockReturnValue(false);+ });- const mockEvent = Mocks.keyboardEvent({ key: "Escape", ctrlKey: true });+ it("should focus cell when Ctrl+Escape is pressed in vim mode", () => {+ const mockEditorView = { current: null };+ const { result } = renderWithProvider(() =>+ useCellEditorNavigationProps(mockCellId, mockEditorView),+ );- act(() => {- result.current.onKeyDown?.(mockEvent);+ const mockEvent = Mocks.keyboardEvent({ key: "Escape", ctrlKey: true });++ act(() => {+ result.current.onKeyDown?.(mockEvent);+ });++ expect(focusCell).toHaveBeenCalledWith(mockCellId);+ expect(mockEvent.continuePropagation).not.toHaveBeenCalled();});- expect(focusCell).toHaveBeenCalledWith(mockCellId);- expect(mockEvent.continuePropagation).not.toHaveBeenCalled();- });+ it("should focus cell when Cmd+Escape (metaKey) is pressed in vim mode", () => {+ const mockEditorView = { current: null };+ const { result } = renderWithProvider(() =>+ useCellEditorNavigationProps(mockCellId, mockEditorView),+ );- it("should focus cell when Cmd+Escape (metaKey) is pressed in vim mode", () => {- const mockEditorView = { current: null };- const { result } = renderWithProvider(() =>- useCellEditorNavigationProps(mockCellId, mockEditorView),- );+ const mockEvent = Mocks.keyboardEvent({ key: "Escape", metaKey: true });- const mockEvent = Mocks.keyboardEvent({ key: "Escape", metaKey: true });+ act(() => {+ result.current.onKeyDown?.(mockEvent);+ });- act(() => {- result.current.onKeyDown?.(mockEvent);+ expect(focusCell).toHaveBeenCalledWith(mockCellId);+ expect(mockEvent.continuePropagation).not.toHaveBeenCalled();});- expect(focusCell).toHaveBeenCalledWith(mockCellId);- expect(mockEvent.continuePropagation).not.toHaveBeenCalled();+ it("should focus cell when Escape (without Ctrl) is pressed in vim mode", () => {+ const mockEditorView = { current: null };+ const { result } = renderWithProvider(() =>+ useCellEditorNavigationProps(mockCellId, mockEditorView),+ );++ const mockEvent = Mocks.keyboardEvent({ key: "Escape" });++ act(() => {+ result.current.onKeyDown?.(mockEvent);+ });++ expect(focusCell).toHaveBeenCalledWith(mockCellId);+ expect(mockEvent.continuePropagation).not.toHaveBeenCalled();+ });});- it("should not focus cell when Escape (without Ctrl) is pressed in vim mode", () => {- const mockEditorView = { current: null };- const { result } = renderWithProvider(() =>- useCellEditorNavigationProps(mockCellId, mockEditorView),- );+ describe("Windows platform", () => {+ beforeEach(() => {+ mockIsPlatformWindows.mockReturnValue(true);+ });- const mockEvent = Mocks.keyboardEvent({ key: "Escape" });+ it("should focus cell when Shift+Escape is pressed in vim mode on Windows", () => {+ const mockEditorView = { current: null };+ const { result } = renderWithProvider(() =>+ useCellEditorNavigationProps(mockCellId, mockEditorView),+ );- act(() => {- result.current.onKeyDown?.(mockEvent);+ const mockEvent = Mocks.keyboardEvent({+ key: "Escape",+ shiftKey: true,+ });++ act(() => {+ result.current.onKeyDown?.(mockEvent);+ });++ expect(focusCell).toHaveBeenCalledWith(mockCellId);+ expect(mockEvent.continuePropagation).not.toHaveBeenCalled();});- expect(focusCell).not.toHaveBeenCalled();- expect(mockEvent.continuePropagation).not.toHaveBeenCalled();+ it("should focus cell when Ctrl+Escape is pressed in vim mode on Windows", () => {+ const mockEditorView = { current: null };+ const { result } = renderWithProvider(() =>+ useCellEditorNavigationProps(mockCellId, mockEditorView),+ );++ const mockEvent = Mocks.keyboardEvent({ key: "Escape", ctrlKey: true });++ act(() => {+ result.current.onKeyDown?.(mockEvent);+ });++ expect(focusCell).toHaveBeenCalledWith(mockCellId);+ expect(mockEvent.continuePropagation).not.toHaveBeenCalled();+ });++ it("should focus cell when Escape (without Shift) is pressed in vim mode on Windows", () => {+ const mockEditorView = { current: null };+ const { result } = renderWithProvider(() =>+ useCellEditorNavigationProps(mockCellId, mockEditorView),+ );++ const mockEvent = Mocks.keyboardEvent({ key: "Escape" });++ act(() => {+ result.current.onKeyDown?.(mockEvent);+ });++ expect(focusCell).toHaveBeenCalledWith(mockCellId);+ expect(mockEvent.continuePropagation).not.toHaveBeenCalled();+ });});});});diff --git a/frontend/src/components/editor/navigation/navigation.ts b/frontend/src/components/editor/navigation/navigation.tsindex c4f1551721..e9447d32bd 100644--- a/frontend/src/components/editor/navigation/navigation.ts+++ b/frontend/src/components/editor/navigation/navigation.ts@@ -4,6 +4,7 @@ import { closeCompletion, completionStatus } from "@codemirror/autocomplete";import { EditorSelection } from "@codemirror/state";import type { EditorView } from "@codemirror/view";import { useAtomValue, useSetAtom, useStore } from "jotai";+import { useMemo } from "react";import { mergeProps, useFocusWithin, useKeyboard } from "react-aria";import { aiCompletionCellAtom } from "@/core/ai/state";import { cellIdsAtom, notebookAtom, useCellActions } from "@/core/cells/cells";@@ -609,6 +610,12 @@ export function useCellEditorNavigationProps() {const setTemporarilyShownCode = useSetAtom(temporarilyShownCodeAtom);const keymapPreset = useAtomValue(keymapPresetAtom);+ const hotkeys = useAtomValue(hotkeysAtom);++ const vimCommandModeShortcut = useMemo(() => {+ const shortcut = hotkeys.getHotkey("command.vimEnterCommandMode");+ return parseShortcut(shortcut.key);+ }, [hotkeys]);const exitToCommandMode = () => {setTemporarilyShownCode(false);@@ -648,18 +655,13 @@ export function useCellEditorNavigationProps(const { keyboardProps } = useKeyboard({onKeyDown: (evt) => {- // For vim mode, require Ctrl+Escape (or Cmd+Escape on Mac) to exit to command mode- if (keymapPreset === "vim") {- if (evt.key === "Escape" && (evt.ctrlKey || evt.metaKey)) {- handleEscape();- }- } else {+ // For vim mode, use configurable shortcut+ if (keymapPreset === "vim" && vimCommandModeShortcut(evt)) {+ handleEscape();+ } else if (evt.key === "Escape") {// For non-vim mode, regular Escape exits to command mode- if (evt.key === "Escape") {- handleEscape();- }+ handleEscape();}-evt.continuePropagation();},});diff --git a/frontend/src/core/hotkeys/hotkeys.ts b/frontend/src/core/hotkeys/hotkeys.tsindex 614ef0a7b6..840f30c543 100644--- a/frontend/src/core/hotkeys/hotkeys.ts+++ b/frontend/src/core/hotkeys/hotkeys.ts@@ -388,6 +388,14 @@ const DEFAULT_HOT_KEY = {},// Command mode (edit a cell, not the editor)+ "command.vimEnterCommandMode": {+ name: "Enter command mode (vim)",+ group: "Command",+ key: {+ main: "Mod-Escape",+ windows: "Shift-Escape",+ },+ },"command.createCellBefore": {name: "Create a cell before current cell",group: "Command",