在.vimrc文件添加如下函数:
点击(此处)折叠或打开
-
nmap <F4> :call DoOneFileMake()<CR>
-
function DoOneFileMake()
-
if(expand("%:p:h")!=getcwd())
-
echohl WarningMsg | echo "Fail to make! This file is not in the current dir! Press redirect to the dir of this file."
-
endif
-
-
exec "w"
-
call SetCompilation()
-
exec "make"
-
exec "copen"
-
endfunction
-
-
SetCompilation()
-
if &filetype=='c'
-
set makeprg=gcc\ %\ -o\ %<
-
elseif &filetype=='cpp'
-
set makeprg=g++ \ %\ -o\ %<根据不同
-
endif
- endfunction
第3-5行:判断这个文件是否在当前文件夹.
第7行:相当于执行命令w
第8行:调用函数SetCompilation(),用来设置编译器或者说设定编译命令.
第9行:执行make命令
第10行:打开quickfix窗口,用于显示编译产生的错误.
第13-19行:根据不同的文件类型,来配置makeprg,也就是make命令调用的编译器或编译命令.
第14行:判断当前的文件类型是否是C 程序.
第15行:设定make命令所调用的编译命令.
说明:这样来设定的好处就是编译产生的错误可以直接在\quickfix窗口中显示出来.
第16-17行,分析同14-15行.