Search code examples
javascriptace-editor

How to programmatically put Ace (vim keyboard layout) into insertion mode


I am using ace with vim keyboard layout. My javascript is

    this.editor = window.ace.edit(this.$el.id)
    ace.config.loadModule("ace/keyboard/vim", function(m) {
        var VimApi = ace.require("ace/keyboard/vim").CodeMirror.Vim
        //here: put editor in insertion mode :startinsert
        VimApi.defineEx("write", "w", function(cm, input) {
            //cm.ace.execCommand("save");
            console.log("My command :w triggered");
        })
    })

How do I put vim into insertion mode?

Or put otherwise: how do I trigger the enterInsertMode action defined in https://github.com/ajaxorg/ace/blob/master/lib/ace/keyboard/vim.js#L3032 ? I can't get access to the actions object needed for this.

Or: how do I send an i-keypress to ace such that insertion mode is started?


Solution

  • There doesn't seem to be any official api to do this, but using either of the following works with the current version of ace:

     editor.onTextInput("i");
    

    or

     var cm = editor.state.cm;
     editor.$vimModeHandler.actions.enterInsertMode(cm, {}, cm.state.vim);