在 Vim 中更高效地编译 Java 代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6411979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Compiling Java code in Vim more efficiently
提问by Pieter
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:
我来自 Eclipse 背景,但我喜欢 Vim 作为文本编辑器。我目前正在试验 Vim 作为 Java IDE。目前我这样做是为了编译:
! 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.
必须有比这更有效的方法。在我目前效率低下的 Vim 工作流程下,我可以在图形 IDE 中更快地完成工作,这超出了我使用 Vim 的目的。
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?
我希望能够:compile
在包含我的 main 方法的类中输入类似的内容来编译我的所有源代码并显示错误消息的分屏列表。你会推荐什么?
Related, but not relevant to me personally:
相关,但与我个人无关:
Update: My takeaway from this question is posted as a separate answer.
回答by sehe
With Makefiles, you could use some very generic things:
使用 Makefile,你可以使用一些非常通用的东西:
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.
将一些键映射到:cnext
并:cprevious
快速导航错误。
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.
使用:colder
/:cnewer
返回到较早/较晚的快速修复列表。Quickfix 将记住您在 quickfix 堆栈中的位置,以获取特定的 quickfix 列表。
回答by Pieter
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
:
将此添加到~/.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.
F9 编译,F10/F11 循环错误。
回答by rekinyz
if you don't use any package in your java class, then
如果你的 java 类中没有使用任何包,那么
//compile
:!javac %
//run
:!java -cp %:p:h %:t:r
map F5 in the .vimrcfile to automate the build
在.vimrc文件中映射 F5以自动构建
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
回答by A.B.
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
如果您运行 linux(使用 bash),这是我的设置:要使用主文件编译和运行该类,只需将其包含在您的 vimrc 文件中
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 和 c (\ac) 时,代码将更改为在 vim 中打开的当前文件的目录(可能是您的主文件)。然后,代码编译 yourfile.java (%:t)。最后,代码运行您的主文件 yourfile (%:t:r)。<CR>代表回车,相当于回车。这种方法也适用于开发具有多个类的项目,因为您可以在上面的行中添加更多代码,使其在运行 main 之前编译其他类。