November 23, 2010

Perforce integration for Vim - courtesy of P4Python


Vim has long been my favorite text editor. (Arguing text editors is like arguing politics, so I won't bore you with the details of why Vim is clearly superior to all other editors.) Although I keep P4V running all day, and am fluent with the command line client, often I find myself wanting to run a quick Perforce command from inside Vim.

There are a couple of existing Vim scripts (here and here) that supply Perforce integration, and I've tried both of them. They have their pluses and minuses, but they both rely on invoking the command line client for every Perforce operation. That feels a bit slow and awkward at times.

So, I decided to try writing a new integration script using P4Python. Yes, among the many wonders of Vim is the fact that it supports Perl and Python embedded in Vim scripts. P4Python, one of Perforce's derived APIs, provides an efficient, comprehensive Perforce interface. Best of all, by using P4Python, Vim does not have to execute a separate process to perform Perforce operations.

I started by writing the operations I'd be most likely to use when working on individual files -- edit, filelog, submit, and so on. But the whole process was so easy, I ended up opening up the Command Reference and implementing a limited version of almost every Perforce command. Some of the commands won't be terribly useful from inside Vim -- I rarely feel like using dbstat when I'm editing text files -- but it shows you how easy and powerful P4Python is. I also experimented with drawing a very limited ASCII-art revision graph. If I want a full fledged revision graph, I'll go to P4V, but sometimes a quick peek at immediate integration history is handy. Plus, I can use it when running Vim in a console, when I don't have a GUI client available.

The Vim script and Vim doc are available in the Public Depot. Installation is similar to most Vim plugins, and is documented in the Vim doc.

Happy Vimming!