vim-dispatch: I should have used this years ago
vim-dispatch is a plugin by Tim Pope that I’d heard a lot about but never really understood the benefits or what it offered me…until now.
Recently I’ve been working a lot with Makefiles. I’m writing my dissertation in Latex and compiling that through a Makefile, I have a couple of C projects which I’ve similarly used Makefiles to save me time, and I’ve also been using other build tools like Gulp, Grunt, or other command line tools like Bundle.
Essentially, something I have to do quite a lot is quickly drop out of Vim to run something, usually one of the aforementioned tools or often my tests too. The problem with these is that they take my focus out of Vim. Most of the time as well, I only care about the result of the program I ran if something went badly. If everything went OK, I don’t need to know. So taking my focus out of Vim and losing where I was isn’t worth it most of the time.
And that’s where Vim Dispatch comes in. It provides two commands. The first, :Make wraps around Vim’s :make, but runs it asynchronously, in the background. Then, once its done, it populates the quickfix list with any problems or errors. If nothing bad happens, you don’t get disturbed. And, most importantly, at no point is your focus taken from Vim. You can keep typing away and coding as your Make command runs in the background. The output from the command is added to the quickfix window, and the quickfix window is populated with the errors. If there is no error, there is no quickfix window population and nothing happens.
If you don’t want to run Make, but would like to run other commands asynchronously, that’s possible too. Dispatch provides a :Dispatch command for just this. Some examples:
:Dispatch bundle install
:Dispatch rspec %
:Dispatch grunt test
These work exactly as the :Make command, but will just run anything you pass it.
Depending on the context on which you run Vim, Dispatch will run what you give it differently. In tmux, which is where I spend most of my time, it will open a split and run it there. If you’re in iTerm on a Mac, it opens a new window. If you’re not in either, it will just run it in the background entirely. It also has Windows support.
And for those longer running tasks, you can use :Make! and :Dispatch!, which run in the background and don’t disturb you at all. When you need to get feedback from that task, you can hit :Copen to view any output.
I seriously recommend checking out Dispatch if you commonly find yourself jumping out of Vim to run a command. It’s really changed my workflow and decreased the time spent jumping out of Vim, which is a massive timesaver.
comments powered by Disqus
Leave a Reply