vim编译c++文件设置

为了从codd::blocks平台转移到vim,最近几天花了好久研究vim,虽然也没有学会什么奇技淫巧,但是终于可以编译c++文件了。
vim和ide比起来,就是秒开,实在受不了vs的庞大,cb的丑陋。

  1. 安装并配置gcc环境
    gcc可以用CodeBlocks的目录下的MinGW,也可以从网上下一个重新安装(gcc下载地址
    接下来是依次打开计算机->系统属性->高级系统设置->高级->环境变量->用户变量->变量PATH(如果没有自己新建一个)->编辑写下MinGW的bin文件夹地址,我的是这样C:\MinGW\bin;最后用分号隔开
    之后按win(就是Ctrl和alt之间的按键)+r ,输入cmd,再输入g++ -v命令。
    如果出现下面的图就可以了
    成功安装

  2. 配置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配置文件

"####默认设置
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
  if $VIMRUNTIME =~ ' '
    if &sh =~ '\<cmd'
      if empty(&shellxquote)
        let l:shxq_sav = ''
        set shellxquote&
      endif
      let cmd = '"' . $VIMRUNTIME . '\diff"'
    else
      let cmd = substitute($VIMRUNTIME, ' ', '" ', '') . '\diff"'
    endif
  else
    let cmd = $VIMRUNTIME . '\diff'
  endif
  silent execute '!' . cmd . ' ' . opt . arg1 . ' ' . arg2 . ' > ' . arg3
  if exists('l:shxq_sav')
    let &shellxquote=l:shxq_sav
  endif
endfunction

"####基本设置

" 显示行号
set nu

" Tab键的宽度
set tabstop=4

" 自动缩进
set autoindent

" 语法高亮度显示
syntax on 

" 配色主题
set background=dark
colorscheme solarized

" 字体和大小
set guifont=Hack:h13

" 显示输入的命令
set showcmd

"设置匹配模式,类似当输入一个左括号时会匹配相应的那个右括号
s
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值