views:

1537

answers:

3

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.

+1  A: 

In the ipythonrc.ini file in you home directory change the editor to MacVim.

Igal Serban
+7  A: 

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.

Manuel Ceron
This sounds truly promising. I've marked it as the answer even though I can't get it to work yet. For some reason my screen session doesn't have the path to ipython or something. I can't quite figure it out.
PEZ
Try starting Screen + IPython in this way: $ screen -S ipython ipython
Manuel Ceron
+1  A: 

I've had the same issue. After experimenting this is my solution: if you placed MacVim.app in /Applications and mvim is in your path, in ~/.ipython/ipythonrc change the line

editor 0

to

editor mvim --remote-tab-wait-silent

I think you need to keep MacVim open in the background. You could also use the -f flag. Look at this thread from the MacVim google group.

Meemo Garza