Tips for using Vim as a Java IDE?

157,949

Solution 1

Some tips:

  • Make sure you use vim (vi improved). Linux and some versions of UNIX symlink vi to vim.
  • You can get code completion with eclim
  • Or you can get vi functionality within Eclipse with viPlugin
  • Syntax highlighting is great with vim
  • Vim has good support for writing little macros like running ant/maven builds

Have fun :-)

Solution 2

I've been a Vim user for years. I'm starting to find myself starting up Eclipse occasionally (using the vi plugin, which, I have to say, has a variety of issues). The main reason is that Java builds take quite a while...and they are just getting slower and slower with the addition of highly componentized build-frameworks like maven. So validating your changes tends to take quite a while, which for me seems to often lead to stacking up a bunch of compile issues I have to resolve later, and filtering through the commit messages takes a while.

When I get too big of a queue of compile issues, I fire up Eclipse. It lets me make cake-work of the changes. It's slow, brutal to use, and not nearly as nice of an editor as Vim is (I've been using Vim for nearly a decade, so it's second nature to me). I find for precision editing—needing to fix a specific bug, needing to refactor some specific bit of logic, or something else...I simply can't be as efficient at editing in Eclipse as I can in Vim.

Also a tip:

:set path=**
:chdir your/project/root

This makes ^wf on a classname a very nice feature for navigating a large project.

So anyway, the skinny is, when I need to add a lot of new code, Vim seems to slow me down simply due to the time spent chasing down compilation issues and similar stuff. When I need to find and edit specific sources, though, Eclipse feels like a sledge hammer. I'm still waiting for the magical IDE for Vim. There's been three major attempts I know of. There's a pure viml IDE-type plugin which adds a lot of features but seems impossible to use. There's eclim, which I've had a lot of trouble with. And there's a plugin for Eclipse which actually embeds Vim. The last one seems the most promising for real serious Java EE work, but it doesn't seem to work very well or really integrate all of Eclipse's features with the embedded Vim.

Things like add a missing import with a keystroke, hilight code with typing issues, etc, seems to be invaluable from your IDE when working on a large Java project.

Solution 3

  • How do I invoke a maven task without leaving vi?

    Maven is no different than any other shell command:

    :!mvn

    You can :set makeprg=mvn if you already have a favourite key mapping for :make.

  • Can I get code completion?

    Yes, eclim is great, a bridge between vim's editing efficiency and Eclipse's Java language-specific awareness.

    <C-n> and <C-p> are not-so-great, but amazingly helpful.

  • How's the syntax highlighting?

    More than good enough for a regex-based highligher.

You may want to consider tools for other vim+java purposes, like code templates (snippetEmu—default snippets suck, but customizability shines), searching for usages and going to declarations (eclim, grep, ctags), generating getters and setters (java_getset, or eclim), automatic imports (eclim). You might also need a java shell for quick experiments (the BeanShell, a.k.a. bsh).

Solution 4

I have just uploaded this Vim plugin for the development of Java Maven projects.

And don't forget to set the highlighting if you haven't already:

enter image description here https://github.com/sentientmachine/erics_vim_syntax_and_color_highlighting

Solution 5

I know this is quite a few years later but here are some interesting plugins. I have not tried either of these yet so YMMV.

https://github.com/mikelue/vim-maven-plugin

https://github.com/vim-scripts/maven-ide

EDIT: Oh an BTW, i've tried eclim off and on, but the reason I like vim is its lightness. Executing eclipse even on headless mode is just too much mental lifting for me.

EDIT2: I've been using playframework lately and this will probably work with maven builds too:

  • For compiling, you can configure VIM's make to run maven or in my case, run a build script, tee that to a file.

    autocmd Filetype java setl makeprg=play_compile
    autocmd Filetype java setl efm=%A\ %#[error]\ %f:%l:\ %m,%-Z\ %#[error]\ %p^,%-C%.%#
    

