文章转载至http://blog.chinaunix.net/uid-21202106-id-2406761.html
事先声明,我使用的VIM完全是基于终端的,而不是gvim或vim-x11。因为后两者不具有平台移植性,花哨的目录和鼠标点击并非必须。
|
|
|
func! CompileGcc()
exec "w"
let compilecmd="!gcc "
let compileflag="-o %< "
if search("mpi\.h")
!= 0
let compilecmd =
"!mpicc "
endif
if search("glut\.h")
!= 0
let compileflag .=
" -lglut -lGLU -lGL "
endif
if search("cv\.h")
!= 0
let compileflag .=
" -lcv -lhighgui -lcvaux "
endif
if search("omp\.h")
!= 0
let compileflag .=
" -fopenmp "
endif
if search("math\.h")
!= 0
let compileflag .=
" -lm "
endif
exec compilecmd." % ".compileflag
endfunc
func! CompileGpp()
exec "w"
let compilecmd="!g++ "
let compileflag="-o %< "
if search("mpi\.h")
!= 0
let compilecmd =
"!mpic++ "
endif
if search("glut\.h")
!= 0
let compileflag .=
" -lglut -lGLU -lGL "
endif
if search("cv\.h")
!= 0
let compileflag .=
" -lcv -lhighgui -lcvaux "
endif
if search("omp\.h")
!= 0
let compileflag .=
" -fopenmp "
endif
if search("math\.h")
!= 0
let compileflag .=
" -lm "
endif
exec compilecmd." % ".compileflag
endfunc
func! RunPython()
exec "!python %"
endfunc
func! CompileJava()
exec "!javac %"
endfunc
func! CompileCode()
exec "w"
if &filetype
== "cpp"
exec "call CompileGpp()"
elseif &filetype
== "c"
exec "call CompileGcc()"
elseif &filetype
== "python"
exec "call RunPython()"
elseif &filetype
== "java"
exec "call CompileJava()"
endif
endfunc
func! RunResult()
exec "w"
if search("mpi\.h")
!= 0
exec "!mpirun -np 4 ./%<"
elseif &filetype
== "cpp"
exec "! ./%<"
elseif &filetype
== "c"
exec "! ./%<"
elseif &filetype
== "python"
exec "call RunPython"
elseif &filetype
== "java"
exec "!java %<"
endif
endfunc
map <F5>
:call CompileCode()<CR>
imap <F5>
<ESC>:call CompileCode()<CR>
vmap <F5>
<ESC>:call CompileCode()<CR>
map <F6>
:call RunResult()<CR>