Saturday, August 11, 2007

Vim Tips for Java #3: Use Omni-Completion (or Intellisense) for syntax completion

As much as I like vim, Netbeans trumps it when we talk about automatic syntax completion. Because of the vast amount of API that is present for the Java platform, the ability of having auto-suggestion for syntax is a godsend.

While things like looking up an API call while you are in the thick of coding are irritating, the effects of such distractions are often much more profound. A recent article by the New York Times suggests that such interruptions do not just result in the reduction of your efficiency, but increase the chances of making mistakes as well.

In vim 7.0, there is a facility that allows you to have auto-suggestion capabilities, which comes bundled for a number of languages like Ruby, Python, etc, but however not for Java. If you have followed my tip in using exhuberant-ctags for Java, the modification will allow vim to have primitive syntax completion capabilities, by using <CTRL-P>. In this case, it tries to match a word you've typed with all the keywords found in ctag's tag file.

The problem with this approach, is that it performs 'dumb' keyword completion of previous matching patterns, rather than the 'intelligent' contextual syntax completion. This can lead to mistakes in suggestions, which can be rather frustrating.

For true contextual syntax completion, there are a few plugins out there works for Java. By and large, they aren't as polished as Netbeans, but still, they are quite functional and and usable. I've used a number of them in the past before, and the one that I reccomend for you to install is vjde.

(An Image of Onmi-Completion for Java in Action)

While the plugin is quite usable, the documentation can use some working on. The default instructions requires you to install it in the /etc/vim global directory, something I'm not willing to do in fear of of messing up my default installation's configurations. Fortunately instructions for local installation is pretty straightforward:

1) Download the latest copy of vjde;
2) Uncompress the downloaded .tar archive;
3) Copy the autoload/, compiler/, doc/, plugin/, syntax/ directories to your $HOME/.vim/ directory;

In order to use the completion, use the <CTRL-X><CTRL-U> keystroke, which will provide you with an Intellisense-like syntax completion capability. Have fun with your new syntax-completion capability, and keep on vimming!


If you like reading this, you may also enjoy:

3 comments:

pipping said...

Life Changing!

Ivo Jiménez said...

Hi,

Thanks so much for this, I was really desperate because I couldn't find something like this.

One question though, it's the autocompletion supposed to work with custom classes or only with system classes?

Cheers

David Kavanagh said...

Hi, I'm going to try this out. If you're still doing java with vim, here's something I developed that worked great, when coupled with a vim ant plugin;
http://code.google.com/p/hotswap/
I could edit code, hit a key combo to invoke an ant target which would compile the files that had changed and swap the classes in the running VM.

Post a Comment