Change vim mode programmatically

Does anyone know how to change the vim mode programmatically? I’m using this extension.

I see it uses CM5 under the hood and calls CodeMirror.signal(cm, "vim-mode-change", {mode: "normal"});

I don’t see a similar property/method in CM6. Here’s a demo of what I’m trying to do:

  1. user changes text
  2. when correct, update text in editor
  3. set mode back to normal mode

vim-demo

Another way this could be achieved would be to send native key events to CodeMirror i.e. send the Escape key.

The team behind that extension responded with two options:

Option 1:

import {Vim} from "@replit/codemirror-vim"

var view = new EditorView({...})
Vim.exitInsertMode(view.cm)

Option 2:

Vim.handleKey(_view.cm, "<Esc>")

Note: you may need to typecast .cm with (view as any).cm.

Source: How to programmatically change modes? · Issue #6 · replit/codemirror-vim · GitHub