Vim has a built-in way for executing programs, namely :!start foo for Windows and :!bar & for Unix. However, the problem I have with those is that they steal focus from Vim. I’d like to launch my build & test script, and while its working and showing output in its command window never have to manually switch back to GVim.
I have thought of 3 possible solutions:
- Use Auto-It/AutoHotkey to set focus back to Gvim. Unfortunately that only works in Windows and it’s brittle.
- Have another process watching my sourcecode, and let that process automatically launch the script when the sourecode changes. (Another process creating a new window does not steal focus). This is a bit gimmicky, as you need to hold off on writing files until you want to start the script, and sometimes write a useless change if you want to re-test even though nothing in the source changed.
- Use a client-server architecture, where Vim sends the command over a socket or something similar. This is actually my preferred solution, if there is any existing cross-platform way to do it. (I would prefer not having an extra GVim instance running for this though.)
So what would be a good way to do this? I have tried AsyncCommander but unfortunately it also steals focus.
When I’m not using a nice build system like
sbtthat watches source code and re-builds/re-tests for me, I tend to come up with hacks like this:Here hitting
F9updates a file. Pick any key binding you like.(and of course you could do the same with
imapfor vim’s insert mode as well.)Then all one needs is a shell script that watches this file and executes the
command that builds/tests your project. On unix, I’d just do:
Running that in a separate window will be neat, because your vim/gvim window will retain it’s focus throughout, while you just keep hitting
F9(or whatever) and glancing at this other window to see how your build went.If you’re up to writing a windows batch script (or powershell, I don’t know) version of that as well, you should be good to go.