Comment 0 for bug 460359

Revision history for this message
Sebastian Busch (webmaster-thamnos) wrote :

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.

Best,
Sebastian.

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:19:05 +0000
@@ -51,29 +51,27 @@
         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():
- #send(str((vim.current.range.start,vim.current.range.end)))
- for l in vim.current.buffer[r.start:r.end+1]:
- send(str(l)+'\n')
- #send(str(vim.current.buffer[vim.current.range.start:vim.current.range.end]).join("\n"))
- #print "lines %d-%d sent to ipython"% (r.start,r.end)
- else:
- 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 run_these_lines(mode='norm'):
+ if check_server():
+ if mode == 'norm':
+ send(vim.current.line)
+ print "line \'%s\' sent to ipython"% vim.current.line
+ elif mode == 'vis':
+ r = vim.current.range
+ #send(str((vim.current.range.start,vim.current.range.end)))
+ for l in vim.current.buffer[r.start:r.end+1]:
+ send(str(l)+'\n')
+ #send(str(vim.current.buffer[vim.current.range.start:vim.current.range.end]).join("\n"))
+ #print "lines %d-%d sent to ipython"% (r.start,r.end)
+ #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"
+ else:
+ raise Exception, "Not connected to an IPython server"

 def toggle_reselect():
     global reselect
@@ -128,16 +126,16 @@
 endfun

 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()