When Vim is compiled with Python support, you can script Vim with Python using the :python
command. How would I go about using this to execute the command and insert the result under the cursor? For example, if I were to execute :python import os; os.listdir('aDirectory')[0]
, I would want the first filename returned to be inserted under the cursor.
EDIT: To clarify, I want the same effect as going to the terminal, executing the command, copying the result and executing "+p
.
:,!python -c "import os; print os.listdir('aDirectory')[0]"
The following works fine for me: write the python code you want to execute in the line you want.
import os
print(os.listdir('.'))
after that visually select the lines you want to execute in python
:'<,'>!python
and after that the python code will replaced by the python output.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With