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命令。
    如果出现下面的图就可以了
    vim编译c++文件设置_第1张图片

  2. 配置vim
    打开vim的配置文件_vimrc,一般在vim目录下。
    输入将下面代码复制进去,我的设置是按F6编译。
    这个文件中用 ” 可以注释

" 编译器选择
map  :call 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  :call SetTitle()
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         :[email protected]')
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 ')
let l = l + 1 | call setline(l,'#include ')
let l = l + 1 | call setline(l,'#include ')
let l = l + 1 | call setline(l,'#include ')
let l = l + 1 | call setline(l,'#include ')
let l = l + 1 | call setline(l,'#include ')
let l = l + 1 | call setline(l,'#include ')
let l = l + 1 | call setline(l,'#include ')
let l = l + 1 | call setline(l,'#include ')
let l = l + 1 | call setline(l,'#include ')
let l = l + 1 | call setline(l,'#include ')
let l = l + 1 | call setline(l,'#include ')
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 =~ '\\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

"设置匹配模式,类似当输入一个左括号时会匹配相应的那个右括号
set showmatch

" 可以在buffer的任何地方使用鼠标
set mouse=a

" 启动的时候不显示那个援助乌干达儿童的提示
set shortmess=atI   

" 与windows共享剪贴板  
set clipboard+=unnamed

" 高亮光标所在行
set cursorline

" 从不备份,禁止生成临时文件
set nobackup
set noswapfile

