Changing codemirror keymap

I am trying to set my custom keymap to codemirror editor cells from my extension.

this.tracker.activeCellChanged.connect(this.onActiveCellChanged, this);

I’m connecting the following function to activeCellChanged event of INotebookTracker:

   private onActiveCellChanged(): void {
      if (this.tracker.activeCell !== null) {
            const view = (this.tracker.activeCell.editor as CodeMirrorEditor).editor;
            const transaction = view.state.update({
               effects: StateEffect.reconfigure.of(keymap.of(myKeymap))
            })
            view.dispatch(transaction);
         }
      }

Following Codemirror 6 style, I’m creating a transaction to reconfigure editor config and then applying it.

And it actually works, I can now use my keybindings in cells. However it breaks something and output is not updating on new executions.

Either this is a bug, or (most likely) I’m doing something wrong.
Isn’t it a correct way to change a keymap?

Jupyterlab 4.0.5, Windows 11.

Not sure if this helps, but I would recomment to use IEditor.injectExtension or IEditorExtensionRegistry.addExtension to extend CodeMirror rather than applying individual transactions on bare editor.

We also add some hard-coded (sic) keymap, you might be overriding it:

1 Like

Thanks. Neither of these work, however. Seems like we can’t override default keybindings by adding extensions. Ended up prepending defaultKeymap with myKeymap in here:
https://github.com/jupyterlab/jupyterlab/blob/97f1c6f957f272113dce8157318d74c4fb23c289/packages/codemirror/src/extension.ts#L686-L693

1 Like

It really depends on which exact keybindings you want to override, but jupyterlab-vim does override a lot, so maybe you can find something useful there: GitHub - jupyterlab-contrib/jupyterlab-vim: Vim notebook cell bindings for JupyterLab