Running Python code in Vim without saving

You have already answered your own question:

:w !python

will run the file in python without saving it. Seriously, test it out yourself! make some changes, run :w !python and then after it runs, run :e!. It will revert all of your changes.

The reason this works is because :w does not mean save. It means write, and by default, it chooses to write the file to the currently selected file, which is equivalent to saving. In bash speak, it's like

cat myfile > myfile

But if you give an argument, it will write the file to that stream rather than saving. In this case, your writing it to python, so the file is not saved.


I wrote a much longer answer on this topic here.


You seem to be confusing :w[!] filename and :w !command.

The former writes the buffer to file filename whereas the latter passes the content of the buffer to command command.

The former could eventually lead to data loss but the latter can't (as long as you don't do crazy things incommand).

enter image description here

Tags:

Python

Vim