8

Is there a way to run my current python code in vim without making any changes to the file? Normally, when I want to test my code from within vim, I would execute this:

:w !python

However, this overrides the current file I am editing. Often, I add print statements or comment stuff out to see why my code isn't working. I do not want such changes to overwrite a previous version of whatever .py file I'm currently working on. Is there a way to do so? Perhaps a combination of saving to a temporary file and deleting it afterwards?

4
  • Something like Picon, which was recently seen on r/vim thread? Commented Apr 10, 2018 at 18:57
  • :w save firstly, then :!python %(%-> filename) and you will get the result. Commented May 2, 2018 at 13:11
  • @Cheney that is the opposite of what is being asked. Commented Oct 14, 2021 at 1:37
  • This is odd, when I do :w !python3 I get a python error. vim help indicates this should either feed the buffer into the external command or write to a temp file and feed that to the external command. Commented Oct 14, 2021 at 1:40

4 Answers 4

10

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This is old, but your bash command probably doesn’t have the intended effect (I believe on some systems, it can empty the file; correct me if I’m wrong).
4

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

Comments

0

You can probably run :sav other_filename or :w %:h/other_filename, the first saving a copy and opening it, the second just saving a copy, then run with the command you're already using. then you can delete whatever file you don't want.

untested though, so be warned.

Comments

0

You could add a map to do this, which will save a lot of time.

:map \b :w<CR>:!python %<CR>

Type \b will save the file and run your current saved code.

:map \bb :!python %<CR>

Type \bb will run the unsaved code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.