具体功能如下: 1.按F5编译单个文件,支持C,C++,C#,也可以支持java。 2.获取编译器错误描述,在错误描述上回车,可以直接跳转到错误行。 总之一句话,懒人的第一选择。 准备工作: 1.安装Vi/Vim/gVim,Windows、Linux环境皆可。 2.gcc、g++编译器。针对这一点,Linux用户一般比较清楚,用命令which gcc一查就知道是不是有gcc了。但是对于windows用户,首先要安装gcc编译器,有几个办法,(1)安装minGW,然后把minGW安装目录下的bin目录放到系统path里。(2)安装Dev-C++,然后把Dev-C++安装目录下的bin目录放到系统path里,(3)安装cygwin,并保证安装了gcc、g++(默认都没有安装,需要选择安装),然后把cygwin安装目录下的bin目录放到系统path里。以上三个方法都可以,可以任意选择,cygwin安装大概有点困难,不过也是学习的过程。 好了,代码列在下面,需要把下面的代码复制到vim的配置文件(Windows下:_vimrc,Linux下:.vimrc)。保存以后,用vim打开一个C/C++程序,然后F5进行编译、差错。 "单个文件编译 if(has("win32") || has("win95") || has("win64") || has("win16")) let g:iswindows=1 else let g:iswindows=0 endif "单个文件编译 map <F5> :call Do_OneFileMake()<CR> function Do_OneFileMake() if expand("%:p:h")!=getcwd() echohl WarningMsg | echo "Fail to make! This file is not in the current dir! Press <F7> to redirect to the dir of this file." | echohl None return endif let sourcefileename=expand("%:t") if (sourcefileename=="" || (&filetype!="cpp" && &filetype!="c")) echohl WarningMsg | echo "Fail to make! Please select the right file!" | echohl None return endif let deletedspacefilename=substitute(sourcefileename,' ','','g') if strlen(deletedspacefilename)!=strlen(sourcefileename) echohl WarningMsg | echo "Fail to make! Please delete the spaces in the filename!" | echohl None return endif if &filetype=="c" if g:iswindows==1 set makeprg=gcc\ -o\ %<.exe\ % else set makeprg=gcc\ -o\ %<\ % endif elseif &filetype=="cpp" if g:iswindows==1 set makeprg=g++\ -o\ %<.exe\ % else set makeprg=g++\ -o\ %<\ % endif "elseif &filetype=="cs" "set makeprg=csc\ \/nologo\ \/out:%<.exe\ % endif if(g:iswindows==1) let outfilename=substitute(sourcefileename,'\(\.[^.]*\)$','.exe','g') let toexename=outfilename else let outfilename=substitute(sourcefileename,'\(\.[^.]*\)$','','g') let toexename=outfilename endif if filereadable(outfilename) if(g:iswindows==1) let outdeletedsuccess=delete(getcwd()."\\".outfilename) else let outdeletedsuccess=delete("./".outfilename) endif if(outdeletedsuccess!=0) set makeprg=make echohl WarningMsg | echo "Fail to make! I cannot delete the ".outfilename | echohl None return endif endif execute "silent make" set makeprg=make execute "normal :" if filereadable(outfilename) if(g:iswindows==1) execute "!".toexename else execute "!./".toexename endif endif execute "copen" endfunction "进行make的设置 map <F6> :call Do_make()<CR> map <c-F6> :silent make clean<CR> function Do_make() set makeprg=make execute "silent make" execute "copen" endfunction 以上直接参考至Vimer,感谢Dantezhu |
|
来自: herowuking > 《VIM》