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.

fix: <CR> in vim (#3849)

Myles Scolnickb77fc1315fcc
diff --git a/frontend/src/core/codemirror/__tests__/__snapshots__/setup.test.ts.snap b/frontend/src/core/codemirror/__tests__/__snapshots__/setup.test.ts.snap
index 032b0dfb74..a39d346ebd 100644
--- a/frontend/src/core/codemirror/__tests__/__snapshots__/setup.test.ts.snap
+++ b/frontend/src/core/codemirror/__tests__/__snapshots__/setup.test.ts.snap
@@ -173,8 +173,7 @@ exports[`snapshot all duplicate keymaps > vim keymaps 2`] = `
},
{
"key": "Enter",
- "run": "<no name>",
- "shift": "<no name>",
+ "run": "run",
},
],
"Escape": [
diff --git a/frontend/src/core/codemirror/keymaps/keymaps.ts b/frontend/src/core/codemirror/keymaps/keymaps.ts
index ba5f315e92..956a6e4303 100644
--- a/frontend/src/core/codemirror/keymaps/keymaps.ts
+++ b/frontend/src/core/codemirror/keymaps/keymaps.ts
@@ -1,11 +1,12 @@
/* Copyright 2024 Marimo. All rights reserved. */
import type { KeymapConfig } from "@/core/config/config-schema";
import { logNever } from "@/utils/assertNever";
-import { defaultKeymap } from "@codemirror/commands";
+import { defaultKeymap, insertNewlineAndIndent } from "@codemirror/commands";
import { type Extension, Prec } from "@codemirror/state";
import { type EditorView, keymap } from "@codemirror/view";
-import { vim } from "@replit/codemirror-vim";
+import { getCM, vim } from "@replit/codemirror-vim";
import { vimKeymapExtension } from "./vim";
+import { once } from "@/utils/once";
export const KEYMAP_PRESETS = ["default", "vim"] as const;
@@ -34,7 +35,21 @@ export function keymapBundle(
];
case "vim":
return [
- keymap.of(defaultKeymap),
+ // Filter out Enter from defaultKeymap
+ keymap.of(defaultVimKeymap()),
+ // Add Enter back for insert mode only
+ keymap.of([
+ {
+ key: "Enter",
+ run: (view) => {
+ const cm = getCM(view);
+ if (!cm?.state.vim?.insertMode) {
+ return false;
+ }
+ return insertNewlineAndIndent(view);
+ },
+ },
+ ]),
// delete the cell on double press of "d", if the cell is empty
Prec.high(
doubleCharacterListener(
@@ -60,6 +75,18 @@ export function keymapBundle(
}
}
+const defaultVimKeymap = once(() => {
+ const toRemove = new Set(["Enter", "Ctrl-v"]);
+ // Remove conflicting keys from the keymap
+ // Enter (<CR>) adds a new line
+ // - it should just go to the next line
+ // Ctrl-v goes to the bottom of the cell
+ // - should enter blockwise visual mode
+ return defaultKeymap.filter(
+ (k) => !toRemove.has(k.key || k.mac || k.linux || k.win || ""),
+ );
+});
+
/**
* Listen for a double keypress of a character and call a callback.
*/