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

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

Open
chiwhalee opened this issue Mar 4, 2018 · 4 comments

Comments

@chiwhalee
Copy link

chiwhalee commented Mar 4, 2018

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?

@chiwhalee chiwhalee changed the title Can one map Ctrl-j/k to other operations instead running cell and jumping to cell up/down? Can one map Ctrl-j/k to other operations instead of running cell and jumping to cell up/down? Mar 4, 2018
@lambdalisue
Copy link
Owner

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
Copy link
Author

chiwhalee commented Mar 5, 2018

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("<C-h>", "h", "normal");
CodeMirror.Vim.map("<C-l>", "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("<C-j>", "j", "normal");
//CodeMirror.Vim.map("<C-k>", "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
Copy link
Owner

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
Copy link
Author

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.

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

2 participants