Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ace editor and vim keybindings: using :w command

I'm integrating the Ace Editor in a web app and using the vim key-bindings like so:

 var editor = ace.edit('editor');
 editor.setDisplayIndentGuides(false);
 editor.setHighlightActiveLine(false);
 editor.setShowFoldWidgets(false);
 editor.setShowInvisibles(false);
 editor.setShowPrintMargin(false);
 editor.setKeyboardHandler('ace/keyboard/vim');

I also have this command mapped to Ctrl-S/Command-S just because I wanted to test the behaviour

editor.commands.addCommand({
  name: 'saveFile',
  bindKey: {
    win: 'Ctrl-S', mac: 'Command-S',
    sender: 'editor|cli'
  },
  exec: function (env, args, request) {
    console.log('saving...', env, args, request);
  }
});

While this works, the problem is that when using the ESCape key to enter "normal" mode in Vim, and using :w to save the file, the command's exec function defined above does not get invoked as it does with Ctrl-S/Command-S ...

And the keybinding-vim.js file throws an Error about CodeMirror.commands.save not being defined ...

I have looked at the API documentation and demos but have been unable to find the "correct" way to fix this.

Help appreciated. Thanks

like image 426
just_a_dude Avatar asked Jan 21 '15 23:01

just_a_dude


1 Answers

There is no public api for doing this yet. But you can do

ace.config.loadModule("ace/keyboard/vim", function(m) {
    var VimApi = require("ace/keyboard/vim").CodeMirror.Vim
    VimApi.defineEx("write", "w", function(cm, input) {
        cm.ace.execCommand("save")
    })
})
like image 135
a user Avatar answered Sep 24 '22 13:09

a user