I wanted to simply :!python % in order to run the file im using in vim, but the obvious conflict is that there's no way for me to distinguish python2 and python3 since including both to $PATH$ would result in two instances of python.exe.
Is there an elegant solution to this? I just want a quick way to run what I'm working on.
2 Answers
You can define two custom commands, e.g.:
:command! Python2 !C:\python2\python %
:command! Python3 !C:\python3\python %Alternatively, in case the Python version differs per project, I'd use one of the local vimrc plugins to set a buffer-local variable (say, b:python_version), and check that in a custom command:
:command! Python execute '!C:\python' . b:python_version . '\python %' There is no obvious conflict as it doesn't matter how many python.exes you have in your PATH, only the first one would be executed. The PATH works from left to right - once the executable is found, the process is invoked and the search discontinues.
So if you have C:\python27\ before C:\python3\, you would always execute python2.
Wikipedia
When a command is entered in a command shell or a system call is made by a program to execute a program, the system first searches the current working directory and then searches the path, examining each directory from left to right, looking for an executable filename that matches the command name given. Executable programs have filename extensions of EXE or COM, and batch scripts have extensions of BAT or CMD. Other executable filename extensions can be registered with the system as well.Once a matching executable file is found, the system spawns a new process in which to run it.
As a solution to this, I would agree with @IngoKarkat's solution - however, I would put that in my ~/.vimrc instead.