lambdalisue / jupyter-vim-binding

Jupyter meets Vim. Vimmer will fall in love.
2.1k stars 136 forks source link

Can one map Ctrl-j/k to other operations instead of running cell and jumping to cell up/down? #123

Open chiwhalee opened 6 years ago

chiwhalee commented 6 years ago

Someone like me maybe acoustomized to use Ctrl-j/k in other ways, say, jump up/down several lines. How can I bind "running cell and jumping to cell up/down" with other keys, and release Ctrl-jk, so that I can use them as I want?

lambdalisue commented 6 years ago

In case if you miss, did you check https://github.com/lambdalisue/jupyter-vim-binding/wiki/Customization ?

Please write what you've tried and how did you fail with some sample code.

chiwhalee commented 6 years ago

Thanks for response. Yes, I‘ve tried that. Especially the snipet under title "Move around in Insert mode with Ctrl-h/j/k/l" ...... // Use Ctrl-h/l/j/k to move around in Normal mode // otherwise it would trigger browser shortcuts CodeMirror.Vim.map("\", "h", "normal"); CodeMirror.Vim.map("\", "l", "normal"); // Updated for v2.0.0 // While jupyter-vim-binding use / to move around cell // The following key mappings should not be defined //CodeMirror.Vim.map("\", "j", "normal"); //CodeMirror.Vim.map("\", "k", "normal");

I uncommented the last two lines, but this take no effects, just as the comment itself says "The following key mappings should not be defined"

lambdalisue commented 6 years ago

Then how about a bit modification of https://github.com/lambdalisue/jupyter-vim-binding/wiki/Customization#run-cell-scrolls-the-cell-to-top-scroll-output-into-view

// Run and select next cell
Jupyter.keyboard_manager.actions.register({
    'help': 'run selected cells',
    'handler': function(env, event) {
        env.notebook.command_mode();
        var actions = Jupyter.keyboard_manager.actions;
        actions.call('jupyter-notebook:run-cell', event, env);
        actions.call('jupyter-notebook:select-next-cell', event, env);
        env.notebook.edit_mode();
    }
}, 'run-and-next', 'vim-binding');

// Run and select previous cell
Jupyter.keyboard_manager.actions.register({
    'help': 'run selected cells',
    'handler': function(env, event) {
        env.notebook.command_mode();
        var actions = Jupyter.keyboard_manager.actions;
        actions.call('jupyter-notebook:run-cell', event, env);
        actions.call('jupyter-notebook:select-previous-cell', event, env);
        env.notebook.edit_mode();
    }
}, 'run-and-previous', 'vim-binding');

// Add two keyboard shortcuts for the new actions
require([
  'nbextensions/vim_binding/vim_binding',
  'base/js/namespace',
], function(vim_binding, ns) {
  // Add post callback
  vim_binding.on_ready_callbacks.push(function(){
    var km = ns.keyboard_manager;
    // Indicate the key combination to run the commans
    km.edit_shortcuts.add_shortcut('ctrl-j', 'vim-binding:run-and-next', true);
    km.edit_shortcuts.add_shortcut('ctrl-k', 'vim-binding:run-and-previous', true);
    // Update Help
    km.edit_shortcuts.events.trigger('rebuild.QuickHelp');
  });
});

Note that all available jupyter mappings are written in https://github.com/lambdalisue/jupyter-vim-binding/blob/master/lib/jupyter/shortcuts.js

chiwhalee commented 6 years ago

I want to map c-j to 5j, namely jump up 5 lines, and I tweaked the lines km.edit_shortcuts.add_shortcut('ctrl-j', 'vim-binding:run-and-next', true); to km.edit_shortcuts.add_shortcut('ctrl-j', '5j', true); or km.edit_shortcuts.add_shortcut('ctrl-j', 'vim-binding:5j', true); however with no luck.