" 自动补全相关
imap  {}O
inoremap ( ()
inoremap [ []
inoremap { {}
inoremap " ""
inoremap ' ''


" 设置默认编码 
set encoding=utf-8 
set fileencodings=utf-8
set termencoding=utf-8

" 解决菜单乱码(不知道有什么用)
source $VIMRUNTIME/delmenu.vim 
source $VIMRUNTIME/menu.vim 

" 解决console输出乱码 (删除之后菜单栏会乱码)
language messages zh_CN.utf-8

" 隐藏菜单和具栏
set guioptions-=m
set guioptions-=T
map   :if &guioptions =~# 'T' 
        \set guioptions-=T 
        \set guioptions-=m 
    \else 
        \set guioptions+=T 
        \set guioptions+=m 
    \endif

"####C/C++ 相关设置
" C/C++ 的编译设置
map  :call CR()
func! CR()
exec "w"
exec "!g++ -O2 -g  % -o %<"
exec "! %<"
"exec "!g++ % -o %<"
"exec "! ./%<
endfunc

" C/C++ 的调试
map  :call Rungdb()
func! Rungdb()
    exec "w"
    exec "!g++ % -g -o %<"
    exec "!gdb ./%<"
endfunc

" C/C++ 的头文件
map  :call SetTitle()
func SetTitle()
let l = 0
let l = l + 1 | call setline(l,'/* ***********************************************')
let l = l + 1 | call setline(l,'Author        :Maktub')
let l = l + 1 | call setline(l,'Email         :[email protected]')
let l = l + 1 | call setline(l,'Blog          :htttp://www.xiang578.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 ')
let l = l + 1 | call setline(l,'//#include ')
let l = l + 1 | call setline(l,'//#include ')
let l = l + 1 | call setline(l,'//#include ')
let l = l + 1 | call setline(l,'//#include ')
let l = l + 1 | call setline(l,'//#include ')
let l = l + 1 | call setline(l,'//#include ')
let l = l + 1 | call setline(l,'//#include ')
let l = l + 1 | call setline(l,'//#include ')
let l = l + 1 | call setline(l,'//#include ')
let l = l + 1 | call setline(l,'//#include ')
let l = l + 1 | call setline(l,'//#include ')
let l = l + 1 | call setline(l,'#include ')
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


"####Vundle 插件管理

filetype off
"Vundle路径
set rtp+=$VIM/vimfiles/bundle/Vundle.vim
"插件的安装路径
call vundle#begin('$VIM/vimfiles/bundle/')

" 插件管理
Plugin 'gmarik/Vundle.vim'
Plugin 'L9'

" 状态栏
Plugin 'bling/vim-airline'
" 
Plugin 'plasticboy/vim-markdown'

" 配色
Plugin 'altercation/vim-colors-solarized'

" 自动对齐
Plugin 'godlygeek/tabular'

" Fugitive: Git 集成,强烈推荐!
" Plugin 'tpope/vim-fugitive'

call vundle#end()
filetype plugin indent on


" airline设置
set laststatus=2
"使用powerline打过补丁的字体
let g:airline_powerline_fonts = 1
set laststatus=2
set t_Co=256   
let g:Powerline_symbols = 'fancy'
"set guifont=Consolas\ for\ Powerline\ FixedD:h15
" 开启tabline
let g:airline#extensions#tabline#enabled = 1
" tabline中当前buffer两端的分隔字符
"let g:airline#extensions#tabline#left_sep = ' '
" tabline中未激活buffer两端的分隔字符
"let g:airline#extensions#tabline#left_alt_sep = '<'
" tabline中buffer显示编号
let g:airline#extensions#tabline#buffer_nr_show = 0
" 映射切换buffer的键位
nnoremap [b :bp
nnoremap ]b :bn

"Vim-Markdown
"1.disable folding
let g:vim_markdown_folding_disabled=1
"2.latex
let g:vim_markdown_math=1
"Disable Default Key Mappings
let g:vim_markdown_no_default_key_mappings=1
" 去掉讨厌的有关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 =~ '\
      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  :call CR()
func! CR()
exec "w"
exec "!g++ -O2 -g  % -o %<"
exec "! %<"
"exec "!g++ % -o %<"
"exec "! ./%<
endfunc

imap  {}O<left><right>

map  ggVG"+y


"inoremap ( ()<LEFT>
"inoremap [ []
"inoremap { {}<LEFT>
"inoremap " ""<LEFT>
"inoremap ' ''

"c++的头文件
map  :call SetTitle()
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         :[email protected]')
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 ')
let l = l + 1 | call setline(l,'#include ')
let l = l + 1 | call setline(l,'#include ')
let l = l + 1 | call setline(l,'#include ')
let l = l + 1 | call setline(l,'#include ')
let l = l + 1 | call setline(l,'#include ')
let l = l + 1 | call setline(l,'#include ')
let l = l + 1 | call setline(l,'#include ')
let l = l + 1 | call setline(l,'#include ')
let l = l + 1 | call setline(l,'#include ')
let l = l + 1 | call setline(l,'#include ')
let l = l + 1 | call setline(l,'#include ')
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\ \ %

新版(未排版)

" 去掉讨厌的有关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 =~ '\$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 lucius
set background=light
colorscheme solarized

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

" 显示输入的命令
set showcmd

" 从不备份,禁止生成临时文件
set nobackup
set noswapfile
" 可以在buffer的任何地方使用鼠标(类似office中在工作区双击鼠标定位)
set mouse=a

" C++,C 的编译设置
map  :call CR()
func! CR()
exec "w"
exec "!g++ -O2 -g  % -o %<"
exec "! %<"
"exec "!g++ % -o %<"
"exec "! ./%<
endfunc

"C,C++的调试
map  :call Rungdb()
func! Rungdb()
    exec "w"
    exec "!g++ % -g -o %<"
    exec "!gdb ./%<"
endfunc

imap  {}O

inoremap ( ()
inoremap [ []
inoremap { {}
inoremap " ""
inoremap ' ''

"c++的头文件
map  :call SetTitle()
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         :[email protected]')
let l = l + 1 | call setline(l,'Blog          :htttp://www.xiang578.top')
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 ')
let l = l + 1 | call setline(l,'//#include ')
let l = l + 1 | call setline(l,'//#include ')
let l = l + 1 | call setline(l,'//#include ')
let l = l + 1 | call setline(l,'//#include ')
let l = l + 1 | call setline(l,'//#include ')
let l = l + 1 | call setline(l,'//#include ')
let l = l + 1 | call setline(l,'//#include ')
let l = l + 1 | call setline(l,'//#include ')
let l = l + 1 | call setline(l,'//#include ')
let l = l + 1 | call setline(l,'//#include ')
let l = l + 1 | call setline(l,'//#include ')
let l = l + 1 | call setline(l,'#include ')
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

" 启动的时候不显示那个援助乌干达儿童的提示
set shortmess=atI   

"make 运行
:set makeprg=g++\ -Wall\ \ %

" 与windows共享剪贴板  
set clipboard+=unnamed

" 命令行(在状态行下)的高度,默认为1,这里是2  
set cmdheight=2 

" 为C程序提供自动缩进  
set smartindent  

" 使用C样式的缩进  
"set cinden

" 语法高亮度显示
syntax on 

"设置匹配模式,类似当输入一个左括号时会匹配相应的那个右括号
set showmatch

"隐藏菜单和具栏
set guioptions-=m
set guioptions-=T
map   :if &guioptions =~# 'T' 
        \set guioptions-=T 
        \set guioptions-=m 
    \else 
        \set guioptions+=T 
        \set guioptions+=m 
    \endif

filetype off

"Vundle的路径
set rtp+=$VIM/vimfiles/bundle/Vundle.vim
"插件的安装路径
call vundle#begin('$VIM/vimfiles/bundle/')

Plugin 'gmarik/Vundle.vim'
Plugin 'L9'
Plugin 'bling/vim-airline'
Plugin 'godlygeek/tabular'
Plugin 'plasticboy/vim-markdown'
Plugin 'altercation/vim-colors-solarized'
"Plugin 'https://github.com/powerline/fonts.git'
call vundle#end()
filetype plugin indent on

" airline设置
set laststatus=2
"使用powerline打过补丁的字体
let g:airline_powerline_fonts = 1
set laststatus=2
set t_Co=256   
let g:Powerline_symbols = 'fancy'
"set guifont=Consolas\ for\ Powerline\ FixedD:h15
" 开启tabline
let g:airline#extensions#tabline#enabled = 1
" tabline中当前buffer两端的分隔字符
"let g:airline#extensions#tabline#left_sep = ' '
" tabline中未激活buffer两端的分隔字符
"let g:airline#extensions#tabline#left_alt_sep = '<'
" tabline中buffer显示编号
let g:airline#extensions#tabline#buffer_nr_show = 0
" 映射切换buffer的键位
nnoremap [b :bp
nnoremap ]b :bn

"Vim-Markdown
"1.disable folding
let g:vim_markdown_folding_disabled=1
"2.latex
let g:vim_markdown_math=1
"Disable Default Key Mappings
let g:vim_markdown_no_default_key_mappings=1

"设置默认编码 
set encoding=utf-8 
set fileencodings=utf-8,chinese,latin-1 
if has("win32") 
set fileencoding=chinese 
else 
set fileencoding=utf-8 
endif 
"解决菜单乱码 
source $VIMRUNTIME/delmenu.vim 
source $VIMRUNTIME/menu.vim 
"解决console输出乱码 
language messages zh_CN.utf-8

你可能感兴趣的:(学习总结)