Building a Java dev environment with Eclim and Vim on OSX

May 11, 2009

As anyone who has done Java development will tell you, doing it without a java-specific IDE can suck. So, enter Eclipse and Netbeans, probably the 2 biggest java IDEs out there (among many).

But I don’t want to use Eclipse or Netbeans. I want to use Vim.

Enter Eclim, which is a way to use Eclipse’s wide array of features in Vim. Check out the site, it gives a better idea of how it’s used than I can. Unfortunately, only binaries for Linux and Windows are available, so we’re going to have to do this the hard way on OSX. Let’s get started.

The first thing we’ll need is Eclipse. I’m using Eclipse 3.4.3, which I downloaded, uncompressed and moved to /Applications/Eclipse (so the full path is /Applications/Eclipse/Eclipse.app, this is important for later).

Next, let’s grab Eclim from the sourceforge site, the tar.gz file (not the .exe or the .sh). I’m using Eclim 1.4.7. Untar and wait a second, we’ll need to grab something before we can build Eclim.

Eclim requires Ant 1.7.1 in order to build (OSX ships with Ant 1.7.0 instead), so download the Ant 1.7.1 binary for OSX from Apache’s sites, untar, but don’t worry about installing it.

Lastly, grab the latest Vim 7.2 tarball from vim.org. Unfortunately, Apple decided not to compile the system vim with ‘sign’ support, so we’ll have to roll our own version of vim.

Now, let’s begin:

First, we’ll need a folder for the vim plugins, which needs to be created manually or Eclim will complain:

[~]% mkdir ~/.vim/eclim

Next, compile the Eclim files from the eclim directory using the ant 1.7.1 binary. In this example I’m only compiling the ant and jdt plugins for Eclim, because the CDT and PDT plugins require other Eclipse libraries, and I only use Eclipse for Java development anyway. Be sure to change your eclipse.home setting if you didn’t put it in the same place as me:

[~/code/eclim_1.4.7]% ../apache-ant-1.7.1/bin/ant -Declipse.home=/Applications/Eclipse/ -Dplugins=ant,jdt

The Ant command will build and install the files at the same time, so afterwards you shouldn’t have to manually install any Vim or Eclim files.

Next, we need to recompile Vim with sign support, here’s how I compiled my version, make sure to keep the –enable-gui=no and –without-x options, or else vim will start X11 every time you run in:

[~/code/vim72]% ./configure --enable-gui=no --without-x --with-features=huge --prefix=/usr/local
[~/code/vim72]% make
[~/code/vim72]% sudo make install

Switch out the system vim for the one we just created:

[~/code/vim72]% sudo mv /usr/bin/vim /usr/bin/vim.mac.old

We’ll need some way to start the Eclim daemon that was installed, and eclimd doesn’t care for symlinks, so I created an alias in my .zshrc (or .bashrc if you use bash):

alias eclimd='/Applications/Eclipse/eclimd'

Next, we need to fix the /Applications/Eclipse/eclimd shell script, because Apple’s readlink command does NOT support the -f option, so change this line:

RESOLVED=`readlink -f "$0"`

To this:

RESOLVED=`readlink "$0"`

Now, after all that, start up eclimd:

[~]% eclimd

You should see something like this (screenshot):

eclimd_started

Alrighty, now we can start Vim, give these two commands a try to see if eclimd communication can be established:

:PingEclim (Will output the Eclipse and Eclim version if everything is working)
:EclimValidate (You should get: “Result: OK, required settings are valid.”)

Hopefully you were able to get a connection. If not, leave a comment and I’ll try to help you out :)

Okay, so it’s set up, congratulations! Now what can you do with it, well I suggest checking out Eclim’s Java page to see what you can do, but the features I end up using the most are validation, completion and correction. Validation will validate the Java file every time you save it, marking the lines where errors in compilation occur. See the following screenshot (notice the red “>>” markers):

eclim_errors

Completion (through Ctrl+x-Ctrl+u) gives you the awesome completion that Eclipse has, like so:

eclim_completion

Correction allows you to go to a line with an error marker, and have Eclipse suggestion a fix, just like it would in the IDE, to apply the fix, put the cursor on the red line and hit Enter:

eclim_correction

Now go read all the documentation to see the neat things you can do :)

I usually end up doing the actual project management (adding files, adding external jars to the classpath, generating ant build.xml files and other gui-type stuff) from Eclipse still, but for the actual coding, I’m all about Vim.

Oh yea, one other thing I should mention, so Vim settings for your .vimrc that are really helpful for this:

If you use Supertab (tab-completion with tab instead of Ctrl+x-Ctrl+u):

" Supertab settings
" supertab + eclim == java win
let g:SuperTabDefaultCompletionTypeDiscovery = [
\ "&completefunc:<c-x><c-u>",
\ "&omnifunc:<c-x><c-o>",
\ ]
let g:SuperTabLongestHighlight = 1

And some Eclim .vimrc settings that I use:

