Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

Custom bindings for <Esc> #8

Closed
gboehl opened this issue Oct 14, 2015 · 19 comments
Closed

Custom bindings for <Esc> #8

gboehl opened this issue Oct 14, 2015 · 19 comments

Comments

@gboehl
Copy link

gboehl commented Oct 14, 2015

Thanks a lot for your excellent work!
I was wondering if there is the chance to allow for multi-stroke bindings (f.e. "j,j" or "j,k") to be mapped against . I only managed to map normal key presses by replacing 'Esc' in the last paragraph...
Would you have any hints?

@lambdalisue
Copy link
Owner

Im sorry but if CodeMirror's mapping didnot work, i hava no idea. While this use CodeMirror's Vim mode, asking it in that repository might give you hint i guess. let me know if you manage.

@lambdalisue
Copy link
Owner

and i'm sure there is a way while CodeMirror could do it :-)

@gboehl
Copy link
Author

gboehl commented Oct 14, 2015

I will, thanks a lot!
Btw, I added those lines after km.command_shortcuts.clear_shortcuts(), so that execution still works in command mode:

km.command_shortcuts.add_shortcut('ctrl-enter', 'ipython.execute-in-place');
km.command_shortcuts.add_shortcut('shift-enter', 'ipython.run-select-next');

@gboehl
Copy link
Author

gboehl commented Oct 14, 2015

They answered quite fast, here is the solution:

  var exports = {
    'load_ipython_extension': function() {
      // apply options and events on existing CodeMirror instances
      namespace.notebook.get_cells().map(function(cell) {
        var cm = cell.code_mirror;
    var map = {'J K': leaveInsertOrNormal,
            'Esc': leaveInsertOrNormal,
            'Tab': insertSoftTab,
    };
    CodeMirror.normalizeKeyMap(map);
        if (cm) {
          cm.setOption('keyMap', 'vim');
          cm.setOption('extraKeys', map);
          cm.on('blur', leaveInsert);
        }
      });
    }
  };
  return exports;

@lambdalisue
Copy link
Owner

Btw, I added those lines after km.command_shortcuts.clear_shortcuts(), so that execution still works in command mode:

It seems useful. I'll add that :-)

You probably need to add 'J K' mapping to https://github.com/lambdalisue/jupyter-vim-binding/blob/master/nbextensions/vim_binding.js#L52-L55 as well.

@JobJob
Copy link

JobJob commented Oct 21, 2015

Hi @gboehl did this actually work for you?

@gboehl
Copy link
Author

gboehl commented Oct 21, 2015

Well, afterwards you won't be able to use 'J' anymore because he's waiting for the 'K', so the answer ist rather "No". :)

To get this working one would have to get deeper into how key bindings are dealt with in CodeMirror, and I unfortunately don't have the time for that. But if you have a solution, let me know!

@JobJob
Copy link

JobJob commented Oct 21, 2015

What I actually did was add this line to /path/to/your/site-packages/notebook/static/components/codemirror/keymap/vim.js:

{ keys: 'jk', type: 'keyToKey', toKeys: '<Esc>', context: 'insert' }, 

somewhere around line 66 under default keymap, just duplicated the line above that was mapping <C-c> to <Esc> in insert mode and changed the keys to jk. Codemirror vim mode handles the rest. I had done this in another project so knew where to look. Enjoy. It won't exit into Jupyter command mode, but it's not a bad start. I also mapped cmd-l to exit direct into jupyter command mode, this is in my vim_bindings.js (somewhere around line 100):

km.edit_shortcuts.add_shortcut('cmd-l', 'ipython.go-to-command-mode');

@JobJob
Copy link

JobJob commented Oct 21, 2015

Great plugin btw lambdalisue - thanks!

@lambdalisue
Copy link
Owner

😄

@JobJob
Copy link

JobJob commented Oct 22, 2015

Actually @gboehl there's an even easier and better way, without modifying vim.js, just put this somewhere in vim_binding.js:

CodeMirror.Vim.map("jk","<Esc>","insert")

It maps any key to any key, e.g. I also have for instance:

CodeMirror.Vim.map("H","^","normal")
CodeMirror.Vim.map("L","$","normal")

@gboehl
Copy link
Author

gboehl commented Oct 22, 2015

Both solutions work just fine, thanks a lot!

@lambdalisue
Copy link
Owner

just put this somewhere in vim_binding.js:

Probably using custom.js is better I guess

@lambdalisue
Copy link
Owner

@lambdalisue TODO Document this conversations to README

@lambdalisue
Copy link
Owner

Thanks guys. I finally add it to the document (use custom.js instead) https://github.com/lambdalisue/jupyter-vim-binding#customize

@JobJob
Copy link

JobJob commented Dec 10, 2015

👍

@raoofha
Copy link

raoofha commented Jan 29, 2017

I have a similar problem, I want to map Esc in normal mode to go to command mode, this is what I tried

  var leaveNormalOrInsertMode = function (cm) {
    var command = CodeMirror.Vim.findKey(cm, "<Esc>"); // <--- I don't know what this does
    //console.log(command, cm.state.vim.normalMode,cm.state.vim.insertMode,cm.state.vim);
    if (typeof command === 'function') {
      return command();
    }else{
      if (!(cm && cm.state.vim.insertMode)){
        CodeMirror.prototype.leaveNormalMode(cm);
      }
    }
  }
    cm_config.extraKeys = $.extend(cm_config.extraKeys || {}, {
      //'Esc': CodeMirror.prototype.leaveInsertMode,
      'Esc': leaveNormalOrInsertMode,
      'Shift-Esc': CodeMirror.prototype.leaveNormalMode,
      'Ctrl-C': false,  // To enable clipboard copy
    });

I guess there might be an issue that you didn't do it yourself by default. if you could give me a hint where the problem is, I'll appreciate it
by the way thanks for your great work @lambdalisue

@hl037
Copy link

hl037 commented Jul 1, 2018

@Tinkidinki
Copy link

Tinkidinki commented Oct 7, 2020

Actually @gboehl there's an even easier and better way, without modifying vim.js, just put this somewhere in vim_binding.js:

CodeMirror.Vim.map("jk","<Esc>","insert")

It maps any key to any key, e.g. I also have for instance:

CodeMirror.Vim.map("H","^","normal")
CodeMirror.Vim.map("L","$","normal")

Do you just add this line, or are there other things to be done? Where in vim_bindings.js should this line be added? Just adding it does not seem to work for me.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants