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.
By request I've posted my takeaway from this question as a separate answer.
Here's how I used everyone's advice.
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.
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
Here the vim wiki article for compiling with javac.
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
/ :cnewer
to go back to earlier/later quickfix lists. Quickfix will remember where in the quickfix stack you were for a specific quickfix list.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With