"play_compile" is just a compile script. It uses SBT so Maven should work just fine here. Even direct javac will work. This way, you can use VIM"s quickfix buffer (:cnext, :clist: cprev, etc).

  • For jumping around the classes, I use ctrl-p. Its beautiful. Use it. Faster than eclipse in jumping around files.

  • For jumping around methods, I use tagsearch with exuberant c-tags. Jump into method declarations by using ctrl-]. Go back using Ctrl-o. Doesnt work as good as eclipse, but it works good enough.

  • I use supertab for code completion. Javacomplete is pretty slow, so I stick with omni-complete. Again, not as accurate as eclipse, but its fast and works good enough for me.

Share:
157,949

Related videos on Youtube

Allain Lalonde
Author by

Allain Lalonde

I'm a Software Developer working on Alternative User Interfaces for business projects. I love working on things that make you "Why?".

Updated on November 06, 2020

Comments

  • Allain Lalonde
    Allain Lalonde over 3 years

    I'm addicted to Vim, it's now my de facto way of editing text files.

    Being that it's mainly a text editor and not an IDE, has anyone got tricks for me to make it easier when developing Java apps?

    Some questions I have:

    • How do I invoke a maven task without leaving vi?
    • Can I get code completion?
    • How's the syntax highlighting?

    Anything else (other than "Don't do it!") that I should know about?

    • Allain Lalonde
      Allain Lalonde over 15 years
      I use netbeans extensively at work, but for my personal server at home... using NetBeans doesn't work well since I'd have to do my development on another computer and then deploy. The round trip is just too long.
    • DeaconDesperado
      DeaconDesperado about 11 years
      @AdamC Inbound from Google here... As a self-admitted command line geek coming from python, I think the barrier to entry in getting setup with a java development environment is greatly raised by needing such programs. I already have to install the JVM... why should one have to do even MORE installation just to get a hello world going? It's just the general disposition people coming from scripting languages are going to have when first learning java.
    • Matthew Kirkley
      Matthew Kirkley over 10 years
      When I am editing in VIM. To get back to the console you can just do ctrl+z which will put vim in the background and bring you back to the console. Then you can use the fg command to bring vim back to your foreground
    • fuzzyTew
      fuzzyTew over 8 years
      You don't have to ctrl=
    • fuzzyTew
      fuzzyTew over 8 years
      You don't have to ctrl+z/fg .. just type :!command and 'command' will be run. If you want a quick shell, type :!bash . then if you exit you go back to vim instead of leaving a dangling background job. You can get a shell in a vim window with plugins.
    • bananaforscale
      bananaforscale about 5 years
      SpaceVim has a general guide for a Java IDE, including layer configuration and usage. spacevim.org/use-vim-as-a-java-ide
  • Michael Ekoka
    Michael Ekoka about 14 years
    command to get help on java syntax highlighting. :help java.vim
  • Krzysztof Krasoń
    Krzysztof Krasoń over 12 years
    vote up for "^wf" I didn't know that there is such thing in vim :)
  • user2427
    user2427 over 12 years
    Can you explain more about ^wf? what it does?
  • GoingTharn
    GoingTharn over 12 years
    thanks for the BeanShell tip, I didn't know about that tool
  • exic
    exic over 11 years
    It opens the java file/class (filename extension .java is not needed) under the cursor in a new split window if it is able to find the file. That's why :set path=** is helpful.
  • n1te
    n1te about 11 years
    Another way to get Vim functionality within Eclipse is Vrapper. It has most of Vim key bindings and unlike viPlugin it's free.
  • taco
    taco over 9 years
    So is the ** literally two asterisks?
  • Donato
    Donato almost 9 years
    How about a debugger that supports step over, step through, etc?
  • lijat
    lijat over 7 years
    Where is ^wf documented so that I can read more about it?
  • JESii
    JESii about 7 years
    @lijat :help ^wf -- just like other vim help
  • johnchen902
    johnchen902 over 6 years
    Linux... symlink vi to vim Counterexample: Arch Linux
  • neoexpert
    neoexpert over 4 years
    vim help says: CTRL+w_f