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

Enable <C-c> mapping to exit insert mode doesn't work #100

Open
tqhdesilva opened this issue May 24, 2017 · 6 comments
Open

Enable <C-c> mapping to exit insert mode doesn't work #100

tqhdesilva opened this issue May 24, 2017 · 6 comments

Comments

@tqhdesilva
Copy link

tqhdesilva commented May 24, 2017

Summary

Tried to use the code from the customization wiki to enable <C-c> for exiting insert mode.

Behavior

Expected

It should rebind <C-c> to exit insert mode.

Actual

When loading notebook, in custom.js:

Uncaught TypeError: Cannot read property 'get_cells' of undefined

Step by step procedure

Paste into ~/.jupyter/custom/custom.js:

// enable the 'Ctrl-C' mapping
// change the code mirror configuration
var cm_config = require("notebook/js/cell").Cell.options_default.cm_config;
delete cm_config.extraKeys['Ctrl-C'];
// change settings for existing cells
Jupyter.notebook.get_cells().map(function(cell) {
    var cm = cell.code_mirror;
    if (cm) {
        delete cm.getOption('extraKeys')['Ctrl-C'];
    }
});
// map the keys
CodeMirror.Vim.map("<C-c>", "<Esc>", "insert");

What you have done to solve the issue

Pasting the code from custom.js into the browser console after load works, as does changing

'Ctrl-C': false,  // To enable clipboard copy

to

'Ctrl-C': CodeMirror.prototype.leaveInsertMode,  // To enable clipboard copy

in vim_bindings.js, similar to what was suggested in #80. I didn't really like changing code from the repository for configuration, so I tried to change the binding again in custom.js:

// Bind <C-c> to exit insert mode
require([
  'nbextensions/vim_binding/vim_binding',
], function(nb) {
  CodeMirror.Vim.map("<C-c>", "<Esc>", "insert");
  nb.attach = (function() {
    var cached_function = nb.attach;
    return function(){
      var result = cached_function.apply(this, arguments);
      var cm_config = require("notebook/js/cell").Cell.options_default.cm_config;
      delete cm_config.extraKeys['Ctrl-C'];
      Jupyter.notebook.get_cells().map(function(cell) {
          var cm = cell.code_mirror;
          if (cm) {
              delete cm.getOption('extraKeys')['Ctrl-C'];
          }
      });
      return result;
    }
  })()
});

It's basically the same code from the wiki, but it sticks the code into the exports.attach function from vim_bindings.js. This works for me.

Remarks

I'm still not sure if it's just an issue on my end that prevents the wiki's custom.js script from working. If other people have had the same issue and my solution works for them I'd like to add it to the customization wiki.

@lambdalisue
Copy link
Owner

I currently does not have an environment to test but does the following code helps you?

// ~/.jupyter/custom/custom.js
require([
  'jquery',
  'base/js/namespace',
  'notebook/js/cell',
  'nbextensions/vim_binding/vim_binding', // To call the function after 'vim_binding'
], function($, ns, cell, vim_binding) {
  "use strict";
  var Cell = cell.Cell;

  // Update default config
  var cm_config = Cell.options_default.cm_config;
  cm_config.extraKeys = $.extend(cm_config.extraKeys || {}, {
    'Ctrl-C': CodeMirror.prototype.leaveInsertMode,
  });

  // Update existing config
  ns.notebook.get_cells().map(function(cell) {
    var cm = cell.code_mirror;
    if (cm) {
      cm.setOption('extraKeys', $.extend(
        cm.getOption('extraKeys') || {},
        cm_config.extraKeys
      ));
    }
  });
});

If not, I need time to investigate...

@lambdalisue
Copy link
Owner

@lambdalisue
Copy link
Owner

Ah, sorry I got the problem by reading the code. The code above won't work

@lambdalisue
Copy link
Owner

https://github.com/lambdalisue/jupyter-vim-binding/blob/master/vim_binding.js#L70-L74

These lines overwrite mappings written in custom.js.
Probably these keys should be obtained from params here https://github.com/lambdalisue/jupyter-vim-binding/blob/master/vim_binding.js#L39-L44 so that users can modify

@lambdalisue
Copy link
Owner

Currently I don't have enough time for this project. So anyone?

@mgmarino
Copy link

This is important to me. I will try to have a look to make a reasonable PR when I get the chance... In the meantime, for anyone coming here, a workaround can be to simply edit the source code of the extension (i.e. $(jupyter --data-dir)/nbextensions/vim_binding/vim_binding.js) to read:

     // ...
      var cm_config = Cell.options_default.cm_config;
      cm_config.keyMap = 'vim';
      cm_config.extraKeys = $.extend(cm_config.extraKeys || {}, {
        'Esc': CodeMirror.prototype.leaveInsertMode,
        'Shift-Esc': CodeMirror.prototype.leaveNormalMode,
        'Ctrl-C': CodeMirror.prototype.leaveInsertMode,
        //'Ctrl-C': false,  // To enable clipboard copy
      });
    // ...

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

No branches or pull requests

3 participants