打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
vim:一键编译单个源文件
 具体功能如下:
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 
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
gVim/Vim 一键编译、连接、运行 C/C++ 单文件(Windows 与 Linux通用)
我的VIMRC
Vim下一键运行python代码
“C语言” 读书札记(三)之[编译执行]
用Vim进行C/C++编程介绍--心随风动
在 Vim 中编写 Python 程序
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服