Is there a good way to run programs from gvim without losing focus?

Vim has a built-in way to run programs, namely :!start foo

Windows and :!bar &

Unix. However, the problem I'm running into is that they steal focus from Vim. I would like to run my build and test script, and as long as its working and showing the output in its command window, I never have to manually switch back to GVim.

I thought of three possible solutions:

  • Use Auto-It / AutoHotkey to bring focus back to Gvim. Unfortunately this only works on Windows and it's fragile.
  • Take another process, observing the source code, and let that process automatically run the script when the sign changes. (The other process creating the new window does not steal focus.) This is a bit inconvenient as you have to delay writing files until you want to run the script, and sometimes write a useless change if you want to retest even if nothing in the original file has changed.
  • Use a client-server architecture where Vim sends a command over a socket or something similar. This is really my preferred solution if there is any existing cross-platform way to do it. (I'd rather not have an additional GVim instance for this, though.)

So what would be a good way to do this? I tried AsyncCommander but unfortunately it steals focus as well.

+3


source to share


2 answers


When I am not using a pretty build system, for example sbt

, which keeps track of the source code and rebuilds / re-installs for me, I tend to come up with hacks like this:

 :map <F9> :!touch and-go<CR><CR>

      

Clicking here F9

updates the file. Choose any key binding you like. (and of course you can do the same with imap

vim's insert mode.)

Then all you need is a shell script that looks at that file and executes a command that builds / checks your project. On unix, I would just do:



 while stat -c "%Y" and-go; do sleep 1; done | 
 stdbuf -i0 -o0 uniq -c | 
 while read l; do 
    echo build...;  # Your build/test execution commands here.
 done

      

Running this in a separate window will be neat because your vim / gvim window will keep it focused while you just keep clicking F9

(or whatever) and viewing that other window to see how your build went.

If you are going to write a windows script package version (or powershell I don't know) that should be fine too.

+3


source


I think this should work:

:silent! !cmd /c start /b dir

      



It also start.exe

has the ability to run with a minimum value rather than using a shared / background console.

Of course dir

should be replaced :)

+1


source







All Articles