lambdalisue / jupyter-vim-binding

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

How to get "Scroll output into view" snippet to work? #101

Closed joelostblom closed 7 years ago

joelostblom commented 7 years ago

Summary

I am trying to get the following snippet from the customization page working:

// override two relevant actions by inserting a scroll-cell-top action
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:scroll-cell-top', event, env);
        env.notebook.edit_mode();
    }
}, 'run-cell', 'vim-binding');

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:scroll-cell-top', event, env);
        actions.call('jupyter-notebook:select-next-cell', event, env);
        env.notebook.edit_mode();
    }
}, 'run-cell-and-select-next', 'vim-binding');

When I paste that into my custom.js, it has no effect on my jupyter-notebook sessions (tried restarting the notebook). I also tried binding the new run to a command with CodeMirror.Vim.map("<S-Enter>", "<Plug>(vim-binding-run-cell)", "normal");, but that didn't help either. Does this snippet still work? Should I put it somewhere else than in my custom.js?

Environment

lambdalisue commented 7 years ago

Sorry to take time to respond. You have to assign that mapping to Jupyter mapping.

https://github.com/lambdalisue/jupyter-vim-binding/issues/102

would help you.

joelostblom commented 7 years ago

@lambdalisue Sorry for the very late reply. I have recently been trying to get this to work again, but I seem to be doing something wrong. As per your suggestion (if I understood it correctly), I added the following to my custom.js:

// Configure Jupyter Keymap
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;
    // Allow Ctrl-2 to change the cell mode into Markdown in Vim normal mode
    km.edit_shortcuts.add_shortcut('ctrl-4', 'vim-binding:run-cell', true);
    // Update Help
    km.edit_shortcuts.events.trigger('rebuild.QuickHelp');
  });
});

However, when I press ctrl-4 (I tried other bindings also), apparently nothing happens. The cell is not executed and it is not centred. What am I doing wrong here?

lambdalisue commented 7 years ago

I copied your code into my custom.js and it worked (Google Chrome 61.0.3163.100 (Official Build) (64-bit) in macOS Sierra)

Are you sure that you are using the latest jupyter and jupyter-vim-binding?

joelostblom commented 7 years ago

Thanks for the reply! I am using Firefox 55.0.3 and also tried in an incognito window with Chromium 61.0.3163.91. My Jupyter notebook is version 5.0.0.


...I cleared a bunch of now irrelevant messages I posted while trying to fix this...


The solution for me was to simply rename the function. Instead of run-cell, I call it run-and-top and this works perfectly. I don't know why this is the case (maybe it gets overwritten by another function with the same name?), but it is working now. I will suggest these name changes in the examples, just in case anyone else has the same problem.

joelostblom commented 7 years ago

I replaced the current solution on the wikipage with mine. Let me know if you want me to add it as a separate edit instead.

For completeness , here is what I ended up with in my custom.js:

// Run and scroll to top
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:scroll-cell-top', event, env);
        env.notebook.edit_mode();
    }
}, 'run-and-top', 'vim-binding');

// Run and scroll to top and select next
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:scroll-cell-top', event, env);
        actions.call('jupyter-notebook:select-next-cell', event, env);
        env.notebook.edit_mode();
    }
}, 'run-and-next-and-top', '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-enter', 'vim-binding:run-and-top', true);
    km.edit_shortcuts.add_shortcut('shift-enter', 'vim-binding:run-and-next-and-top', true);
    // Update Help
    km.edit_shortcuts.events.trigger('rebuild.QuickHelp');
  });
});