Is there a clean alias that I can put in my vimrc that will run the current file using python if there is a python extension?
At the moment I have nnoremap <leader>r :!%:p<Enter>
as suggested in this question (when I hit \+r it runs the open file). It works great for bash files, where the convention is to add a shebang line and make the file executable, but for python files it obviously exits 1.
As a workaround I have added a new command - command PyBang :call append(0, "#!/usr/bin/env python")
- that adds a python shebang to the top of the file. The problem is that this is not really convention for .py files and making them all executable seems like a lot of effort for some reason.
So I am struggling to write an alias that will run :!%:p<Enter>
if there is no extension and prepend python
if there is a .py
extension on the file in the buffer.
Any ideas are really appreciated.