Skip to content
This repository was archived by the owner on Aug 22, 2023. It is now read-only.
Alisue edited this page Dec 4, 2015 · 12 revisions

This wiki is a place to share snippets that enhance the usability of the vim - IPython integration, but aren't so general that they are part of the binding.

Emacs like binding in Insert Mode

require(['codemirror/keymap/vim'], function() {
  // Emacs like binding
  CodeMirror.Vim.map("<C-a>", "<Esc>^i", "insert");
  CodeMirror.Vim.map("<C-e>", "<Esc>$a", "insert");
  CodeMirror.Vim.map("<C-f>", "<Esc>lwi", "insert");
  CodeMirror.Vim.map("<C-b>", "<Esc>lbi", "insert");
  CodeMirror.Vim.map("<C-d>", "<Esc>lxi", "insert");
});

Move around in Insert mode with Ctrl-h/j/k/l

require(['codemirror/keymap/vim'], function() {
  // Use Ctrl-h/l/j/k to move around in Insert mode
  CodeMirror.Vim.defineAction('[i]<C-h>', function(cm) {
    var head = cm.getCursor();
    CodeMirror.Vim.handleKey(cm, '<Esc>');
    if (head.ch <= 1) {
      CodeMirror.Vim.handleKey(cm, 'i');
    } else {
      CodeMirror.Vim.handleKey(cm, 'h');
      CodeMirror.Vim.handleKey(cm, 'a');
    }
  });
  CodeMirror.Vim.defineAction('[i]<C-l>', function(cm) {
    var head = cm.getCursor();
    CodeMirror.Vim.handleKey(cm, '<Esc>');
    if (head.ch === 0) {
      CodeMirror.Vim.handleKey(cm, 'a');
    } else {
      CodeMirror.Vim.handleKey(cm, 'l');
      CodeMirror.Vim.handleKey(cm, 'a');
    }
  });
  CodeMirror.Vim.mapCommand("<C-h>", "action", "[i]<C-h>", {}, { "context": "insert" });
  CodeMirror.Vim.mapCommand("<C-l>", "action", "[i]<C-l>", {}, { "context": "insert" });
  CodeMirror.Vim.map("<C-j>", "<Esc>ja", "insert");
  CodeMirror.Vim.map("<C-k>", "<Esc>ka", "insert");

  // 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");
  CodeMirror.Vim.map("<C-j>", "j", "normal");
  CodeMirror.Vim.map("<C-k>", "k", "normal");
});

Selecting all

require(['codemirror/keymap/vim'], function() {
   CodeMirror.Vim.map("<C-a>", "ggVG", "normal");
});

Show trailing spaces

// custom.js
require([
  'base/js/namespace',
  'notebook/js/cell',
  'codemirror/addon/edit/trailingspace'
], function(ns, cell) {
  var cm_config = cell.Cell.options_default.cm_config;
  cm_config.showTrailingSpace = true;

  ns.notebook.get_cells().map(function(cell) {
    var cm = cell.code_mirror;
    if (cm) {
      cm.setOption('showTrailingSpace', true);
    }
  });
});
/* custom.css */
.cm-trailingspace {
  border-bottom: 1px solid #ccc;
}

Adding custom actions

require(['codemirror/keymap/vim'], function() {
  // a
  CodeMirror.Vim.defineAction("hello", function(){console.log("hello")});
  // 'a' is the key you map the action to
  CodeMirror.Vim.mapCommand("a", "action", "hello", {}, {context: "normal"}); 
});

Calling a Jupyter shortcut

require(['base/js/namespace'], function(ns) {
    ns.keyboard_manager.actions.call('jupyter-notebook:run-cell-and-insert-below');
});

Powerful resources

Clone this wiki locally