How to Set up VIM for Java

  • Print this article

The VIM text editor is at its best in programming, where its support for programmable keystrokes and scripted editing tasks can vastly improve your productivity. However, unlike a more traditional Integrated Development Environment (IDE), it does take some setting up to get it working for your language.

Instructions

    • 1

      Download "javacomplete" from the first resource. This free script analyzes your Java class path to find the classes and libraries you are using and to provide auto-completion features as your program.

    • 2

      Double-click the "javacomplete.zip" file you downloaded and extract it to the directory "~/.vim" in Linux/Mac OS X or "C:\Program Files\Vim\vimfiles" in Windows.

    • 3

      Open Vim and type ":e $MYVIMRC".

    • 4

      Add the following lines anywhere in the file:

      :if has("autocmd")

      : autocmd Filetype java setlocal omnifunc=javacomplete#Complete

      :endif

      :inoremap <buffer> <C-X><C-U> <C-X><C-U><C-P>

      These will enable Java Completion to be performed anytime you press "Ctrl-X" and "Ctrl-U" in order. Feel free to replace the "Ctrl-X" "Ctrl-U" keystroke with any other keystroke you like.

    • 5

      Add the following line while you still have your Vim configuration file open:

      :abb psvm public static void main(String[] args) {<CR><CR>}<UP>

      This will expand the word "psvm" into a standard main function for you anywhere you type it, a handy time saver for Java programmers.

    • 6

      Add one last line to your Vim configuration file:

      :map [F5] [ESC]:!ant . > BUILD_LOG<CR>:e BUILD_LOG

      This simple script will build your current Java project automatically whenever you press "F5" using the same "ant" utility used to build projects in Netbeans or Eclipse. It will output the results of the build process to a file name "BUILD_LOG" and open the "BUILD_LOG" in a new VIM buffer for your inspection.

Tips & Warnings

  • This is by no means all the productivity gains you can achieve from a proper VIM configuration, but code completion, building at a keystroke, and text expansion of common functions is a great start.

Related Searches

References

Comments

You May Also Like

Related Ads

Featured
View Mobile Site