CodeMirror 6: Programmatically moving cursor to other editor instance

Hi @marijn, I’m trying to programmatically move the cursor from one editor to another. So far I have something like the following to be triggered on “Tab” (which does not work):

function tabCursor(view) {
    let nextView = _getNextView_(view);

    nextView.dispatch(
        nextView.state.update({
            selection: new EditorSelection([EditorSelection.cursor(0)], 0);
        })
    )
    return true;
}
...
{ key: 'Tab', run: tabCursor }

The actual result is that the cursor doesn’t move. Any ideas of how to make this work?

I suppose you’ll also want to focus the other editor.

1 Like

Ahh. Of course. That works. Thanks!

Somewhat related: would it be possible for an extension to be able to support multiple cursors spread across multiple editor instances? Something like the current multiple cursor extension, but not limited to a single editor instance?

Not in a straightforward way. CodeMirror uses the browser’s focus behavior, which means that your key events are only going to a single editor.

So an individual editor would need to be able to access references to all the editors with cursors/selections in them and propagate the event to them?