IPython - Enhanced Interactive Python

ipy.vim understands visual mode

Reported by Sebastian Busch on 2009-10-25
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Fix Committed
Fernando Perez

Bug Description

I took the code from https://code.launchpad.net/~pivanov/ipython/devel which has an improved vim integration in ipython. it supports both, sending one line and many lines to ipython. to do so, one would need to:
1) go to one line and hit Shift-F5
2) select many lines in visual mode and hit F9.

however, vim can execute different things upon the same key stroke, depending on if it is in visual mode or not. i remapped the keybindings a bit, now having action and setting regarding the whole file on F5/F9 and same for a line or range of lines on the same keys with Shift pressed:

<F5> run the whole file
<S-F5> run one line when in normal mode
<S-F5> run the selected range when in visual mode

<F9> toggle_send_on_save
<S-F9> toggle_reselect

Besides, I took the two functions together into one which takes a keyword.


Here is my patch in from of a bzr diff:

=== modified file 'docs/examples/core/ipy.vim'
--- docs/examples/core/ipy.vim 2009-05-13 00:08:49 +0000
+++ docs/examples/core/ipy.vim 2009-10-25 11:45:10 +0000
@@ -51,29 +51,25 @@
         raise Exception, "Not connected to an IPython server"
     print "\'run %s\' sent to ipython" % vim.current.buffer.name

-def run_this_line():
- if check_server():
- send(vim.current.line)
- print "line \'%s\' sent to ipython"% vim.current.line
- else:
- raise Exception, "Not connected to an IPython server"
-def run_these_lines():
- r = vim.current.range
- if check_server():
+def run_these_lines(mode='norm'):
+ if check_server():
+ r = vim.current.range
         for l in vim.current.buffer[r.start:r.end+1]:
- #print "lines %d-%d sent to ipython"% (r.start,r.end)
+ if mode == 'norm':
+ print "line %d sent to ipython"% (r.start+1)
+ elif mode == 'vis':
+ #reselect the previously highlighted block
+ if reselect:
+ vim.command("normal gv")
+ #vim lines start with 1
+ print "lines %d-%d sent to ipython"% (r.start+1,r.end+1)
+ else:
+ raise Exception, "mode not recognized"
         raise Exception, "Not connected to an IPython server"
- #reselect the previously highlighted block
- if reselect:
- vim.command("normal gv")
- #vim lines start with 1
- print "lines %d-%d sent to ipython"% (r.start+1,r.end+1)

 def toggle_reselect():
     global reselect
@@ -123,21 +119,21 @@
         let s:ssos = 1
         au BufWritePost *.py :py run_this_file()
- echo "Autowsend On"
+ echo "Autosend On"

 map <silent> <F5> :python run_this_file()<CR>
-map <silent> <S-F5> :python run_this_line()<CR>
-map <silent> <F9> :python run_these_lines()<CR>
-map <silent> <S-F9> :python toggle_reselect()<CR>
+map <silent> <S-F5> :python run_these_lines(mode='norm')<CR>
+vmap <silent> <S-F5> :python run_these_lines(mode='vis')<CR>
 map <silent> <C-F6> :python send('%pdb')<CR>
 map <silent> <F6> :python set_breakpoint()<CR>
 map <silent> <s-F6> :python clear_breakpoint()<CR>
 map <silent> <F7> :python run_this_file_pdb()<CR>
 map <silent> <s-F7> :python clear_all_breaks()<CR>
-imap <C-F5> <ESC><F5>a
+imap <C-F5> <ESC><C-F5>a
 imap <S-F5> <ESC><S-F5>a
 imap <silent> <F5> <ESC><F5>a
-map <C-F5> :call <SID>toggle_send_on_save()<CR>
+map <F9> :call <SID>toggle_send_on_save()<CR>
+map <silent> <S-F9> :python toggle_reselect()<CR>
 py connect()

Tags: vim Edit Tag help

Related branches

description: updated
Fernando Perez (fdo.perez) wrote :

Sorry, Sebastian, but your patch doesn't apply. The attachment is broken:

amirbar[ipython]> bzr patch ~/visualmode.patch
patch: **** Only garbage was found in the patch input.
bzr: ERROR: Patch application failed

and when I try to copy/paste the diff above into a file and use it as a patch, I get:

amirbar[ipython]> patch -p0 < visualmode.diff
patching file docs/examples/core/ipy.vim
Hunk #1 FAILED at 51.
Hunk #2 FAILED at 119.
patch unexpectedly ends in middle of line
2 out of 2 hunks FAILED -- saving rejects to file docs/examples/core/ipy.vim.rej
patch unexpectedly ends in middle of line

If you make a new attachment with

bzr diff docs/examples/core/ipy.vim > visualmode.diff

I'll be happy to apply it.


Changed in ipython:
status: New → In Progress
assignee: nobody → Fernando Perez (fdo.perez)

Fernando -- sorry for the trouble. Please find attached the new patch.

However, I am not sure how this fits into the current version of ipython; the ipy_vimserver seems to be moved...

Best regards,

Fernando Perez (fdo.perez) wrote :

Thanks a lot, Sebastian. I've committed your patch to the 0.10.1 series:


And I made minimal changes to adapt it to trunk:


When you make the switch to trunk (the configuration changed a lot, so it may take a little work on your part), please give it a test run and drop us any fixes you may have.

Thanks again for the contribution!

Changed in ipython:
status: In Progress → Fix Committed
To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers