4

I have tried Googling but I couldn't find anything relevant.

Is it possible to program in Python on VIM and do something like

:!iPython

to have Python execute the code?

Last time I tried that, it just opened up Python in the terminal.

2 Answers 2

5

Save the current buffer with :w, then :!/usr/bin/env python %.

10
  • Then that will execute the program or the command in the shell whichever I have written?
    – dustin
    Mar 27, 2013 at 15:06
  • It will run the file you are currently editing (presumably a Python program) in Python. Give it a try.
    – tgies
    Mar 27, 2013 at 18:15
  • I just tried it. It executed and then said hit enter to return. So everything went through but python didn't come up and show anything.
    – dustin
    Mar 27, 2013 at 18:51
  • now I am receiving invalid syntax errors for something I ran yesterday with no problems. Is there anything that needs to be setup in /.vimrc for this to work?
    – dustin
    Mar 27, 2013 at 19:17
  • No. If you are receiving invalid syntax errors, it's probably because you have invalid Python syntax. Nothing about how vim is configured will impact that.
    – tgies
    Mar 27, 2013 at 19:25
2

or even better set aw in your .vimrc, follow by

:pyf %

this will not incur you to bring up shell and hence better.

if you want you can map it and save in your .vimrc following cmd

map <F8> pyf %<CR>

hitting F8 will execute your python file

1
  • This is not quite the same as using /usr/bin/env python - :pyf will always use /usr/bin/pythonX (depending on python3 vs python2).
    – muru
    Jan 9, 2016 at 9:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .