vim插件和配置
插件
pathogen 可以方便地管理vim插件
在没有pathogen的情况下,vim插件的文件全部都放在.vim目录,卸载插件很麻烦,pathogen可以将不同的插件放在一个单独的目录下,安装插件只需要将插件的文件夹复制到制定文件夹(bundle)内,卸载插件时只需要删除文件夹,方便好用。
以下插件都可以采用“下载zip包到vundle文件夹,解压zip包”的方法进行安装
下载及安装https://github.com/tpope/vim-pathogen
nerdtree 用于查看目录树
下载https://github.com/scrooloose/nerdtree
tabular 用于格式化代码
下载https://github.com/godlygeek/tabular
Tagbar 查看代码结构
下载https://github.com/majutsushi/tagbar
airline 用于设置vim主题
下载https://github.com/bling/vim-airline
molokai 用于设置vim主题
下载https://github.com/tomasr/molokai
mru 保存最近打开的文件
下载https://github.com/vim-scripts/mru.vim
easymotion 用于搜索
下载https://github.com/easymotion/vim-easymotion
ctrlp 强大的搜索插件
下载https://github.com/kien/ctrlp.vim
AutoComplPop 用于自动补全
下载https://github.com/vim-scripts/AutoComplPop
omnicppcomplete 用于自动补全
下载https://github.com/vim-scripts/OmniCppComplete
配置
基本配置
按键映射
按键映射的配置都写在.vimrc中,.vimrc放在主目录
imap jk <ESC\> " 按jk退出编辑状态,再也不用小指头按Esc了
map wq <ESC>:wq<CR> " 按wq就直接保存退出
imap wq <ESC>:wq<CR> " 按wq就直接保存退出
map qq <ESC>:q!<CR> " 按qq就直接不保存退出
imap qq <ESC>:q!<CR> " 按qq就直接不保存退出
map <F2> <ESC><C-W><C-W> " 按F2进行窗口切换,不按<C-W><C-W>
map <F3> <ESC>:Tagbar<CR> " 按F3打开代码结构
map <F4> <ESC>:NERDTreeToggle<CR>
map <F12> <ESC>:!ctags -R --c++-kinds=+px --fields=+aiKSz --extra=+q .<CR> " 按F12生成tag
ctrlp按键映射
map ,pp <ESC>:CtrlP<CR> "搜索文件
map ,pd <ESC>:CtrlPDir<CR> "搜索文件夹
map ,pu <ESC>:CtrlPUndo<CR> "搜索修改记录,可以回退
map ,pl <ESC>:CtrlPLine<CR> "搜索行
map ,pc <ESC>:CtrlPChangeAll<CR> "搜索修改记录,可以定位光标
map ,pm <ESC>:CtrlPMRU<CR> "搜索最近修改的文件
补全
vim中的自动补全很麻烦,但是写代码没有自动补全完全无法接受。为了实现自动补全,我一开始使用omnicppcomplete和AutoComplPop,后来听说有个牛逼的插件叫做YouCompleteMe,可以进行语法分析,实现自动补全,于是我就去试试。我们的开发机无法访问外网,也不能直接用root用户下直接安装软件,安装YouCompleteMe确实麻烦。我先后用源码编译安装了cmake、llvm、python、vim和YouCompleteMe,花了不少时间,终于把YouCompleteMe装好了,不过使用的时候,补全还是不够智能,没有想象中那么好。后来搬到了新的开发机,不想再重新安装YouCompleteMe了,重新回到omnicppcomplete和AutoComplPop。最后我发现,omnicppcomplete和AutoComplPop实现的补全效果比YouCompleteMe好。几乎每个vim使用者都会面临自动补全的功能,在这里,我建议使用omnicppcomplete和AutoComplPop组合,简单方便,效果又好。
因为项目是使用blade build编译,BUILD记录了项目的依赖,于是我写了一个python脚本去分析BUILD文件,获取相应的依赖,再根据依赖去生成tag,提高代码补全能力。最终,vim的代码补全能力也不错!使用方法如下:
- 安装ctags、omnicppcomplete和AutoComplPop
- 在vim中添加一句配置:set tags+=./deps_tags
- 在BUILD所在目录下运行deps_tag.py脚本,根据依赖生成deps_tags
- 按F12生成本项目的tags
- 愉快地使用补全
- 这里其实可以做到每次保存文件都自动重新生成tags,但是因为tags是根据所在工作路径的,如果不是在项目路径就会生成没必要的tags
最后,欢迎使用我的vim配置
" set coding
set termencoding=utf-8
set encoding=utf-8
set fileencoding=utf-8
set fenc=utf-8
set fencs=utf-8,usc-bom,euc-jp,gb18030,gbk,gb2312,cp936
set backspace=indent,eol,start
set nocompatible " be iMproved, required
set mouse=a " mouse useful
set nu " set number
set nocp
set nobackup " no backup
set noerrorbells " no voice
set showmatch " show match of () {} []
set ignorecase " ignore case in search
set cursorline " hightlight current line
set cursorcolumn " hightlight current collum
"set autochdir " auto change dir
set autoindent " autoindent 是自动缩进, cindent是特别针对C语言语法自动缩进
set smartindent
set cindent
set tabstop=4 " 制表符为4
set softtabstop=4
set shiftwidth=4
set scrolloff=3 " 光标移动到buffer的顶部和底部时保持3行距离
set cmdheight=2 " 命令行(在状态行下)的高度,默认为1,这里是2
set statusline=%F%m%r%h%w[%p%%] " 我的状态行显示的内容
set laststatus=2 " 总是显示状态行
syntax on " 语法高亮
" key map
imap jk <ESC> " 按jk退出编辑状态
map wq <ESC>:wq<CR>
imap wq <ESC>:wq<CR>
map qq <ESC>:q!<CR>
imap qq <ESC>:q!<CR>
map cp <ESC>:set mouse=c<CR>:set nonu<CR>
map ncp <ESC>:set mouse=a<CR>:set nu<CR>
map ,pa <ESC>:set paste<CR>
map ,np <ESC>:set nopaste<CR>
map gn <ESC>:tabnext<CR>
map gm <ESC><C-W><C-W>
map <F2> <ESC><C-W><C-W>
map <F3> <ESC>:Tagbar<CR>
map <F4> <ESC>:NERDTreeToggle<CR>
map <F12> <ESC>:!ctags -R --c++-kinds=+px --fields=+aiKSz --extra=+q .<CR>
"pathogen
filetype off " required
call pathogen#infect()
filetype plugin on
"theme
syntax enable
set t_Co=256
let g:airline_theme="dark"
set background=dark
colorscheme molokai
"let g:AutoOpenWinManager = 1
let g:NERDTreeDirArrows = 1
let g:nerdtree_tabs_autoclose = 0
let g:bufExplorerSplitVertSize = 30
let g:bufExplorerMaxHeight=30
let g:winManagerWindowLayout = "FileExplorer|BufExplorer"
let g:winManagerWidth = 30
"easymotion
map / <Plug>(easymotion-sn)
omap / <Plug>(easymotion-tn)
map n <Plug>(easymotion-next)
map N <Plug>(easymotion-prev)
map <Leader>l <Plug>(easymotion-lineforward)
map <Leader>j <Plug>(easymotion-j)
map <Leader>k <Plug>(easymotion-k)
map <Leader>h <Plug>(easymotion-linebackward)
let g:EasyMotion_use_upper = 1 " Use uppercase target labels and type as a lower case
let g:EasyMotion_smartcase = 1 " type `l` and match `l`&`L`
let g:EasyMotion_use_smartsign_us = 1 " Smartsign (type `3` and match `3`&`#`)
"easygrep
let g:EasyGrepMode = 2 " All:0, Open Buffers:1, TrackExt:2
"let g:EasyGrepCommand = 0 " Use vimgrep:0, grepprg:1
let g:EasyGrepRecursive = 1 " Recursive searching
let g:EasyGrepIgnoreCase = 1 " not ignorecase:0
let g:EasyGrepFilesToExclude = "*.bak, *~, cscope.*, *.a, *.o, *.pyc, *.bak"
"let g:EasyGrepOpenWindowOnMatch = 0
"ctrlp
let g:ctrlp_by_filename = 1
let g:ctrlp_working_path_mode = '0'
"let g:ctrlp_cmd = 'CtrlPMixed'
let g:ctrlp_match_window = 'bottom,order:ttb,min:1,max:10,results:20'
"let g:ctrlp_regexp = 0
let g:ctrlp_extensions = ['buffertag', 'dir', 'undo', 'line', 'changes', 'mixed', 'bookmarkdir']
map ,pp <ESC>:CtrlP<CR>
map ,pd <ESC>:CtrlPDir<CR>
map ,pu <ESC>:CtrlPUndo<CR>
map ,pl <ESC>:CtrlPLine<CR>
map ,pc <ESC>:CtrlPChangeAll<CR>
map ,pb <ESC>:CtrlPBookmarkDir<CR>
map ,pm <ESC>:CtrlPMRU<CR>
"acp
let g:acp_enableAtStartup = 1
let g:acp_completeoptPreview = 0
let g:acp_ignorecaseOption = 1
let g:acp_behaviorKeywordCommand = "\<C-p>"
let g:acp_behaviorKeywordLength = 1
"omnicppcomplete
let OmniCpp_MayCompleteDot = 1 " autocomplete with .
let OmniCpp_MayCompleteArrow = 1 " autocomplete with ->
let OmniCpp_MayCompleteScope = 1 " autocomplete with ::
let OmniCpp_SelectFirstItem = 2 " select first item
let OmniCpp_NamespaceSearch = 2 " search namespaces in this and included files
let OmniCpp_ShowPrototypeInAbbr = 1 " show function prototype in popup window
let OmniCpp_GlobalScopeSearch=1 " enable the global scope search
let OmniCpp_DisplayMode=1 " Class scope completion mode: always show all members
let OmniCpp_DefaultNamespaces=["std"]
" au CursorMovedI,InsertLeave * if pumvisible() == 0|silent! pclose|endif " 自动关闭补全窗口
set completeopt=menu,longest
set tags+=./deps_tags