Advice regarding IPython + MacVim Workflow


I've just found IPython and I can report that I'm in deep love. And the affection was immediate. I think this affair will turn into something lasting, like the one I have with screen. Ipython and screen happen to be the best of friends too so it's a triangular drama. Purely platonic, mind you.

The reason IPython hits the soft spots with me are very much because I generally like command prompts, and especially *nix-inspired prompts with inspiration from ksh, csh (yes, chs is a monster, but as a prompt it sport lots of really good features), bash and zsh. And IPython does sure feel like home for a *nix prompt rider. Mixing the system shell and python is also a really good idea. Plus, of course, IPython helps a lot when solving the Python Challenge riddles. Invaluable even.

Now, I love Vim too. Since I learnt vi back in the days there's no turning back. And I'm on Mac when I have a choice. Now I'd like to glue together my IPython + MacVim workflow. What I've done so far is that I start Ipython using:

ipython -e "open -a MacVim"

Thus when I edit from IPython it starts MacVim with the file/module loaded. Could look a bit like so:

In [4]: %run foo #This also "imports" foo anew
hello world

In [5]: edit foo
Editing... done. Executing edited code... #This happens immediately
hello world

In [6]: %run foo
hello SO World

OK. I think this can be improved. Maybe there's a way to tie IPython into MacVim too? Please share your experiences. Of course if you use TextMate or some other fav editor I'm interested too. Maybe some of the lessons are general.

5/23/2017 11:45:36 AM

Accepted Answer

I use Linux, but I believe this tip can be used in OS X too. I use GNU Screen to send IPython commands from Vim as recommended by this tip. This is how I do it:

First, you should open a terminal and start a screen session called 'ipython' or whatever you want, and then start IPython:

$ screen -S ipython
$ ipython

Then you should put this in your .vimrc:

autocmd FileType python map F5 :w<CR>:!screen -x ipython -X stuff $'\%run %:p:h\n'<CR><CR>

Then when you hit F5, it will tell Screen to execute the command '%run file' inside the 'ipython' created previously, where file is your current buffer in Vim.

You can tweak this to execute the command you want inside IPython from Vim. For example I use this:

autocmd FileType python map <F5> :w<CR>:!screen -x ipython -X stuff $'\%reset\ny\n\%cd %:p:h\n\%run %:t\n'<CR><CR>

This executes %reset (answering yes to the prompt), then change to the directory where the current buffer in vim is located and then %run the file. This is specially useful if you have the %pdb active in IPython.

Don't forget that you need an active Screen session called 'ipython' with IPython running inside.

If you like Emacs. There is good support for IPython.

1/17/2009 7:13:43 PM

This questions is stale now, but just for reference - if you're using IPython 0.11 with ZeroMQ enabled, take a look at vim-ipython (an older version of which shipped with 0.11).

Using this plugin, you can send lines or whole files for IPython to execute, and also get back object introspection and word completions in Vim, like what you get with: object?<enter> and object.<tab> in IPython.

Take a look at two screencasts of this plugin in action on this post.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow