Vim Tips for Java #2: Using exuberant-ctags
ctags
is a great tool for programmers. It creates an index to your
source code to allow you to trawl through them for cross referencing.
While the way it works doesn’t look as snazzy as nicely formatted
javadoc output like Netbeans, it does its
job well enough for you to read up a field or method definition whenever
you need it. You’ll need to install exuberant-ctags separately, which
you can find on its website.
A requirement of using ctags, is that you’ll need to have the Java
source code available for ctags to parse them into a searchable index
file for vim. It’s usually located in your Java distribution at
$JAVA_HOME/src.zip
. Unzip the file, and in my case I extract it into
the $JAVA_HOME/share
directory I’ve created. Then run
exuberant-ctags
:
exuberant-ctags -R --language-force=java -f.tags /opt/sun-jdk-1.5.0.08/share/
This command generates a '.tags'
index file in my home directory. The
next thing to do is to allow vim to be able to locate and use the index
file. Add the following line into your .vimrc:
autocommand FileType java set tags=~/.tags
This should now allow you to jump to any definition in the Java API, whenever you need to look it up from your code. To test this, create a Java file and put some code in it:
public class TestClass {
String s = new String();
}
Move your cursor under to the word ‘String’ and press ctrl-]
. Voila,
you should now be reading into the source of.. not just yet, it is not
java.lang.String
! Most probably it is showing you some other classes
that has a String object in its field, which is probably not what you
are looking for. To cycle through the remaining matches, use the command
mode instruction ':ts'
to go through the list of matching tags and
select the right item you want.
You might also want to be familiarized with the navigation keystrokes,
by doing a ':help'
on ':ta'
, ':ts'
, 'CTRL-L'
, 'CTRL-]'
, which
may be useful.
A few dissatisfactions that I still have with this method:
-
It doesn’t allow me to read the embedded javadocs in code as html. It will be pretty cool if this feature can be linked to lynx and shown in a window within vim, but I’m not certain if it is possible;
-
I don’t believe that searching through all the miscellaneous fields of other classes is the best way of using ctags. There may be something that I have not mastered that permits me to fully utilize the power of ctags yet.
I’ll be looking further to see if I can learn more tips on using ctags, but in the meantime, if anybody have any suggestions for improvement, I’ll appreciate if can leave a suggestion, thanks!
If you like reading this, you may also enjoy:
- Vim Tips for Java #1: Build Java files with Ant Automatically
- Vim Tips for Java #3: Use Omni-Completion (or Intellisense) for Automatic Syntax Completion
- Vim Tips for Java #4: Using ‘tab’ for Syntax Completion
- Vim Tips for Java #5: Folding Code Blocks to Prevent Visual Blindness
- Vim Tips for Java #6: Auto-Bracketing Within Vim