Zum Inhalt springen

Configure Vim for basic coding in Java

19/04/2014

I’m a heavy Vim user and had only a few experience with Eclipse. Fortunately I could avoid using Eclipse the most time because I’m usually coding in languages other than Java. The only occasion for me to touch Eclipse is when I code for assignments from college. I tried it out several times and every time it was a big mess. I can’t understand the idea behind Eclipse distinguishing packages and folders at all. The whole interface is filled up with uncomprehensive buttons and configuration options. And the biggest problem on top of all, I’ve ever encountered, was to use the Eclipse Git plugin, which MIGHT work well, if every person working on the project uses Eclipse. But as soon as there’s someone using an other editor, the package structure gets so messed up after synching with remote branch that you can spend hours to figure out how to tell Eclipse that a folder is actually a package. The simplest solution there was always deleting everything and creating a new project.
After going through this mess twice and spending hours trying to figure out how to fix it, I decided to drop Eclipse and get my Vim to work with Java.

So here we go:

1.Syntax check
For getting Vim to check the Java syntax I installed the plugin syntastic using the Vim plugin manager Vundle.

Bundle 'https://github.com/scrooloose/syntastic.git'

Syntastic provides 2 different syntax checkers for Java, checkstyle and javac. I tried out checkstyle but it didn’t work for me (I’m on Linux version 3.13.6-1-ARCH).
For using javac add following lines to .vimrc

"syntastic"                                                                                                             
let g:syntastic_java_checker = 'javac'
let g:syntastic_java_javac_classpath = "./lib/*.jar\n./src"

The javac_classpath should be set to the folder, where javac can find the .jar and the .class files. For setting classpath for multiple files like the junit-4.11.jar + hamcrest-core-1.3.jar (which javac should be aware of otherwise the test files won’t be checked correctly), seperat them using "\n".
I’ve figured this out using the Vim command:

:verbose function SyntaxCheckers_java_GetLocList

This function gets the javac classpath string set in .vimrc and splits it on „\n“ delimiter.

2.File browser
I got the Vim plugin NERD Tree, which is an excellent file browser.

Bundle 'https://github.com/scrooloose/nerdtree'

Adding following lines to .vimrc will enable the toggle shortcut CTRL-n

"nerdtree"                                                                                                                 
map <C-n> :NERDTreeToggle<CR>

To open the selected file in a new tab there’s the shortcut t.

PS: I’m aware of the plugin Eclim. But I think I can do better without all the „convenient“ stuffs available on Eclipse like auto completion or run the code in editor.

Kommentar verfassen

Hinterlasse einen Kommentar