Add vim key bindings for IPython 1.0.0

Posted on October 4, 2013

12


I am a fun of vim and work a lot with IPython Notebook recently. It would be cool to combine the two. Not like emacs having a great plugin supporting IPython protocol, vim is just not good at interacting with external processes. Luckily, I found CodeMirror, the rich editor js lib used by IPython Notebook, has an option to enable vim-like key binding and it is really simple to setup. OK, here we go.

Before we start, you need to find out where your IPython is installed. For me, it is just something like ‘lib/python2.7/site-packages/IPython’. From now on all the paths mentioned are based on that. There are two files to be edited to enable vim key binding, i.e., ‘html/template/notebook.html’ and ‘html/static/notebook/js/codecell.js’.

Add the following line around the place where other script tags are placed in ‘html/template/notebook.html’.


<script src="{{ static_url("components/codemirror/keymap/vim.js") }}" charset="utf-8"></script>

For ‘html/static/notebook/js/codecell.js’, find the following snippet and add ‘keyMap’ as it shows


CodeCell.options_default = {
  cm_config : {
    extraKeys: {
      "Tab" : "indentMore",
      "Shift-Tab" : "indentLess",
      "Backspace" : "delSpaceToPrevTabStop",
      "Cmd-/" : "toggleComment",
      "Ctrl-/" : "toggleComment"
    },
    mode: 'ipython',
    theme: 'ipython',
    keyMap: 'vim',
    matchBrackets: true
  }
};

Tada, you have the vim key binding for IPython Notebook now. One more thing, you might find Esc key doesn’t bring you back to normal (command) mode and you need to press ctrl-[ to simulate it. Enjoy!

Tagged: ,
Posted in: Python