I waste a lot of time between Vim and Python. I find it too slow to manually copy-paste from Python to Vim and vice versa. A good broken example is:
%
If you want to do some python calls without compiling vim with the python interpreter (that would allow you to write plug-ins in Python, and it's also needed for Omnicomplete anyway) you can try as this:
:.!python -c "import os; print os.getcwd()"
That would tell you where you are in the drive (current path).
Now let's number a few lines, starting from an empty file so we can see the result easily:
:.!python -c "for i in range(1,101): print i"
(vim numbers lines from 1 not 0) Now we have just the number of each line in every line up to line 100.
Let's now put a small script in your current path (as shown above) and run it, see how it works. Let's copy paste this silly one. In reality you will find most useful to do script that output one line per line, but you don't have to do that as this script shows:
print "hi"
try:
while True:
i=raw_input()
print "this was:",i
except EOFError:
print "bye"
So you can call, for instance (imagine you called it "what.py"):
:10,20!python what.py
(Note that tab completion of file names works, so you can verify it's actually in the path)
As you can see, everyline is fed to the script as standard input. First it outputs "hi", at the end "bye" and in between, for each line you output "this was: " plus the line. This way you can process line-by-line. Notice you can do more complex stuff than processing line by line, you can actually consider previous lines. For such stuff I'd rather import sys and do it like this:
import sys
print "hello"
for i in sys.stdin.readlines():
i = i.rstrip("\n") # you can also prevent print from doing \n instead
print "here lyeth",i
print "see you"
Hope that helps.
I think what you really want is explained here.
I am unsure of their usefulness, perhaps for further reading:
I think you't just missing the -c
flag. For example:
:.!python -c "print 'hello'"
You should not that the script that you provide acts as a filter on the line selection. That is, your script can read from stdin
to operate directly on the lines given (.
, %
, ...). Anything more than the simplest tasks, however, and you'd be better off putting the python commands into a script file of its own.
On Windows, if you are editing the python script, just do:
!start python my...
and press tab
to cycle along the available file names, until you find your match:
!start python myscript.py
It will run in a new cmd
window. Personally I prefer to do !start cmd
and from there run Python, since it is easier to debug from the eventual error messages.
I just wrote the following module that lets you edit a temporary buffer in vim directly in the Python interpreter.
http://philipbjorge.github.com/EditREPL/