array, a better shell

Roland Puntaier Roland.Puntaier at br-automation.com
Wed Dec 20 08:18:44 EST 2006


>I have also used the shell of Mathematica. It's quite powerful and it
>can show graphics too inlined, but globally I don't like it fully
>because it makes editing small programs a pain (for me)...

I use Vim to edit python code and can execute any selection (F3) or single 
lines (F2) 
whenever I want to (Vim must be built to include the python interpreter).

Here is the according Vimrc code

"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")
    else:
      print result
EOL 
command -narg=* Pyl python PrintCurrentLine(<f-args>) 
map <F2> :py PrintCurrentLine()<CR>

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>



More information about the Python-list mailing list