JavaImp.vim : Insert and sort Java import statements, view JavaDoc from class name, and more.
| script karma
Downloaded by 3795
Comments, bugs, improvements
script versions (upload new version)
|This script can generate a Java class name to package mapping file from multiple source paths. Once you have create the mapping file, you can use this file to insert import statements in your .java file using a simple key mapping. It can also sort your imports too!
JavaImp can also be integrated with JavaDoc, where you can lookup APIs very quickly by just hitting a hot key on a class name. This script can save the time you spent looking up docs and typing in routine import package names. Give it a try!
- It's recommended that you have the Unix "sort" binary (or the Windows's
"sort" or cygwin's "sort" will do) in your path. For the jar support you
also need to have the
"jar" binary in your path.
- To use JavaDoc viewing, you probably need to have a pager like "w3m" or
"lynx". You can also use your usual web browser for this. You do not
really need this if you do not use the JavaDoc viewing feature.
Put this file in your ~/.vim/plugin directory.
You need to set two global variables in your .vimrc in order for this to
let g:JavaImpPaths = "..."
let g:JavaImpDataDir = "..."
The g:JavaImpPaths is a comma separated list of paths that point to the
roots of the 'com' or 'org' etc.
For example: if you have your Java source files in
/project/src/java/com/blah and /project2/javasrc/org/blah...., you'll put
this in your .vimrc file:
let g:JavaImpPaths = "/project/src/java,/project2/javasrc"
If there are too many paths, you can split them into separate lines:
let g:JavaImpPaths = "/project/src/java," .
\ "/project2/javasrc," .
Note: Don't forget the ',' to separate the paths.
If ',' is not convenient for you, set g:JavaImpPathSep to the
(single-character) separator you would like to use:
let g:JavaImpPathSep = ':'
The g:JavaImpDataDir is a directory that you use to store JavaImp
settings and cache files. Default is:
let g:JavaImpDataDir = $HOME . "/vim/JavaImp"
Note: Since version 2.1, the "g:JavaImpDataDir" variable replaces
"g:JavaImpClassList" and "g:JavaImpJarCache". If "g:JavaImpClassList" and
"g:JavaImpJarCache" are not set, then they default to
"g:JavaImpDataDir/JavaImp.txt" and "g:JavaImpDataDir/cache/" accordingly.
The files and directory will be created automatically when you generate
the JavaImp.txt file. It's recommended that you to use the
g:JavaImpDataDir variable instead.
Now you are ready for some actions. You can now do a:
:JavaImpGenerate or :JIG
If you have not created the directory for g:JavaImpDataDir yet, this will
create the appropriate paths. JIG will go through your JavaImpPaths and
search for anything that ends with .java, .class, or .jar. It'll then
write the mappings to the JavaImp.txt and/or the cache files.
After you've generated your JavaImp.txt file, move your cursor to a class name
in a Java file and do a:
:JavaImp or :JI
And the magic happens! You'll realize that you have an extra import
statement inserted after the last import statement in the file. It'll
also prompts you with duplicate class names and insert what you have
selected. If the class name is already imported, it'll do nothing.
will do a similar thing with less verbosity. This is useful to be used in
You can also sort the import statements in the file by doing:
:JavaImpSort or :JIS
JavaImp will try to find the source file of the class under your cursor
:JavaImpFile or :JIF
Doing a :JavaImpFileSplit or :JIFS will open a splitted window on the
If you want to use the JavaDoc viewing feature for JavaImp, you should set
g:JavaImpDocPaths. Similar to how you set the g:JavaImpPaths,
g:JavaImpDocPaths contains a list of root level directories that contains
your java docs. This, together with a HTML pager (like w3m or lynx on
Unix), let you view the JavaDocs very quickly by just hitting :JID on a
class name. For example, you can set:
let g:JavaImpDocPaths = "/usr/java/docs/api," .
The default pager is set to:
let g:JavaImpDocViewer = "w3m"
On windows, you can put iexplore.exe or mozilla.exe in your path and set
the g:JavaImpDocViewer to "iexplore.exe" or "mozilla.exe". Note that
windows' shell and spaces in the path don't mix. Thus, given an absolute
path of the HTML viewer to g:JavaImpDocViewer may not work.
If you have your g:JavaImpDocPaths and g:JavaImpDocViewer set correctly
You can then do this with your cursor on a classname:
:JavaImpDoc or :JID
JavaImp will find your class accordingly and open the viewer to the
class based on the import list that you've generated by :JIG. You can
also set in your java.vim filetype plugin to get a similar man page
behavior by press "K":
nmap <buffer> K :JID<CR>
You can make the following settings in your .vimrc file:
(Deprecated in 2.1) The g:JavaImpClassList is a file you specify to store
the class mappings. The default is set to:
let g:JavaImpClassList = g:JavaImpDataDir . "/JavaImp.txt"
(Deprecated in 2.1, enabled by default and set relative to the
g:JavaImpDataDir) It's recommended that you set a directory for the
caching result for your jar files. JavaImp saves the result from each
"jar" command in this directory. The default is set to:
let g:JavaImpJarCache = g:JavaImpDataDir . "/cache"
(In in 2.1, g:JavaImpSortBin is set to "sort", so this is disabled by
default) The sorting algorithm gives preferences to the java.* classes.
You can turn this behavior off by putting this in your .vimrc file.
let g:JavaImpSortJavaFirst = 0
NOTE: If you do not have a sort binary, set the following to "", and
JavaImp will use the pure Vim implementation. However, if
JavaImpClassList gets too huge, the sorting might choke. If you
are on a unix machine or you have the sort binary in your path, it's
recommended that you set (or leave it as default):
let g:JavaImpSortBin = "sort"
By default, the sort algorithm will insert a blank line among package
group with package root for 2 similar levels. For example, the import of
Note the classes that begins similar package root with two beginning
levels of package hierarchy are stuck together. You can set the
g:JavaImpSortPkgSep to change this behavior. The default
g:JavaImpSortPkgSep is set to 2. Do not set it too high though for you'll
insert a blank line after each import. If you do not want to insert blank
lines among the imports, set:
let g:JavaImpSortPkgSep = 0
Be warned that g:JavaImpSortRemoveEmpty has no effect if
g:JavaImpSortPkgSep is set.
After you have generated the JavaImp.txt file by using :JIG, you can use
it as your dictionary for autocompletion. For example, you can put the
following in your java.vim ftplugin (note here g:JavaImpDataDir is set
before running this):
exe "setlocal dict=" . g:JavaImpDataDir . "/JavaImp.txt"
or put this in your .vimrc
exe "set dict=" . g:JavaImpDataDir . "/JavaImp.txt"
After you have done so, you can open a .java file and use ^P and ^N to
autocomplete your Java class names.
Importing your JDK Classes
JavaImp also support a file type called "jmplst". A jmplst file
essentially contains the output of a "jar tf" command. It is mainly use
in the case where you want to import some external classes in JavaImp but
you do not have the source directory nor the jar file. For example, you
can import the JDK classes, which can be located in $JAVA_HOME/src.jar
(different in different distributions) with your JDK distribution,
by using the jmplst file:
To expose the standard JDK classes to JavaImp:
1. If you have "sed":
> jar tf $JAVA_HOME/src.jar | sed -e 's#^src/##' > jdk.jmplst
If you do not have "sed":
> jar tf $JAVA_HOME/src.jar > jdk.jmplst
> vim jdk.jmplst
[Execute the following vim commands:]
[ This will select vertically the all the "src/" prefixes and delete
them, then save the file. Essentially, we want to get rid of the src
directory otherwise it'll screw up the import statements. ]
2. Put the jdk.jmplst in a directory that you've added in your
g:JavaImpPaths. For example, I put my jdk.jmplst in
$HOME/vim/JavaImp/jmplst directory, and add $HOME/vim/JavaImp/jmplst to
3. Open vim with the JavaImp.vim loaded and Do a :JIG. You should see
that JavaImp will pick up many more classes.
4. Try to do a :JI on a "Vector" class, for example, to see whether you
can add the import statement from the JDK.
Click on the package to download.
ip used for rating: 184.108.40.206
||Fixes a bug where JavaImp ignores files that start with 0.
||Do not insert the import if the class you want to insert is in the same package of the current source file. (Thanks to Adam Hawthorne)
||Minor bug fixes.
||Added the JavaDoc viewing and source viewing capabilities. You can now view API calls through an HTML viewer or load up a source file from your paths.
||Added an option to override the default JavaImpPaths separator. Fixes an error when trying to do :JI over an unamed buffer.
||Fixes a critical bug in where a single import match would put in a "0" as its name. Users of 2.1.1 should update to this immediately. Thanks Matt Paduano for pointing this out.