Silly question; best way to run a python program from Vim?

K

Kenneth McDonald

After looking around at various editors, I've finally decided on
(I think :) ) Vim for my future work. (jed is a great editor,
but doesn't do neat things like allow me to write python code
to add to the editor functionality. Emacs is just too bloated
these days. jEdit is one of the best GUI editors I've seen, but
I've still not seen a GUI editor with the power of those with
a non-GUI ancestry. etc. etc.)

However, I can't quite figure out how one 'should' excute
the Python program one is currently working on in Vim. I'm
aware of the :python and :pyfile commands, but they seem to
be oriented towards running scripts which are intended to
do editing tasks, i.e. which interact with Vim. I simply want
to do something like

:runthissucker

to execute the current file under python, pop up a window
with the results, automatically take me to the point in
the buffer (or other file) where an error was encountered,
etc. In other words, I want to run the program as part of
the development process of that program, not as an addon to
Vim. (Looking back, that isn't terribly clear, but it's about
as clear as I can think to make it right now.) Using 'jed'
and the standard jed python mode, this was just '^C^C', but
from what I understand of the python functionality of
Vim, I currently have to do something like

:python my_file.py

Thanks for the help. Of course, if you have other useful
suggestions for using Vim with Python, please feel free
to contribute those also.

Cheers,
Ken
 
R

Ryan Paul

However, I can't quite figure out how one 'should' excute
the Python program one is currently working on in Vim. I'm
aware of the :python and :pyfile commands, but they seem to
be oriented towards running scripts which are intended to
do editing tasks, i.e. which interact with Vim. I simply want
to do something like

I've been using python/vim for about a year now, I think. I wrote a
vim/python script that lets me easily redirect stdout to a vim buffer. I
can execute a python script, and have the output appear in a nofile
buffer. This is a bit kludgy, but its extremely useful. I have other key
bindings set up so I can use this for all sorts of things, not just python.

Here is the relevant excerpt from my vimrc - I hope you find this
useful.

""" Python Stuff
python << EOF
import vim
from vim import *
import sys,commands

def first(c,l):
for x in l:
if c(x): return x

txtsplit = '-'*20
CB = lambda: current.buffer
io = sys.stdout,sys.stderr
getBuf = lambda x:first(x,buffers)
pybuf = lambda x:x[0].startswith('Program Output')
prepend = lambda l,t: l[:len(l)-len(l.lstrip())] + t + l.lstrip()

def rep(t,l):
for x in l.items():
t = t.replace(x[0],x[1])
return t

def getSel(l1,l2):
return '\n'.join(current.buffer.range(int(l1),int(l2))[:])+'\n'

class vBuf:
def __init__(s,b=None):
s.buf = b and b or getBuf(pybuf)
if not s.buf:
command(':bel new')
command(':set bt=nofile')
s.buf = current.buffer
s.clear()

def write(s,t):
if '\n' in t: map(s.buf.append,t.split('\n'))
else: s.buf[-1]+=t

def clear(s):
s.buf[:] = None
s.buf[-1] = 'Program Output Buffer'
s.buf.append('-'*20)
s.buf.append('')

def redirbuf(b=None):sys.stdout = sys.stderr = vBuf(b)
def resetbuf():sys.stdout,sys.stderr = io

def PyValOut(r):
redirbuf()
exec(r)
print txtsplit
resetbuf()

def PyExOut(r):
redirbuf()
print commands.getstatusoutput(r)[1]
print txtsplit
resetbuf()

def FileDir(x): return '/'.join(x.split('/')[:-1])
def ToFileDir(): command('lcd '+FileDir(current.buffer.name))

EOF

"""" Python Stuff
command! -range Pval py PyValOut(getSel(<f-line1>,<f-line2>))
map <leader>c :py vBuf().clear()<cr>
autocmd BufEnter *.py map <Leader>, :py PyExOut('python '+CB().name)<cr>
 
?

=?ISO-8859-1?Q?Gr=E9goire_Dooms?=

Kenneth McDonald wrote:
....
Thanks for the help. Of course, if you have other useful
suggestions for using Vim with Python, please feel free
to contribute those also.

I have been using ipython + vim for over a year now and I'm quite happy
with this.

I usually have both a vim and an ipython session running at the same
time when coding. When I want to test/debug, I close my vim session and
do the test/debug/edit cycle from within ipython. ipython has a special
command called '@edit file' which launches you $EDITOR, let you change
your code and runs the file once you exit the editor.

This plus the @pdb trick which launches pdb when an uncaught exception
is raised fullfill my needs.

Moreover, ipython has command and filename completion, shell like
commands (cd, ls, !), colorized magic help (object.method? pretty prints
object.method.__doc__), etc....

ipython: http://ipython.scipy.org/

Hope this helps
 
N

news

You have to tell vim you want it to treat what you type as a command.
Enter command mode by typing :
and allow vim to execute by adding ! directly after, then type the program
eg
:! hi_lowgame.py will run the hi_lowgame I have coded from inside vim.
regards
Darren
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top