Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing Python with Gvim

  • open gVim.
  • then using the File Menu and MenuItem Open to open a file pi.py which has the following tiny script:

enter image description here

How do I execute this code using gVim?


EDIT

If I use either :! python pi.py or :w !python - then I get the following:

enter image description here

like image 503
whytheq Avatar asked May 05 '13 14:05

whytheq


People also ask

Can you write Python in Vim?

The improved version of vi editor is Vim that can be used for creating or editing source codes of different types of programming or scripting languages. It is a configurable text editor and works faster than other command-based text editors. It can also work with various plugins and vimscript.

How do I create a new Python file in Vim?

Write Your Python Script To write in the vim editor, press i to switch to insert mode. Write the best python script in the world. Press esc to leave the editing mode. Write the command :wq to save and quite the vim editor ( w for write and q for quit ).


3 Answers

You don't need to save the file, you can run the current buffer as stdin to a command such as python by typing:

:w !python -

(The hyphen at the end probably isn't necessary, python will generally use stdin by default)

edit: seeing as you are new to vim, note that this will not save the file, it will just run it. You will probably want to learn how to save your file.

like image 189
Will Hardy Avatar answered Oct 19 '22 10:10

Will Hardy


If you have python support compiled into vim you can use :pyfile % to run the current file. (python 2.7)

If you have python 3 support use :py3file % instead

pyfile help

like image 35
FDinoff Avatar answered Oct 19 '22 10:10

FDinoff


Something like this. Type following command in vi command model.

:! python test.py
like image 4
Yarkee Avatar answered Oct 19 '22 09:10

Yarkee