7

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?

Erik Cederstrand
  • 9,643
  • 8
  • 39
  • 63
Wet Feet
  • 4,435
  • 10
  • 28
  • 41
  • Something like [Picon](https://github.com/gokcehan/picon), which was recently seen on [r/vim thread](https://www.reddit.com/r/vim/comments/8aj4rr/picon_run_code_in_python_interactive_console/)? – Peter Rincker Apr 10 '18 at 18:57
  • `:w` save firstly, then `:!python %`(%-> filename) and you will get the result. – Cheney May 02 '18 at 13:11
  • @Cheney that is the opposite of what is being asked. – Max Power Oct 14 '21 at 01: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. – Max Power Oct 14 '21 at 01:40

4 Answers4

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.

DJMcMayhem
  • 7,285
  • 4
  • 41
  • 61
  • 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). – D. Ben Knoble Feb 23 '19 at 01:57
3

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

romainl
  • 186,200
  • 21
  • 280
  • 313
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.

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.

Bill Zhao
  • 31
  • 3