Configuring Vim for Java Development, Part 1
I haven’t done full time Java
development in about 5 years. Back
then, I started by using NetBeans as my
development environment, but later transitioned to IntelliJ
IDEA. At the time, I thought IDEA was a
brilliant IDE, though like all Java desktop applications I’ve used, it was
slow, consumed memory like it was going out of style, and its non-native UI
never blended in well with the operating system.
During the five year hiatus from Java development, I became addicted to
Vim. I edit everything I can in Vim: code, email messages (via
mutt or
MacVim), documents (one of the reasons
I use LaTeX for work documents is so I can
use Vim to edit them), and the shell. My muscle memory is now mapped to
Vim, and it’s very uncomfortable to be stuck in a foreign editing
environment.
Now that I’ve returned to Java development, I’d like to get an environment
configured where I can use Vim as my primary editor. I’ve downloaded the
latest version of IDEA and gave it a test run. Someone wrote a Vim plugin
for it, but I’ve found it seriously lacking. Some of the most basic
navigation commands don’t work (Ctrl-D, Ctrl-U, Ctrl-W, and others), making
a typical editing session pretty frustrating.
I also briefly tried Eclipse, but quickly gave
up. My first impression of the UI is that it’s clunky and unintuitive, slow
(just starting the program on my Mac requires the Dock icon to bounce
roughly fifty thousand times), and the “workspace” paradigm doesn’t make a
whole lot of sense.
I’m now back to using Vim, specifically
MacVim, a great Vim port for the Mac.
I’ve searched around on the Internet for different ways to configure Vim
for Java. They’re of widely varying quality, and can take some tinkering to
get to work in a general way. I figured I’d start sharing those tips I find
useful here as I collect them.
The first one I’ve found is just a simple configuration to compile your
Java source code within Vim via an Ant build
file. I’ve seen many different configurations floating around on the
Internet, but I think I’ve put one together that will work for multiple
projects (no hardcoded directory paths), is simple (minimizing convoluted
configuration commands), and lets you move among compiler errors within
Vim.
To compile your Java source code via an Ant build file, add the following
line to your .vimrc
file:
autocmd BufNewFile,BufRead *.java set makeprg=ant -emacs -find build.xml build
This line sets Vim’s make
command to call Ant directly,
instructing it to search the current and parent directories for the build
file. My build file is called build.xml
. I also pass it the Ant target
that actually does the compilation – build
in my case – though I plan
to look into parameterizing this command so I can potentially run unit
tests or perform other commands without filling my .vimrc
with lots of
duplication.
The -emacs
portion of the command passed to Ant gets javac
to print
compiler errors “without adornments.” This should help you avoid the need
to add ==convoluted expressions== to your .vimrc
to enable Vim can parse
the compiler errors.
You can now perform a build from within Vim by typing the following:
:make
When the build is complete, you can navigate any compiler errors with the
following commands:
Enter
: Go to the first error message.:cnext
: Go to the next error message.:cprevious
: Go to the previous error message.:cc
: Vim will shorten the error message if needed to fit within the
space available. This command will show the whole error message.:clist
: List all compiler errors.:clist!
: Shows all lines of the compiler output, not just those bits
that Vim recognized.:cfirst
: Go to the first error message.:clast
: Go to the last error message.:cc 2
: Go to error number 2.
See the Vim
documentation for the full
range of commands.
I’ll continue to post more as I continue to flesh out my configuration.
Configuring Environment Variables on Mac OS X
In our time it is broadly true that political writing is bad writing. Where it is not true, it will generally be found that the writer is some kind of rebel, expressing his private opinions, and not a ‘party line’. Orthodoxy, of whatever colour, seems to demand a lifeless, imitative style.