如何使vim作为IDE运行和编译Java文件?

如何使vim作为IDE运行和编译Java文件?,vim,Vim,我已经在Linux vimrc文件中使用了一个vim函数,只需按“F5”即可执行Java文件 下面是我使用的函数 inoremap <F5> <Esc> :call CompileRunGcc()<CR><CR> func! CompileRunGcc() exec "w" if &filetype == 'java' exec "!javac %" exec "!time java -cp %:p:h %:t:r" endif endfu

我已经在Linux vimrc文件中使用了一个vim函数,只需按“F5”即可执行Java文件

下面是我使用的函数

inoremap <F5> <Esc> :call CompileRunGcc()<CR><CR>
func! CompileRunGcc()
exec "w"
if &filetype == 'java'
exec "!javac %"
exec "!time java -cp %:p:h %:t:r"
endif
endfunc
inoremap:callcompilerungcc()
func!编译器ungcc()
执行官“w”
if&filetype=='java'
exec“!javac%”
exec“!time java-cp%:p:h%:t:r”
恩迪夫
endfunc
但当出现编译错误时,它会显示错误消息以及以前编译的程序的输出

我想要的是只显示错误消息,而不显示先前编译程序的输出。如何执行该操作?

您可以使用该变量检查
javac
命令是否失败。在这种情况下,您可以跳过运行程序,在这种情况下,错误消息将留给您检查

function! CompileRunGcc()
  exec "w"
  if &filetype ==# 'java'
    exec "!javac %"
    if !v:shell_error
      exec "!time java -cp %:p:h %:t:r"
    endif
  endif
endfunction

在我看来,您最好使用quickfix列表特性将发现的错误和警告导入到Vim中。从那里,您可以使用
:cnext
:cprev
导航错误

关于Java的最佳功能,您必须探索各种与Java相关的编译器插件。例如,有一个
:编译器javac

关于如何知道编译是否成功,我使用类似Q/a中描述的方法:

唯一改变的是
exec\u行
变量的构建方式。它看起来像(未经测试)

来自我的构建工具包装插件的
”
函数!lh#顺便说一句#构建#获取#度量()中止
设qf=getqflist()
让已识别=过滤器(qf,'get(v:val,“valid”,1)'
“TODO:支持其他区域设置,请参阅lh#po#context().tranlate()
let errors=filter(复制(已识别),'v:val.type==“E”|| v:val.text=~“\\v^*(error | erreur)”)
let warnings=filter(复制(已识别),'v:val.type==“W”|| v:val.text=~“\\v^*(警告|注意)”)
让res={'all':len(qf),'errors':len(errors),'warnings':len(warnings)}
返回res
端功能
“在java插件中
函数s:生成和运行(文件)中止
让tgt=fnamemodify(a:file,':r')
“以确保缓冲区已保存
exe“更新”。a:档案
exe“make”。tgt
如果lh#顺便说一句#构建#获取#度量()错误
echom“检测到错误,执行中止”
科本
返回
恩迪夫
让path=fnamemodify(a:file,':p:h')
让exec_line='!时间是java-cp。“路径”。tgt
exe exec_行
端功能
“对于,我假设这是在java ftplugin中定义的,因为
“具体的”!时间是“java”。它可以推广到许多不同的领域
“不过是语言。
nnoremapµ:调用生成和运行(展开(“%”)

我强烈建议(我是vim的粉丝,请注意)使用Java IDE,比如免费的Intellij社区版(当然还有其他免费的IDE),您的效率会更高。谢谢您的建议@BrianAgnew
" From my build-tools-wrapper plugin
function! lh#btw#build#_get_metrics() abort
  let qf = getqflist()
  let recognized = filter(qf, 'get(v:val, "valid", 1)')
  " TODO: support other locales, see lh#po#context().tranlate()
  let errors   = filter(copy(recognized), 'v:val.type == "E" || v:val.text =~ "\\v^ *(error|erreur)"')
  let warnings = filter(copy(recognized), 'v:val.type == "W" || v:val.text =~ "\\v^ *(warning|attention)"')
  let res = { 'all': len(qf), 'errors': len(errors), 'warnings': len(warnings) }
  return res
endfunction


" in a java ftplugin    
function s:build_and_run(file) abort
  let tgt  = fnamemodify(a:file, ':r')
  " to make sure the buffer is saved
  exe 'update ' . a:file
  exe 'make ' . tgt
  if lh#btw#build#_get_metrics().errors
    echom "Error detected, execution aborted"
    copen
    return
  endif

  let path = fnamemodify(a:file, ':p:h')
  let exec_line = '!time java -cp ' . path. ' ' . tgt
  exe exec_line
endfunction

" With <buffer>, I assume this is defined in a java-ftplugin, because the 
" specific '!time java'. It could be generalized to many different 
" languages though.
nnoremap <buffer> µ :<C-U>call <sid>build_and_run(expand('%'))<cr>