1) r!
:r! python test.py
Let’s say test.py is a file with print(123) inside it.
This will run the existing file with Python. It will enter the output in Vim. Here is the breakdown:
- : (command line)
- r reads
- ! executes linux command
- python runs python with test.py file
2) !! (Bang Bang)
:.! python test.py
Alternatively, if you type !! in normal mode, this will type ! with a dot in command line like above. The same outcome will be achieved. Here is the breakdown.
- . enter to current line
- ! execute system command
- python runs Python
- % feeds current file to Python
3) Feed Python with a Quick Command
:.! python <<< "print(123)"
Previous outcome can be matched with Python command execution style mentioned here.
- <<< feeds Python code from system’s temporary file system to Python Interpreter.
- “” inside the quotes our content for Python command is formed
- print(123) actual Python command itself
This is incredible as so many systems are used on the fly and in harmony. Vim, Linux OS and Python are utilized in two words and a few characters and the program’s result is written in the same file.
4) Feed Python with the Existing File
:.! python %
Finally, you can feed the existing Vim file to the Python directly and insert the output from Python on the cursor’s line. For this task, you need to feed % to Python representing the whole current file.
- . enter to current line
- ! execute system command
- python runs Python
- % feeds current file to Python
This is really next level scripting pipeline. I always appreciated Vim’s intuitive style for writing code and human language. When mix n matched with Python or Linux commands it gets even more impressive.