" ---------------------------------------------------------------
" GLOBAL 环境配置
" 使用:helptags <帮助文件所在文件夹>导入对应的帮助文件
" ---------------------------------------------------------------
set mouse=a "使用鼠标
set ve=block "光标可以定位在没有实际字符的地方
set nowrap "不自动换行
set nocompatible "不要vim模仿vi模式,建议设置,否则会有很多不兼容的问题
set nu "显示行号
set cursorline "高亮显示当前行
"hi CursorLine cterm=NONE ctermbg=darkred ctermfg=white guibg=darkred guifg=white
"hi CursorColumn cterm=NONE ctermbg=darkred ctermfg=white guibg=darkred guifg=white
colorscheme desert "配色方案为desert
autocmd! bufwritepost vimrc source $MYVIMRC "自动载入vim配置文件
set showcmd "在状态栏显示正在输入的命令
" 文件编码配置
set encoding=utf8 fileencodings=utf-8,gb2312
" ---------------------------------------------------------------
" 查找/替换相关的设置
" ---------------------------------------------------------------
set hlsearch "开启高亮显示搜索结果
set incsearch "开启增量搜索
set gdefault "替换时所有的行内匹配都被替换,而不只是第一个
" ---------------------------------------------------------------
" 编程相关的设置
" ---------------------------------------------------------------
filetype indent on "针对不同的文件类型采用不同的缩进格式
syntax enable "语法高亮
syntax on "开启文件类型侦测
set tabstop=4 "设置tab键的宽度
set shiftwidth=4 "换行时行间交错使用4个空格
set autoindent "自动对齐
set smartindent "智能对齐方式
"set showmatch "设置匹配模式,类似当输入一个左括号时会匹配相应的那个右括号
"set path+=/usr/include/c++/4.7, "设置vim搜索文件的路径
" ---------------------------------------------------------------
" 全能补全
" 使用:help new-omin-completion查看全能补全介绍
" ---------------------------------------------------------------
filetype plugin indent on "启用自动补全
set completeopt=longest,menu "按Ctrl+N进行代码补全
" ---------------------------------------------------------------
" 代码折叠
" :help usr_28.txt和:help fold.txt查看相关介绍
" za打开/关闭当前折叠
" ---------------------------------------------------------------
set foldmethod=syntax
set foldlevel=100 " 启动vim时不要自动折叠代码
" ---------------------------------------------------------------
" 单个文件编译
" ---------------------------------------------------------------
map <F5> :call Do_OneFileMake()<CR>
imap <F5> <esc>:call Do_OneFileMake()<CR>
function Do_OneFileMake()
exec "w"
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 "cw"
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
" ---------------------------------------------------------------
" QuickFix窗口:输出一些供选择的结果
" 使用:help quickfix命令查看QuickFix窗口的帮助文件
" :cw打开窗口 :ccl关闭窗口
" :cn切换到下一个结果 :cp切换到上一个结果
" ---------------------------------------------------------------
" ---------------------------------------------------------------
" 一键编译openGL程序
" ---------------------------------------------------------------
map <F4> :call CompileOpenGLRun()<CR>
imap <F4> <esc>:call CompileOpenGLRun()<CR>
func CompileOpenGLRun()
exec "w"
exec "!gcc % -g -o %< -lglut -lGL -lGLU"
exec "! ./%<"
endfunc
" ---------------------------------------------------------------
" 快捷键
" ---------------------------------------------------------------
"Ctrl + C 选中状态下复制
vnoremap <C-c> "+y
"Ctrl + V 粘贴剪切板中的内容
map <C-v> "+p
imap <C-v> <esc>"+pa
vmap <C-v> d"+P
"Ctrl + A 全选
map <C-a> ggvG
imap <C-a> <esc>ggvGa
vmap <C-a> ggvG
"Ctrl+S 保存
map <C-s> :w
imap <C-s> <Esc>:wa<cr>i<Right>
"切换到换buffer
nmap bn :bn<cr>
nmap bp :bp<cr>
"选中一段文字并全文搜索这段文字
vnoremap * y/<C-h>=escape(@", '\\/.*$^~[]')<CR><CR>
vnoremap # y?<C-h>=escape(@", '\\/.*$^~[]')<CR><CR>
" ---------------------------------------------------------------
" 判定当前操作系统类型
" ---------------------------------------------------------------
if(has("win32") || has("win95") || has("win64") || has("win16"))
let g:iswindows=1
else
let g:iswindows=0
endif
autocmd BufEnter * lcd %:p:h
"###############################################################################
" The following is the Plugins' setting
"###############################################################################
" ---------------------------------------------------------------
" Ctags程序:在文件里跳来跳去
" 使用<C-]>跳到变量或函数等的定义处
" 使用<C-T>跳回刚才调用的地方
" 使用ctags -R重新生成tags文件
" 使用:help usr_29查看tags的说明
" ---------------------------------------------------------------
set tags=tags
set tags+=./tags
set tags+=~/vimTags/cpp_src/tags
set tags+=~/vimTags/GL/tags
" ---------------------------------------------------------------
" Tlist插件:高效浏览源代码
" 使用:help taglist.txt查看帮助文件
" 使用:Tlist命令调出TagList窗口
" 在tag上回车跳到定义处,空格则在最下方的命令栏显示该函数的函数体
" ---------------------------------------------------------------
let Tlist_Show_One_File=1 "设置为1,显示1个文件的函数列表;设置为0,同时显示多个文件
let Tlist_Exit_OnlyWindow=1 "如果taglist窗口是最后一个窗口,则退出vim
let Tlist_File_Fold_Auto_Close=1 "同时显示多个文件中的tag时,可使taglist只显示当前文件tag,其它文件的tag都被折叠起来。
" ---------------------------------------------------------------
" winManager插件:将Taglist窗口和netrw窗口整合
" netrw.vim插件:文件浏览器,可以列出当前目录中的文件,用来查看工程中的源文件
" 使用wm命令调出Taglist和netrw窗口
" 使用:help winmanager查看帮助文件
" ---------------------------------------------------------------
let g:winManagerWindowLayout='FileExplorer|TagList'
nmap wm :WMToggle<cr>
" ---------------------------------------------------------------
" cscope程序:超过频的ctags
" cscope的主要功能是通过不同的子命令“find”来实现的
" 使用:cs reset重新初始化所有连接
" 使用:help if_cscop.txt命令查看帮助文件
" ---------------------------------------------------------------
map <F12> :call Do_CsTag()<CR>
imap <F12> <esc>:call Do_CsTag()<CR>
" <C-_>g的按法是先按:Ctrl+Shift+-,然后很快按g
" 查找本C符号
nmap <C-_>s :cs find s <C-R>=expand("<cword>")<CR><CR>:copen<CR>
" 查找本定义
nmap <C-_>g :cs find g <C-R>=expand("<cword>")<CR><CR>
" 查找调用本函数的函数
nmap <C-_>c :cs find c <C-R>=expand("<cword>")<CR><CR>:copen<CR>
" 查找本字符串
nmap <C-_>t :cs find t <C-R>=expand("<cword>")<CR><CR>:copen<CR>
" 查找本egrep模式
nmap <C-_>e :cs find e <C-R>=expand("<cword>")<CR><CR>:copen<CR>
" 查找本文件
nmap <C-_>f :cs find f <C-R>=expand("<cfile>")<CR><CR>:copen<CR>
" 查找包含本文件的文件
nmap <C-_>i :cs find i ^<C-R>=expand("<cfile>")<CR>$<CR>:copen<CR>
" 查找本函数调用的函数
nmap <C-_>d :cs find d <C-R>=expand("<cword>")<CR><CR>:copen<CR>
function Do_CsTag()
exec "w"
let dir = getcwd()
if filereadable("tags")
if(g:iswindows==1)i
let tagsdeleted=delete(dir."\\"."tags")
else
let tagsdeleted=delete("./"."tags")
endif
if(tagsdeleted!=0)
echohl WarningMsg | echo "Fail to do tags! I cannot delete the tags" | echohl None
return
endif
endif
if has("cscope")
silent! execute "cs kill -1"
endif
if filereadable("cscope.files")
if(g:iswindows==1)
let csfilesdeleted=delete(dir."\\"."cscope.files")
else
let csfilesdeleted=delete("./"."cscope.files")
endif
if(csfilesdeleted!=0)
echohl WarningMsg | echo "Fail to do cscope! I cannot delete the cscope.files" | echohl None
return
endif
endif
if filereadable("cscope.out")
if(g:iswindows==1)
let csoutdeleted=delete(dir."\\"."cscope.out")
else
let csoutdeleted=delete("./"."cscope.out")
endif
if(csoutdeleted!=0)
echohl WarningMsg | echo "Fail to do cscope! I cannot delete the cscope.out" | echohl None
return
endif
endif
if(executable('ctags'))
"silent!execute "!ctags-R --c-types=+p --fields=+S *"
silent! execute "!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q ."
endif
if(executable('cscope') && has("cscope") )
if(g:iswindows!=1)
silent! execute "!find . -name '*.h' -o -name '*.c' -o -name '*.cpp' -o -name '*.java' -o -name '*.cs' > cscope.files"
else
silent! execute "!dir /s/b *.c,*.cpp,*.h,*.java,*.cs >> cscope.files"
endif
silent! execute "!cscope -b"
execute "normal :"
if filereadable("cscope.out")
execute "cs add cscope.out"
endif
endif
endfunction
" ---------------------------------------------------------------
" A插件:c/h文件之间相互切换
" :A在新Buffer中切换到c/h文件
" :AS横向分割窗口并打开c/h文件
" :AV纵向分割窗口并打开c/h文件
" :AT新建一个标签页并打开c/h文件
" ---------------------------------------------------------------
"nnoremap <silent> <F12> :A<CR>
" ---------------------------------------------------------------
" Grep插件:在工程中查找
" 帮助手册在grep.vim文件头部
" ---------------------------------------------------------------
nnoremap <silent> <F3> :Grep<CR>
" ---------------------------------------------------------------
" MiniBufExplorer插件:快速浏览和操作Buffer
" 帮助手册在grep.vim文件头部
" :help buffer查看buffer概念
" <Tab>向前循环切换到每个buffer名上
" <S-Tab>向后循环切换到每个buffer名上
" <Enter>在打开光标所在的buffer
" d删除光标所在的buffer
" ---------------------------------------------------------------
" <C-Tab>向前循环切换到每个buffer上,并在但前窗口打开
" <C-S-Tab>向后循环切换到每个buffer上,并在但前窗口打开
let g:miniBufExplMapCTabSwitchBufs = 1
" <C-h,j,k,l>切换到上下左右的窗口中
let g:miniBufExplMapWindowNavVim = 1
" <C-箭头键>切换到上下左右窗口
let g:miniBufExplMapWindowNavArrows = 1
" ---------------------------------------------------------------
" 对NERD_commenter的设置
" ---------------------------------------------------------------
let NERDShutUp=1
"不添加此配置的话,,cc不起作用
",cm是多行注释,,cu是取消注释
let mapleader = ","
" ---------------------------------------------------------------
" 显示垂直对齐线,有两种方法
" ---------------------------------------------------------------
"第一种,用空格缩进,对indent guides的设置
"使用<mapleader>ig启动
let g:indent_guides_guide_size=1
"第二种,用tab缩进
:set list lcs=tab:\|\
" ---------------------------------------------------------------
" echofunc插件:提示函数原型
" 通过按键"Alt+-"和"Alt+="向前和向后翻页
" ---------------------------------------------------------------
" ---------------------------------------------------------------
" omnicppcomplete插件:实现写C/C++语言时自动补全
" 通过按键"Alt+-"和"Alt+="向前和向后翻页
" ---------------------------------------------------------------
vimrc文件链接: http://pan.baidu.com/share/link?shareid=1311100005&uk=1728829166
vim插件链接:http://pan.baidu.com/share/link?shareid=1309236327&uk=1728829166