25

I come from an Eclipse background, but I love Vim as a text editor. I'm currently experimenting with Vim as a Java IDE. Currently I do this to compile:

! javac MyClass.java

followed by

! java -cp . MyClass

If I have compile errors, I have to go back to the compiler output using ! and manually jump to every line that produced an error. And as soon as I start adding other classes, I have to compile each of them separately.

There's got to be a more efficient way than this. Under my current inefficient Vim workflow, I can get stuff done faster in a graphical IDE, which beats the purpose of using Vim for me.

I'd like to be able to enter something like :compile in the class containing my main method to compile all my sources and be presented with a split-screen list of error messages. What would you recommend?


Related, but not relevant to me personally:


Update: My takeaway from this question is posted as a separate answer.

Community
  • 1
  • 1
Pieter
  • 31,619
  • 76
  • 167
  • 242
  • Are you aware there are Vim plugins for Eclipse and even Eclipse "plugins" for vim: http://vrapper.sourceforge.net/home/ - http://eclim.org/ - http://www.google.de/search?q=eclpise+vim – RoToRa Jun 20 '11 at 13:38
  • IMHO, If you want to edit text use Vim, however if you want to develop code, use a tool which was designed to develop code, an IDE. Can you use eclipse and vim? – Peter Lawrey Jun 20 '11 at 13:39
  • Make files, or (java!) Nant files come to mind. – sehe Jun 20 '11 at 13:45
  • @RoToRa: I had negative experiences with Eclim. I might look into Eclipse plugins that provide Vim functionality inside the IDE though. @Peter: I read something about a person who optimized their Vim workflow so well that they were equally or more productive than their coworkers who use graphical IDEs. – Pieter Jun 20 '11 at 13:48
  • @sehe: can the creation of these files be automated by Vim? Eclipse can build projects without any prior configuration (expect maybe for some default settings). – Pieter Jun 20 '11 at 13:50
  • Instead of :copen you should consider using :cwindow. :cwindow only opens the "errorview" if any compile errors occoured. – Mathias Bak Dec 25 '12 at 18:41
  • Hi @Pieter; can you move your edit explaining how you used the answers into an answer by itself? It looks like useful information, but it should not be included in the question; it is perfectly legitimate for you to post it as an answer. Thanks. – Keith Pinson Feb 05 '13 at 20:38
  • I edited the post and created the answer. – Pieter Feb 06 '13 at 10:51

5 Answers5

17

By request I've posted my takeaway from this question as a separate answer.


Here's how I used everyone's advice.

Walking through compile errors quickly

Add this to ~/.vimrc:

autocmd Filetype java set makeprg=javac\ %
set errorformat=%A%f:%l:\ %m,%-Z%p^,%-C%.%#
map <F9> :make<Return>:copen<Return>
map <F10> :cprevious<Return>
map <F11> :cnext<Return>

F9 to compile, F10/F11 to cycle through errors.

Pieter
  • 31,619
  • 76
  • 167
  • 242
13

if you don't use any package in your java class, then

//compile
:!javac %

//run
:!java -cp %:p:h %:t:r

map F5 in the .vimrc file to automate the build

map <F5> :call CompileRunGcc()<CR>
func! CompileRunGcc()
exec "w"
if &filetype == 'c'
exec "!gcc % -o %<"
exec "!time ./%<"
elseif &filetype == 'cpp'
exec "!g++ % -o %<"
exec "!time ./%<"
elseif &filetype == 'java'
exec "!javac %"
exec "!time java -cp %:p:h %:t:r"
elseif &filetype == 'sh'
exec "!time bash %"
elseif &filetype == 'python'
exec "!time python2.7 %"
elseif &filetype == 'html'
exec "!firefox % &"
elseif &filetype == 'go'
exec "!go build %<"
exec "!time go run %"
elseif &filetype == 'mkd'
exec "!~/.vim/markdown.pl % > %.html &"
exec "!firefox %.html &"
endif
endfunc
rekinyz
  • 6,576
  • 2
  • 29
  • 32
7

Here the vim wiki article for compiling with javac.

jamapag
  • 9,290
  • 4
  • 34
  • 28
  • "Add the following to your vimrc file to map F9 to compile, and F10 to run. *(big chunk of code)*" Now that's what I call user-friendly. But alright, if it helps me boost my efficiency in the long run I'd be happy to try it. – Pieter Jun 20 '11 at 13:45
  • 1
    @Pieter you can just add: `autocmd Filetype java set makeprg=javac\ %` and `set errorformat=%A%f:%l:\ %m,%-Z%p^,%-C%.%#` to your vimrc file and then in your main class call: `:make` – jamapag Jun 20 '11 at 14:07
  • This works for me as long as all my code is in one file. If I make a file *Main.java* that depends on `MyClass` in *MyClass.java*, I get *cannot find symbol* errors. I see that your vim rule seems to call `javac`, but it doesn't appear to generate the **.class* files needed so that *Main.java* can use *MyClass*. – Pieter Jun 22 '11 at 18:52
3

With Makefiles, you could use some very generic things:

JAVAFILES=$(wildcard *.java)

mytarget: $(JAVAFILES)
    javac $^

On the other hand, you would probably fine doing

:compiler javac
:se makeprg=javac\ **/*.java
:make
:copen

Map some keys to :cnext and :cprevious to navigate errors quickly.

Use :colder / :cnewerto go back to earlier/later quickfix lists. Quickfix will remember where in the quickfix stack you were for a specific quickfix list.

sehe
  • 374,641
  • 47
  • 450
  • 633
  • 1
    I'd prefer using make as well. If I remember right, then the book "Managing projects with GNU make" by Robert Mecklenburg contains a small section on how to write a generic Makefile for all kinds of java projects. – evnu Jun 20 '11 at 14:28
1

If you run linux(use bash), here is my setup: to compile and run the class with your main file just include this in your vimrc file

nnoremap <leader>ac :cd %:p:h <CR> :! javac %:t<CR> :! java %:t:r<CR>

Let me run you through how this code works. When you press the leader key followed by a and c (\ac), the code changes to the directory of the current file that is open in vim(presumably your main). Then, the code compiles yourfile.java (%:t). Finally, the code runs your main file yourfile (%:t:r). < CR > stands for carriage return, which is the equivalent of enter. This approach would work for developing projects with multiple classes as well, because you could add more code to the above line to make it compile other classes before running main.

A.B.
  • 51
  • 1
  • 5