Python IDE

Roland Puntaier Roland.Puntaier at br-automation.com
Wed Jun 20 04:10:30 EDT 2007


Hi,

With a python-enabled VIM it's possible to execute and thus test the 
python code. I have the following lines in my vimrc.
F2 prints the result of a line evaluation on the bottom or in a window 
named pyout if there. I open pyout with ":vert rightb new pyout" mapped to 
F1.
F4 does the same on the selected part of a line
F3 evaluates without printing


py import vim

py << EOL
def PyOutput(): 
  iPy=-1
  for i in range(len(vim.buffers)):
    if vim.buffers[i].name and vim.buffers[i].name.find("pyout")!=-1:
      iPy=i
  return iPy
EOL

"pyout if present must be right below
"r replaces, p prints, symb sets the start
py << EOL 
def PrintCurrentLine(*args): 
  cur_str = vim.current.line 
  action, symb = None, None 
  for i in args: 
    if i in ["r","p"]: action = i 
    else:              symb   = i 
  try:    start = cur_str.rindex(symb)+len(symb) 
  except: start = 0 
  eval(compile("pyoutres="+cur_str[start:],'<string>','exec'),globals()) 
  result = eval("pyoutres",globals()) 
  if action == "r": 
    vim.current.line = cur_str[:start]+str(result) 
  else: 
    iPy=PyOutput()
    if iPy != -1:
      resS=">>> "+cur_str+":"+'\n'+str(result)+'\n'
      resL=resS.split('\n')
      vim.buffers[iPy].range(0,0).append(resL)
      vim.command("wincmd b")
      vim.command("normal gg")
      vim.command("wincmd p")
      vim.command("wincmd j")
    else:
      print result
EOL 
command -narg=* Pyl python PrintCurrentLine(<f-args>) 

map <F2> :py PrintCurrentLine()<CR>

"this evaluates selected region with <F2>
map <F4> "tyo^["tp<F2>u
map <F1> :vert rightb new pyout


python << EOL 
def EvaluateCurrentRange(): 
  c_r=vim.current.range
  i=0
  while c_r[0].startswith(' '*(i+1)):
    i+=1
  rngstr='\n'.join([ln[i:] for ln in c_r])
  eval(compile(rngstr,'<string>','exec'),globals()) 
EOL 
map <F3> :py EvaluateCurrentRange()<CR>




python-list-bounces+roland.puntaier=br-automation.com at python.org schrieb 
am 19.06.2007 17:20:08:

> Bjoern Schliessmann wrote:
> > BartlebyScrivener wrote:
> >> VIM
> > 
> > *clap-clap*
> > 
> > BTW, are there tutorials on the more arcane vim functions that come
> > in handy with Python?
> > 
> > Regards,
> > 
> > 
> > Bj?rn
> > 
> Not a tutorial, but I found this little snippet:
> 
> http://www.ibiblio.org/obp/pyBiblio/tips/elkner/vim4python.php
> 
> I thought the last line was especially neat.
> 
> Will
> -- 
> http://mail.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list