" Eclim settings
" ,i imports whatever is needed for current line
nnoremap <silent> <LocalLeader>i :JavaImport<cr>
" ,d opens javadoc for statement in browser
nnoremap <silent> <LocalLeader>d :JavaDocSearch -x declarations<cr>
" ,<enter> searches context for statement
nnoremap <silent> <LocalLeader><cr> :JavaSearchContext<cr>
" ,jv validates current java file
nnoremap <silent> <LocalLeader>jv :Validate<cr>
" ,jc shows corrections for the current line of java
nnoremap <silent> <LocalLeader>jc :JavaCorrect<cr>
" 'open' on OSX will open the url in the default browser without issue
let g:EclimBrowser='open'

Enjoy!

tags: , , , , , ,
posted in development, eclim, ide, java, osx, supertab, vim by Lee

13 Comments to "Building a Java dev environment with Eclim and Vim on OSX"

  1. Jon Raphaelson wrote:

    Lee. I <3 you.

    Do you know if this stuff would work for the other languages Eclipse has support for? I’m thinking, it would be amazing to use this to get Vim completion and stuff for C.

  2. Jon Raphaelson wrote:

    Oh, and MacVim has +signs compiled in, so if you want the good GUI window (and the theme support that brings), you should just be able to go that way.

  3. Lee wrote:

    @Jon:
    Yep! You can get the C/C++ stuff by first installing the CDT plugins for Eclipse ( http://www.eclipse.org/cdt/ ), and then changing the ant build line to have -Dplugins=ant,jdt,cdt and it’ll build the C/C++ stuff also.

    The same applies for PDT (PHP) and the other languages Eclipse has plugins for.

  4. Sayandev Mukherjee wrote:

    Lee, I was trying to follow your instructions but the complication with ant fails with the following:

    BUILD FAILED
    /Users/sayan/eclim_1.4.9/build.xml:197: The following error occurred while executing this line:
    /Users/sayan/eclim_1.4.9/build.xml:340: Failure:

    Failed to compile eclim plugin ‘ant’.
    Check that your eclipse install has the necessary dependencies,
    or only compile eclim with the features that your eclipse version
    supports.

    For example:

    $ ant -Dplugins=ant,jdt

    Am I doing something wrong? Any help appreciated!

  5. Sayandev Mukherjee wrote:

    Sorry, I meant “compilation with ant” not “complication with ant”. For your reference, my ant command was the following:

    ../apache-ant-1.7.1/bin/ant -Declipse.home=/Applications/Eclipse/ -Dplugins=ant,jdt,cdt

    I have the Galileo Eclipse build on Leopard.

  6. Lee wrote:

    @Sayandev,
    According to the eclim mailing list, it looks like Eclipse 3.5 (Galileo) isn’t supported by Eclim except in the git repo so far.

  7. Ralph wrote:

    Lee,
    Thanks for this – it worked great. I am using the latest Eclipse (3.5). Also, I already have MacVim installed so I went that route.
    One issue I am having is enabling the completion. When I type Ctr-X,Ctr-U, I get the the following error:
    Option “completefunc” is not set
    I have set completefunc but to no avail. Do you know how to resolve this?

    Thanks,
    Ralph

  8. Ralph wrote:

    problem solved. I added ‘filetype plugin on’ to my .vimrc

  9. codekiln wrote:

    lee,
    we use all the same programs – and, it seems, for a lot of the same reasons. amazing. thank you for this article.

  10. Gus wrote:

    Thanks for the awesome post!

    However I’m facing a problem that I can’t figure out what’s wrong.

    I used some of your settings on my vimrc file to switch from Ctrl+x-Ctrl+u to Tab, it works… but the problem is that it doesn’t really work exactly the same way, when I use Ctrl+x-Ctrl-u I get more specific details of the methods I could use, such as return type, arguments, method signature. But with the tab feature it only shows up the methods name. do you have any idea what I am doing wrong? is there any other settings that I should set to make it work exactly the same?

    vimrc settings:

    ” Supertab settings
    ” supertab + eclim == java win
    let g:SuperTabDefaultCompletionTypeDiscovery = [
    \ “&completefunc:”,
    \ “&omnifunc:”,
    \ ]
    let g:SuperTabLongestHighlight = 1

    Thanks,

    Gus

  11. Lee wrote:

    @Gus: I would recommend trying a :SuperTabHelp command, which should list all the available completion types and add the one you’re looking for to the g:SuperTabDefaultCompletionTypeDiscovery variable.

  12. Eric wrote:

    And, is there a quick way without going to eclipse to create the project folder? I never really realized what the structure contained.

  13. AK wrote:

    Can’t I use Vim+eclim without Eclipse. I want to avoid Eclipse for its bulk and clutter. Or is there a way I can get a setup like Eclim without having actually to install Eclipse? As in using some other vim plugins/scripts etc?

 
Powered by Wordpress and MySQL. Theme by Shlomi Noach, openark.org