rump roast
rump roast

Reputation: 814

vi shortcuts in ipython notebook

I use ipython notebooks heavily to prototype ideas and build up code line by line, and I'd really like to be able to use vi shortcuts intracell. It seem like ipython notebook ships with functionality which simply needs to be enabled, though a couple options I've found don't seem to work:

http://www.borsuk.org/2014/07/20/ipython-notebook-vim-keys/ http://spaceli.wordpress.com/2013/10/04/add-vim-key-bindings-for-ipython-1-0-0/

I've also tried ivanov's vimception which works keybinding-wise but also breaks syntax highlighting among other things, and above all doesn't seem necessary if ipython ships with codemirror etc.

Upvotes: 10

Views: 3870

Answers (3)

eatsfood
eatsfood

Reputation: 1088

For recent versions of iPython, you can launch it with this:

ipython --TerminalInteractiveShell.editing_mode=vi

You can also set this permanently in your iPython configuration file (~/.ipython/profile_default/ipython_config.py by entering:

c.TerminalInteractiveShell.editing_mode = 'vi'

Upvotes: 1

rump roast
rump roast

Reputation: 814

EDIT: This works much better than vimception: https://github.com/lambdalisue/jupyter-vim-binding


After a bit more research and more messing around, ivanov's vimception can be made to work pretty well.

https://github.com/ivanov/ipython-vimception

To fix the syntax highlighting, comment out line 346 in vimception.js. https://github.com/ivanov/ipython-vimception/issues/7

Also, using the %load_ext vimception does not allow one to turn vimception off, so instead just paste in the javascript as mentioned in the vimception readme.

Finally, vimception highlights the entire line in white making the text hard to read with a dark theme. This can be disabled leaving only a cursor by changing styleActiveLine line 209 in vimception.js to false.

209 cm.setOption('styleActiveLine', false);

really nice way to use python!

Upvotes: 3

memeplex
memeplex

Reputation: 2502

Here is a simple, up-to-date (ipython 3.2), snippet to add to you custom.js. It fixes the most important issues.

define([
  'base/js/namespace',
  'base/js/events',
  'jquery',
  'codemirror/keymap/vim',
  'codemirror/addon/dialog/dialog'
], function(IPython, events, $) {
  events.on('app_initialized.NotebookApp', function() {
    IPython.keyboard_manager.edit_shortcuts.remove_shortcut('esc');
    IPython.Cell.options_default.cm_config.vimMode = true;
    // codemirror dialog for cmdline and search
    $('head').append('<link rel="stylesheet" type="text/css" ' +
      'href="/static/components/codemirror/addon/dialog/dialog.css">')
    $('head').append('<style type="text/css">' +
      '.CodeMirror-dialog {opacity: 0.9 !important;}</style>');
    // avoid ipython command mode while in codemirror dialog
    var bind_events = IPython.Cell.prototype.bind_events;
    IPython.Cell.prototype.bind_events = function () {
      bind_events.apply(this);
      if (!this.code_mirror) return;
      var that = this;
      this.code_mirror.on('blur', function() {
        if ($('.CodeMirror-dialog').length > 0)
          that.events.trigger('edit_mode.Cell', {cell: that});
      });
    }
  });
});

Upvotes: 1

Related Questions