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!
Jason
October 26, 2013
Thanks for posting a how-to. I recently started using vim and have been looking for IPython integration.
spaceli
October 26, 2013
Glad to know my post is helpful. But I just also found some flaws in Code Mirror that do not go well with IPython and the vim keymap is not so accurate. Hopefully, they would improve that in the future. By the way you may want to try Emacs with plugins like http://www.emacswiki.org/emacs/Evil and http://tkf.github.io/emacs-ipython-notebook/ which seems a better alternative. Good luck!
asford
December 16, 2013
You can also enable these keybindings via ipython’s custom.js mechanism. Add the following to your profile’s static/custom/custom.js:
$.getScript(“static/components/codemirror/keymap/vim.js”)
IPython.CodeCell.options_default.cm_config[“keyMap”] = “vim”
jyeee
February 22, 2014
Thank you very much for posting this!
Additionally, you can use ctrl-c (in OSX) to get back to command mode and the IPython built-in cmd-m,[jk] to move between cells.
Jeff
March 3, 2014
I tried both the original proposal and asford’s comment. Neither worked. Using Ipython 1.2.1 with Enthought Canopy, on Linux. Though that’s all on a sever, and so I’m interfacing with the IPython Notebook GUI with a Mac. Any thoughts?
jyeee
March 4, 2014
@Jeff – My server is OSX and anaconda so I can’t duplicate, but try clearing the cache to get the updated JS.
Jeff
March 4, 2014
That worked! It notably only worked for the original proposal, not asford’s.
This is going to be great. O_O
David Ketcheson
April 19, 2014
With the current CodeMirror, I think you need to set
vimMode: true
instead of ‘keymap: ‘vim”. More importantly, with IPython 2.0’s modal user interface, it seems that there’s no way to get command mode within a cell, since escape takes you to the notebook’s command mode…
akai
August 5, 2014
@David That is the reason why I can’t move to 2.x. Codemirror even implements some Ex commands of vim and allows various extensions. I hope somebody could find a workaround…
David Ketcheson
August 7, 2014
Checkout https://github.com/ivanov/ipython-vimception !
It’s still a little buggy, but works pretty well.
jyeee
August 13, 2014
+1 David Ketcheson! vimception is still missing some features but it’s got enough vi to get me by 🙂 Thank you!
Andrew Temme
September 15, 2014
Any ideas on how to apply this to text and markdown cells?