vim编译c++文件设置
为了从codd::blocks平台转移到vim,最近几天花了好久研究vim,虽然也没有学会什么奇技淫巧,但是终于可以编译c++文件了。
vim和ide比起来,就是秒开,实在受不了vs的庞大,cb的丑陋。
安装并配置gcc环境
gcc可以用CodeBlocks的目录下的MinGW,也可以从网上下一个重新安装(gcc下载地址)
接下来是依次打开计算机->系统属性->高级系统设置->高级->环境变量->用户变量->变量PATH(如果没有自己新建一个)->编辑写下MinGW的bin文件夹地址,我的是这样C:\MinGW\bin;最后用分号隔开
之后按win(就是Ctrl和alt之间的按键)+r ,输入cmd,再输入g++ -v命令。
如果出现下面的图就可以了
配置vim
打开vim的配置文件_vimrc,一般在vim目录下。
输入将下面代码复制进去,我的设置是按F6编译。
这个文件中用 ” 可以注释
" 编译器选择
map <F6> :call CR()<CR>
func! CR()
exec "w"
exec "!g++ -O2 -g % -o %<"
exec "! %<"
"exec "!g++ % -o %<"
"exec "! ./%<
endfun
3.写程序
一般可以在桌面上新建一个txt文件,再将它改名程a.cpp之类的直接拖进vim里面来写,还可以配置_vimrc文件来一键生成头文件之类的,具体的也是在那个文件中加入下面的代码,按F2可以生成。
模版来自kuangbin大神
"c++的头文件
map <F2> :call SetTitle()<CR>
func SetTitle()
let l = 0
let l = l + 1 | call setline(l,'/* ***********************************************')
let l = l + 1 | call setline(l,'Author :xryz')
let l = l + 1 | call setline(l,'Email :523689985@qq.com')
let l = l + 1 | call setline(l,'Created Time :'.strftime('%c'))
let l = l + 1 | call setline(l,'File Name :'.expand('%'))
let l = l + 1 | call setline(l,'************************************************ */')
let l = l + 1 | call setline(l,'')
let l = l + 1 | call setline(l,'#include <stdio.h>')
let l = l + 1 | call setline(l,'#include <string.h>')
let l = l + 1 | call setline(l,'#include <iostream>')
let l = l + 1 | call setline(l,'#include <algorithm>')
let l = l + 1 | call setline(l,'#include <vector>')
let l = l + 1 | call setline(l,'#include <queue>')
let l = l + 1 | call setline(l,'#include <set>')
let l = l + 1 | call setline(l,'#include <map>')
let l = l + 1 | call setline(l,'#include <string>')
let l = l + 1 | call setline(l,'#include <math.h>')
let l = l + 1 | call setline(l,'#include <stdlib.h>')
let l = l + 1 | call setline(l,'#include <time.h>')
let l = l + 1 | call setline(l,'using namespace std;')
let l = l + 1 | call setline(l,'')
let l = l + 1 | call setline(l,'int main()')
let l = l + 1 | call setline(l,'{')
let l = l + 1 | call setline(l,' //freopen("in.txt","r",stdin);')
let l = l + 1 | call setline(l,' //freopen("out.txt","w",stdout);')
let l = l + 1 | call setline(l,' ')
let l = l + 1 | call setline(l,' return 0;')
let l = l + 1 | call setline(l,'}')
endfunc
再按f6就可以编译了,之后自己看着办吧
最后我的vim配置文件
" 去掉讨厌的有关vi一致性模式,避免以前版本的一些bug和局限
set nocompatible
source $VIMRUNTIME/vimrc_example.vim
source $VIMRUNTIME/mswin.vim
behave mswin
set diffexpr=MyDiff()
function MyDiff()
let opt = '-a --binary '
if &diffopt =~ 'icase' | let opt = opt . '-i ' | endif
if &diffopt =~ 'iwhite' | let opt = opt . '-b ' | endif
let arg1 = v:fname_in
if arg1 =~ ' ' | let arg1 = '"' . arg1 . '"' | endif
let arg2 = v:fname_new
if arg2 =~ ' ' | let arg2 = '"' . arg2 . '"' | endif
let arg3 = v:fname_out
if arg3 =~ ' ' | let arg3 = '"' . arg3 . '"' | endif
let eq = ''
if $VIMRUNTIME =~ ' '
if &sh =~ '\<cmd'
let cmd = '""' . $VIMRUNTIME . '\diff"'
let eq = '"'
else
let cmd = substitute($VIMRUNTIME, ' ', '" ', '') . '\diff"'
endif
else
let cmd = $VIMRUNTIME . '\diff'
endif
silent execute '!' . cmd . ' ' . opt . arg1 . ' ' . arg2 . ' > ' . arg3 . eq
endfunction
" 显示行号
set nu
" 历史记录数
set history=1000000
" Tab键的宽度
set tabstop=4
" 统一缩进为4
set shiftwidth=4
" 在行和段开始处使用制表符
set smarttab
" 自动缩进
set autoindent
set cindent
" 配色主题
colo evening
set guifont=monaco:h10
" 输入的命令显示出来,看的清楚些
set showcmd
" 从不备份,禁止生成临时文件
set nobackup
set noswapfile
" 可以在buffer的任何地方使用鼠标(类似office中在工作区双击鼠标定位)
set mouse=a
" 编译器选择
map <F6> :call CR()<CR>
func! CR()
exec "w"
exec "!g++ -O2 -g % -o %<"
exec "! %<"
"exec "!g++ % -o %<"
"exec "! ./%<
endfunc
imap <c-]> {<cr>}<c-o>O<left><right>
map <C-A> ggVG"+y
"inoremap ( ()<LEFT>
"inoremap [ []<LEFT>
"inoremap { {}<LEFT>
"inoremap " ""<LEFT>
"inoremap ' ''<LEFT>
"c++的头文件
map <F2> :call SetTitle()<CR>
func SetTitle()
let l = 0
let l = l + 1 | call setline(l,'/* ***********************************************')
let l = l + 1 | call setline(l,'Author :xryz')
let l = l + 1 | call setline(l,'Email :523689985@qq.com')
let l = l + 1 | call setline(l,'Created Time :'.strftime('%c'))
let l = l + 1 | call setline(l,'File Name :'.expand('%'))
let l = l + 1 | call setline(l,'************************************************ */')
let l = l + 1 | call setline(l,'')
let l = l + 1 | call setline(l,'#include <stdio.h>')
let l = l + 1 | call setline(l,'#include <string.h>')
let l = l + 1 | call setline(l,'#include <iostream>')
let l = l + 1 | call setline(l,'#include <algorithm>')
let l = l + 1 | call setline(l,'#include <vector>')
let l = l + 1 | call setline(l,'#include <queue>')
let l = l + 1 | call setline(l,'#include <set>')
let l = l + 1 | call setline(l,'#include <map>')
let l = l + 1 | call setline(l,'#include <string>')
let l = l + 1 | call setline(l,'#include <math.h>')
let l = l + 1 | call setline(l,'#include <stdlib.h>')
let l = l + 1 | call setline(l,'#include <time.h>')
let l = l + 1 | call setline(l,'using namespace std;')
let l = l + 1 | call setline(l,'')
let l = l + 1 | call setline(l,'int main()')
let l = l + 1 | call setline(l,'{')
let l = l + 1 | call setline(l,' //freopen("in.txt","r",stdin);')
let l = l + 1 | call setline(l,' //freopen("out.txt","w",stdout);')
let l = l + 1 | call setline(l,' ')
let l = l + 1 | call setline(l,' return 0;')
let l = l + 1 | call setline(l,'}')
endfunc
" txt高亮设置
"syntax on
"filetype plugin on
"au BufEnter *.txt setlocal ft=txt
set shortmess=atI " 启动的时候不显示那个援助乌干达儿童的提示
"winpos 5 5 " 设定窗口位置
"set lines=40 columns=155 " 设定窗口大小
"make 运行
:set makeprg=g++\ -Wall\ \ %
版权声明:本文为博主原创文章,未经博主允许不得转载。http://xiang578.top/