Define a new "key-map"

TLDR: This is sorta like a mix of VIM normal mode & ParEdit

I want to do the following. Pressing Ctrl-S puts CodeMirror into an alternative mode, where

h → go to left sibling
l → goto right sibling
j → go down in syntax tree
k → go up in syntax tree
… → other keys for navigating via syntaxTree nodes
Ctrl-S → go back to regular CodeMirror editing

So basically Ctrl-S toggles between two modes: regular codemirror editing mode and special ast-navigating mode.

What is the right layer to do this ?


EDIT: I already have the functions for navigating the syntax tree. What I need help on is defining this keymap / injecting it into CodeMirror.

You could use a high-precedence key binding with the any option and make it check whether the editor is in your custom mode and handle these keys when it is. Or make all your key bindings check whether the proper mode is active. Or reconfigure the editor to add/remove keymaps when switching modes. Not sure what will work best in practice.