Program Python in VIM

P

Peter Wu

I'm giving vim a try to program Python. The following are the steps I
follow to code/test a python program.

vi test.py
[key in some python code]
:wq
:!python test.py


Is there any other way? I don't want to type 'python test.py' every time
I've made any modifications. In Emacs, I can simply fire C-c C-c to fire
the python interpreter. Thanks!
 
?

=?iso-8859-1?Q?Fran=E7ois?= Pinard

[Peter Wu]
I'm giving vim a try to program Python. The following are the steps I
follow to code/test a python program.
vi test.py
[key in some python code]
:wq
:!python test.py
Is there any other way? I don't want to type 'python test.py' every time
I've made any modifications. In Emacs, I can simply fire C-c C-c to fire
the python interpreter. Thanks!

I do not know if there is a better way, but what I do for one is to
create a small Makefile like:


try:
python test.py


(there is really a TAB above), and then, from `vim', do `:make'.
 
C

Chris Raspe

from the VIM FAQ...

30.1. Can I run a shell inside a Vim window?

Currently Vim doesn't have support for running shell and other external
commands inside a Vim window.

For more information, read

:help shell-window

Alternatively, you can try using the Unix "screen" utility or the 'splitvt'
program.

You can also use the vimsh plugin by Brian Sturk to run a shell in a Vim
window. To use this you need to have Vim built with python support. For
more information visit the following URL:

http://vim.sourceforge.net/scripts/script.php?script_id=165



On Thu, 22 Jan 2004 11:17:56 -0500, Peter Wu wrote
 
P

Paul Moore

Peter Wu said:
I'm giving vim a try to program Python. The following are the steps I
follow to code/test a python program.

vi test.py
[key in some python code]
:wq
:!python test.py


Is there any other way? I don't want to type 'python test.py' every time
I've made any modifications. In Emacs, I can simply fire C-c C-c to fire
the python interpreter. Thanks!

There's always

:map ^C^C :w^M:!python %^M

which makes C-c C-c write the file and then run it in Python.

Paul.
 
B

Brian Victor

François Pinard said:
I do not know if there is a better way, but what I do for one is to
create a small Makefile like:

try:
python test.py

I use a similar Makefile, and then I bind F4 to "make" in my .vimrc:

map ^[[14~ :make^M

To make that work, you have to make liberal use of control-V. Type:

map <ctrl-V><F4> :make<ctrl-V><enter>

into your .vimrc, and F4 should be bound to the make command.
One-keystoke running! (Does that violate an amazon patent?...)
 
A

Alan Gauld

Peter Wu said:
vi test.py
[key in some python code]
:wq

You should only need :w here. The q will close the window which
presumably you don't want?


:!!

repeats the last command.

However if you are using vim in a *nix terminal rather than the
GUI version you could use CTRL-Z to suspend vim then run python
foo.py from the shell prompt. Resume python, edit and CTRL Z
again. !! at the prompt will repeat the last run command.


:!! is probably the nearest direct equivalent.

Alan G.
Author of the Learn to Program website
http://www.freenetpages.co.uk/hp/alan.gauld
 
E

Eric Wilhelm

Peter Wu said:
I'm giving vim a try to program Python. The following are the steps I
follow to code/test a python program.

vi test.py
[key in some python code]
:wq
:!python test.py


Is there any other way? I don't want to type 'python test.py' every
time I've made any modifications. In Emacs, I can simply fire C-c C-c
to fire the python interpreter. Thanks!

There's always

:map ^C^C :w^M:!python %^M

which makes C-c C-c write the file and then run it in Python.

This is good, but what about starting your file with #!/usr/bin/python,
setting it as executable with :!chmod 755 %, and then leaving python out
of the "run this file" line? Advantage here is that you can then be a
Perl programmer as well, or even toss-off some of those really simple
deals in Bash script.

Another suggestion: maybe leave the ^M off of the end so that you can ^c^c
<enter> or add some arguments if needed.

Of course, if you want emacs-style bindings, you could just use emacs:)
I'd be more inclined to connect this one to something closer to the
escape key and lose the bucky bits.

--Eric
 
M

Mike Huffman

:!python test.py
:!!

repeats the last command.

% is the symbol for "current file name," so I use:

:w
:!python %

then thereafter in the current vi session:
:!!

It would be nice to be able to combine the write and shell command in
one single command...

Mike
 
S

Scott F

(e-mail address removed) (Mike Huffman) wrote in

:w
:!python %

then thereafter in the current vi session:
:!!

It would be nice to be able to combine the write and shell command
in one single command...

This should do it.

map <somekey> :w<CR>:!python %<CR>

Scott
 
M

Mike Huffman

Scott F said:
This should do it.

map <somekey> :w<CR>:!python %<CR>

It does indeed, Thanks!

Unfortuantely I do not use vi consistently enough to ever get beyond
the basics. Had some false starts finding a good key to map that
worked on all my vi installations (Linux, Cygwin, Windows console);
settled on Ctrl-K: unused and easy to type.

Actually, at the moment I am using the maping for Perl, but don't tell
anyone :)

Mike
 

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

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top