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.

Also allow `Ctrl` as modifier key on macOS (#7608)

Trevor Manzfb111ab59b9e
diff --git a/frontend/src/core/codemirror/cells/extensions.ts b/frontend/src/core/codemirror/cells/extensions.ts
index c26c49896d..27afefac95 100644
--- a/frontend/src/core/codemirror/cells/extensions.ts
+++ b/frontend/src/core/codemirror/cells/extensions.ts
@@ -7,6 +7,7 @@ import { createTracebackInfoAtom } from "@/core/cells/cells";
import { type CellId, HTMLCellId, SCRATCH_CELL_ID } from "@/core/cells/ids";
import type { KeymapConfig } from "@/core/config/config-schema";
import type { HotkeyProvider } from "@/core/hotkeys/hotkeys";
+import { duplicateWithCtrlModifier } from "@/core/hotkeys/shortcuts";
import { store } from "@/core/state/jotai";
import { createObservable } from "@/core/state/observable";
import { formatKeymapExtension } from "../extensions";
@@ -33,8 +34,9 @@ function cellKeymaps({
}): Extension[] {
const keybindings: KeyBinding[] = [];
+ // Run-related keybindings get Ctrl equivalents on macOS for Jupyter/Colab users
keybindings.push(
- {
+ ...duplicateWithCtrlModifier({
key: hotkeys.getHotkey("cell.run").key,
preventDefault: true,
stopPropagation: true,
@@ -43,8 +45,9 @@ function cellKeymaps({
actions.onRun();
return true;
},
- },
+ }),
{
+ // Shift-Enter has no Cmd, so no Ctrl equivalent needed
key: hotkeys.getHotkey("cell.runAndNewBelow").key,
preventDefault: true,
stopPropagation: true,
@@ -59,7 +62,7 @@ function cellKeymaps({
return true;
},
},
- {
+ ...duplicateWithCtrlModifier({
key: hotkeys.getHotkey("cell.runAndNewAbove").key,
preventDefault: true,
stopPropagation: true,
@@ -73,7 +76,7 @@ function cellKeymaps({
actions.moveToNextCell({ cellId, before: true });
return true;
},
- },
+ }),
{
key: hotkeys.getHotkey("cell.aiCompletion").key,
preventDefault: true,
diff --git a/frontend/src/core/hotkeys/__tests__/shortcuts.test.ts b/frontend/src/core/hotkeys/__tests__/shortcuts.test.ts
index 7e9403279f..3c13a9ec30 100644
--- a/frontend/src/core/hotkeys/__tests__/shortcuts.test.ts
+++ b/frontend/src/core/hotkeys/__tests__/shortcuts.test.ts
@@ -1,6 +1,6 @@
/* Copyright 2026 Marimo. All rights reserved. */
import { describe, expect, it, vi } from "vitest";
-import { parseShortcut } from "../shortcuts";
+import { duplicateWithCtrlModifier, parseShortcut } from "../shortcuts";
describe("parseShortcut", () => {
it("should recognize single key shortcuts", () => {
@@ -21,10 +21,14 @@ describe("parseShortcut", () => {
expect(shortcut(event)).toBe(true);
});
- it("should recognize combined Cmd key shortcuts", () => {
+ it("should recognize combined Cmd key shortcuts with meta or ctrl", () => {
const shortcut = parseShortcut("Cmd-a");
- const event = new KeyboardEvent("keydown", { key: "a", metaKey: true });
- expect(shortcut(event)).toBe(true);
+ // Cmd should accept both metaKey and ctrlKey (like Mod)
+ const metaEvent = new KeyboardEvent("keydown", { key: "a", metaKey: true });
+ expect(shortcut(metaEvent)).toBe(true);
+
+ const ctrlEvent = new KeyboardEvent("keydown", { key: "a", ctrlKey: true });
+ expect(shortcut(ctrlEvent)).toBe(true);
});
it("should recognize Arrow key shortcuts", () => {
@@ -164,3 +168,56 @@ describe("parseShortcut", () => {
expect(parseShortcut("Ctrl+A")(event)).toBe(false);
});
});
+
+describe("duplicateWithCtrlModifier", () => {
+ it("should duplicate Cmd binding with Ctrl variant on macOS", () => {
+ // Mock macOS platform
+ vi.spyOn(window.navigator, "platform", "get").mockReturnValue("MacIntel");
+
+ const binding = { key: "Cmd-Enter", run: () => true };
+ const result = duplicateWithCtrlModifier(binding);
+
+ expect(result).toHaveLength(2);
+ expect(result[0].key).toBe("Cmd-Enter");
+ expect(result[1].key).toBe("Ctrl-Enter");
+
+ vi.restoreAllMocks();
+ });
+
+ it("should not duplicate binding without Cmd", () => {
+ vi.spyOn(window.navigator, "platform", "get").mockReturnValue("MacIntel");
+
+ const binding = { key: "Shift-Enter", run: () => true };
+ const result = duplicateWithCtrlModifier(binding);
+
+ expect(result).toHaveLength(1);
+ expect(result[0].key).toBe("Shift-Enter");
+
+ vi.restoreAllMocks();
+ });
+
+ it("should not duplicate Cmd-Ctrl binding to avoid Ctrl-Ctrl", () => {
+ vi.spyOn(window.navigator, "platform", "get").mockReturnValue("MacIntel");
+
+ const binding = { key: "Cmd-Ctrl-Enter", run: () => true };
+ const result = duplicateWithCtrlModifier(binding);
+
+ // Should NOT create a Ctrl-Ctrl-Enter variant
+ expect(result).toHaveLength(1);
+ expect(result[0].key).toBe("Cmd-Ctrl-Enter");
+
+ vi.restoreAllMocks();
+ });
+
+ it("should not duplicate on non-macOS platforms", () => {
+ vi.spyOn(window.navigator, "platform", "get").mockReturnValue("Win32");
+
+ const binding = { key: "Cmd-Enter", run: () => true };
+ const result = duplicateWithCtrlModifier(binding);
+
+ expect(result).toHaveLength(1);
+ expect(result[0].key).toBe("Cmd-Enter");
+
+ vi.restoreAllMocks();
+ });
+});
diff --git a/frontend/src/core/hotkeys/shortcuts.ts b/frontend/src/core/hotkeys/shortcuts.ts
index 35244f0604..dd5b909645 100644
--- a/frontend/src/core/hotkeys/shortcuts.ts
+++ b/frontend/src/core/hotkeys/shortcuts.ts
@@ -1,5 +1,6 @@
/* Copyright 2026 Marimo. All rights reserved. */
+import type { KeyBinding } from "@codemirror/view";
import { Logger } from "@/utils/Logger";
import { NOT_SET } from "./hotkeys";
@@ -106,8 +107,8 @@ function areKeysPressed(keys: string[], e: IKeyboardEvent): boolean {
function normalizeKey(key: string): string {
const specialKeys: { [key: string]: string } = {
control: "ctrl",
- command: "meta",
- cmd: "meta",
+ command: "mod",
+ cmd: "mod",
option: "alt",
return: "enter",
};
@@ -144,3 +145,34 @@ export function resolvePlatform(): Platform {
}
return "linux";
}
+
+/**
+ * On macOS, duplicate a Cmd-based keybinding to also work with Ctrl.
+ * This allows users coming from Jupyter/Colab to use Ctrl-Enter to run cells.
+ *
+ * Returns an array with the original binding, plus a Ctrl variant on macOS.
+ * For use with CodeMirror keymap bindings.
+ *
+ * Design decision: User-defined Cmd shortcuts also get Ctrl equivalents.
+ * The edge case is if a user wants `Cmd+<x>` and `Ctrl+<x>` to trigger
+ * different actions, this isn't currently supported. Given the relatively
+ * small number of keymaps, we're keeping this simple. If it becomes an issue,
+ * we can refactor to resolve a special "Mod" key internally and require users
+ * to specify explicit single-key mappings.
+ *
+ * Note: If the binding already contains Ctrl (e.g., Cmd-Ctrl-Enter),
+ * no duplication is done to avoid producing invalid Ctrl-Ctrl-key combos.
+ */
+export function duplicateWithCtrlModifier<T extends KeyBinding>(
+ binding: T,
+): T[] {
+ // Skip if not macOS, not a Cmd binding, or already has Ctrl
+ if (
+ !isPlatformMac() ||
+ !binding.key?.includes("Cmd") ||
+ binding.key.includes("Ctrl")
+ ) {
+ return [binding];
+ }
+ return [binding, { ...binding, key: binding.key.replaceAll("Cmd", "Ctrl") }];
+}