tags:

views:

133

answers:

2

I'm editing a file in ~/Documents. However, my working directory is somewhere else, say ~/Desktop.

The file I'm editing is a Python script. I'm interested in doing a command like...

:!python

without needing to do

:!python ~/Documents/script.py

Is that possible? If so, what would be the command?

Thank you.

+10  A: 

Try: !python %

phi
+6  A: 

G'day,

I quite often map a key to do this for me. I usually use the F5 key as that has no command associated with it by default in vim.

The mapping I like to use is:

:map <F5> :w<CR>:!python % 2>&1 \| tee /var/tmp/robertw/results<CR>

this will also make sure that you've written out your script before running it. It also captures any output, after duplicating stderr onto stdout, in a temp file.

If you've done a:

:set autoread

and a:

:sb /var/tmp/robertw/results

you will finish up with two buffers being displayed. One containing the script and the other containing the output, incl. errors, from your script. By setting autoread the window displaying the output will be automatically loaded after pressing the v key.

A trick to remember is to use cntl-ww to toggle between the windows and that the mapping, because it refers to % (the current file) will only work when the cursor is in the window containing the Python script.

I find this really cuts down on my code, test, debug cycle time.

HTH

cheers,

Rob Wells
The v key by default starts characterwise-visual mode.
@blixtor, oops, you're right. I'm so used to using visual block (cntl-v) I forgot about simple visual mode. I'll update.
Rob Wells