Vim Tips Wiki

Some Java tips

1,623pages on
this wiki
Add New Page
Add New Page Talk0
Tip 385 Printable Monobook Previous Next

created December 9, 2002 · complexity intermediate · author demian l neidetcher · version 6.0

I started to aggregate some Java and Vim tips (in part because I easily forget stuff). They're the kind of tips that are a little elusive but deliver a big bang for their effort; like actually getting quickfix to work with Java, using ctags with Java, etc.


You can avoid using

autocmd BufRead *.java ...

commands by using filetype plugins. See :help filetype. These files will be sourced only when opening a source file of the corresponding file type.

Wondering if others are having luck with the quickfix stuff on Win2000? Almost everything works except going to the error after building. I can do :clist to get all errors and the ant printout exists but all javac stuff is missing, which means it is filtering javac comments/errors but not returning them to buffer. Any pointers?

Try the following:

if executable("jikes") || executable("jikes.exe")
  nn<silent><buffer> [1
    \:let&l:mp='jikes -nowarn -d <C-R>=$cp<CR> -Xstdout +E %:t'<CR>
elseif executable("javac") || executable("javac.exe")
  nn<silent><buffer> [1 :let&l:mp='javac -d <C-R>=$cp<CR> %:t'<CR>

Nice page of tips. One problem, your gc mapping doesn't really work, because gd depends on [[ (:help gd; :help [[), which only finds the beginning of a function if you put your opening curly braces on a line of their own. Many people don't.

That is, it will work in functions like this:

public void function(String args)
  // function code

but not in functions like this:

public void function(String args) {
  // function code

So basically you'll get something totally wrong if your instance variable name occurs anywhere before the function.

I have been reading all the tips about making quickfix work but there is something that I miss. I use ant 1.5.1, vim 6.1 and javac (yes its slow i know)

i have the following line in my .vimrc file

set makeprg=ant\ -emacs\ -q\ -find\ build.xml\

I read somewhere that if you use the -emacs option you would not have to mess with the errorformat. but guess what, it does not work. it will give me zero erros. however, if i have ant dump the stuff to a file it clearly shows that there are errors in the file. the dump looks like this:

C:\private\programmering\test of ant\src\ ';' expected
C:\private\programmering\test of ant\src\ cannot resolve symbol
symbol : variable f
location: class HelloWorld
2 errors
file:C:/private/programmering/test of ant/build.xml:18: Compile failed; see the compiler error output for details.
Total time: 2 seconds

so why doesnt these errors show up when i do clist???

i have also tried not using the -q and -emacs option with the following errorformat

set efm=%A\ %#[javac]\ %f:%l:\ %m,%-Z\ %#[javac]\ %p^,%-C%.%#

when compiling it will report lots of errors but no real ones. it will falsely report the following:

1: Searching for build.xml ...
2: Buildfile: c:\private\programmering\test of ant\build.xml
4: init:
6: compile:
7: [javac] Compiling 1 source file to c:\private\programmering\test of ant\build

and thats it... no mention about the real errors...

btw, im running xp but i have been trying it on win2k as well.

on the problem of making the quickfix work on Win2000 (or XP): I had the problem mentioned by Jonas, and setting

set shellpipe=>%s\ 2>&1

fixed it. (Note that this is the value for shellredirect.)

Another useful Java setup I use is to build a tags file for the source code of the JDK and have vim use it. The JDK source code can be downloaded from Sun.

Then I build two tag files, one for my project, one for the JDK:

pushd $HOME
ctags -f jdk_tags -R $JAVA_HOME/src
ctags -f proj_tags --exclude=CVS -R $PROJ_HOME/com $PROJ_HOME/webapps

In .vimrc, I tell Vim to use the JDK tags first, then my Project:

set tags=$HOME/jdk_tags,$HOME/proj_tags

This is useful when using tags for completions, along with tagjumping for definitions (and see how things are implemented by Sun).

Along with this, I use the dictionary keyword completion on a per-buffertype basis and the smart tab completion metioned on the above webpage.

My completion is set like so:

set cpt=k,.,w,b,u,t,i

Which means that when I start to type something and hit Tab, it will essentially first go through the Java keywords, then the JDK source, then my project source.

These are also helpful Java settings:

" Highlight functions using Java style
let java_highlight_functions="style"
" Don't flag C++ keywords as errors
let java_allow_cpp_keywords=1

And this abbreviation is cool as well:

iab ff for (Iterator i =; i.hasNext())  {<LEFT><LEFT><LEFT><LEFT><LEFT><LEFT><LEFT><LEFT><LEFT><LEFT><LEFT><LEFT><LEFT><LEFT><LEFT><LEFT>

At some point along the way, Vim included an ant.vim compiller script that does the set makeprg/errorformat for ant and javac, respectively. It can be enabled like this:

:compiler ant

Unfortunately, this seems to not persist through buffers during the quick fix mode. So you can do this:

:au BufReadPost *.java exe ":compiler ant"

Which will do a :compiler ant on every buffer reading in a .java file.

For me, because I need to pass in some extra args to ant every time I compile, I defined a function with those arguments:

function! BuildJar(jarname)
exe "make -Dtarget=jar -Dversion=DEV ".a:jarname

And then defined a command to run it:

:command -nargs=1 Ant :call BuildJar(<f-args>)

And then an abbreviation because I don't want to have to type 'Ant' all the time.

:cab ant Ant

So in Vim I do this:

:ant target

And it runs ant with my args in quick fix mode.

I recently added this:

autocmd BufReadPost *.java exe "set dict+=".escape($VIMRUNTIME.'\syntax' .&filetype.'.vim',' \$,')

To use the standard Java syntax file for a dictionary for *.java files.

The link in this tip is dead. Try:

Thanks everyone! I found all of this very useful.

In particular, in my vimrc I put the following:

:compiler ant
:set makeprg=ant\ -find\ 'build.xml'

This is with ant 1.6.2. The -find tells ant to search all of your parent directories until it finds a build.xml file.

I've then set a bunch of mappings for some common ant targets across the projects I use:

map <unique> ,ac <Esc>:make compile<CR>
map <unique> ,ad <Esc>:make dist<CR>

That's the basic idea anyway. For those using ctags, there's also a similar program called jtags that works well for me.

One other setting I use after running :compiler ant

:set shellpipe=2>&1\ \|\ tee

Without doing this, the quickfix commands (cl, cn, cp) do not work correctly for me. It basically thinks the ant output is part of the error messages.

If you use:

set tags=tags;

in your .vimrc it will search from the current file directory up to the root directory for a file called 'tags'.

That way you can avoid having the directoryname of your project in your .vimrc. (Which is very handy when your are working on multiple projects at the same time!)

I had to set suffixesadd to get the gf to work.

autocmd BufRead *.java set include=^#\s*import
autocmd BufRead *.java set includeexpr=substitute(v:fname,'\\.','/','g')
autocmd BufRead *.java set

I'm using Ant 1.6, javac 1.4.2, and Cygwin. I ran into a similar problem as Jonas'. It seems the problem was that javac returned Windoze style paths, and vim did not recognize or parse the strings as filenames.

The basic fix is that you will need to correct paths in the error messeges (on the fly) from...

[javac] C:\Some\Path\ something that vim can understand, like...

[javac] /cygdrive/c/Some/Path/

I just used a quick shellpipe hack like this:

set shellpipe=\|\ \s\e\d\ \-\r\ '\s\/\c\:\\\\\/\\\/\c\y\g\d\r\i\v\e\\\/\c\\\/\/\i'\ \|\ \s\e\d\ \-\r\ '\y\/\\\\\/\\\/\/'\ \|\ \t\e\e

which is really just doing (unescaped):

| sed -r 's/c:\\/\/cygdrive\/c\//i' | sed -r 'y/\\/\//' | tee

This is probably not the most elegant fix :) Also, I'd avoid using spaces in your paths and filenames...not sure if they will need to be escaped as well.

Presuming that your Java source is collected under a directory named src AND that the package structure reflects the directory structure AND you're on a *nix box (or one that uses / as a file separator), this abbreviation may be useful (insert ^V as needed):

ab MAKPKG ^[:r !pwd^M:s:/:.:g^M:s/^.*\.src\./package /^MA;^M

(I suppose one could make it work with \r and such instead of ^V^M... I just haven't tried that yet.)

Also on Fandom

Random Wiki