@searcher2xiao
2015-07-20T08:35:51.000000Z
字数 5529
阅读 8464
CentOS vim ycm
原本想在CentOS6.6下搞vim的集成开发环境,中间遇到了各种问题,包括gcc版本(因为之前装系统的时候没有划分swap分区,编译高版本gcc时失败),Python版本,后来索性放弃了,转至CentOS 7下做。CentOS 7本身自带的vim是7.4版本的,支持Python,gcc版本是4.8.3,能顺利编译clang。连CentOS6.6和CentOS7搞了一天一夜,纪念一下可算把vim+ycm整出来了,做个记录。下面的内容都是参考别人的成果,具体链接在小标题中都有给出,如有不明白之处,请按图索骥。既然都是别人写的,自己的部分只有几张图片和几个输出信息,那这篇博文自然也是转载的了。
系统首先得有gcc编译器,编译clang需要借助llvm框架,编译条件如下:
| Package | Version | Notes |
|---|---|---|
| GNU Make | 3.79,3.79.1 | Makefile/build processor |
| GCC | >=4.7.0 | C/C++ complier1 |
| python | >=2.7 | Automated test suite2 |
| GNU M4 | 1.4 | Marcro processor for configuration3 |
| GNU Autoconf | 2.60 | Configuration script builder3 |
| GNU Automake | 1.9.6 | aclocal macro genertaor3 |
| libtool | 1.5.22 | Shared library manager3 |
| zlib | >=1.2.3.4 | Compression library4 |
安装svn,用于下载clang源码。
yum install svn
新建目录,进入工作
svn co http://llvm.org/svn/llvm-project/llvm/trunk llvmcd llvm/toolssvn co http://llvm.org/svn/llvm-project/cfe/trunk clangcd ../..cd llvm/tools/clang/toolssvn co http://llvm.org/svn/llvm-project/clang-tools-extra/trunk extracd ../../../..cd llvm/projectssvn co http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rtcd ../..
编译
mkdir buildcd build../llvm/configure --enable-optimized --enable-targets=host-onlymakemake install
查看编译是否成功
[root@localhost build]#clang --versionclang version 3.7.0 (trunk 242040)Target: x86_64-unknown-linux-gunThread model: posix[root@localhost build]#clang++ --versionclang version 3.7.0 (trunk 242040)Target: x86_64-unknown-linux-gunThread model: posix
编码工作中偏爱Sublime Text2的背景色,因此找了个和这个差不多的,名为monokai。喜欢的朋友可以到这里下载。下面说说怎么对vim主题进行设置。
- 创建文件夹.vim(如有则跳过)
mkdir ~/.vim
- 在.vim下创建colors目录,将下载的主题文件*.vim放到这个目录下
- 编辑.vimrc文件,在其中加入两行代码:
vim ~/.vimrc
syntax enablecolorscheme monokai
- 效果如下
- 下载Vundle
git clone https://github.com/gmarik/Vundle.vim.git ~/.vim/bundle/Vundle.vim
- 参考~/.vim/bundle/Vundle.vim/README.md配置vimrc,配置如下:
```vimset nocompatible " be iMproved, requiredfiletype off " required"" set the runtime path to include Vundle and initializeset rtp+=~/.vim/bundle/Vundle.vimcall vundle#begin()" alternatively, pass a path where Vundle should install plugins"call vundle#begin('~/some/path/here')"" let Vundle manage Vundle, requiredPlugin 'gmarik/Vundle.vim'"" The following are examples of different formats supported." Keep Plugin commands between vundle#begin/end." plugin on GitHub repoPlugin 'tpope/vim-fugitive'" plugin from http://vim-scripts.org/vim/scripts.htmlPlugin 'L9'" Git plugin not hosted on GitHubPlugin 'git://git.wincent.com/command-t.git'" git repos on your local machine (i.e. when working on your own plugin)Plugin 'file:///home/gmarik/path/to/plugin'" The sparkup vim script is in a subdirectory of this repo called vim." Pass the path to set the runtimepath properly.Plugin 'rstacruz/sparkup', {'rtp': 'vim/'}" Avoid a name conflict with L9Plugin 'user/L9', {'name': 'newL9'}"" All of your Plugins must be added before the following linecall vundle#end() " requiredfiletype plugin indent on " required" To ignore plugin indent changes, instead use:"filetype plugin on"" Brief help" :PluginList - lists configured plugins" :PluginInstall - installs plugins; append `!` to update or just :PluginUpdate" :PluginSearch foo - searches for foo; append `!` to refresh local cache" :PluginClean - confirms removal of unused plugins; append `!` to auto-approve removal"" see :h vundle for more details or wiki for FAQ" Put your non-Plugin stuff after this line
这里简单说明一下如何使用这个插件管理器,首先把想要安装的插件放在这两行代码之间
call vundle#begin()call vundle#end()
其中第12行是必装的Plugin,这样就可以用Vundle来管理Vundle自己。用法在36至40行有简单的说明。
- 在.vimrc中添加
Plugin 'Valloric/YouCompleteMe'
- 安装插件,打开vim,执行
:PluginInstall
- 创建文件夹放置编译文件
cd ~mkdir ycm_buildcd ycm_build
- 生成Makefile
cmake -G "Unix Makefiles" -DEXTERNAL_LIBCLANG_PATH=/usr/local/lib/libclang.so ~/.vim/bundle/YouCompleteMe/cpp
- make
make ycm_support_libs
vimrc中添加如下内容。
" ycm setting" 让vim的补全菜单行为与一般IDE一致set completeopt=longest,menu"离开插入模式后自动关闭预览窗口autocmd InsertLeave * if pumvisible() == 0|pclose|endif"回车即选中当前项inoremap <expr> <CR> pumvisible()?"\<C-y>":"<CR>""上下左右键的行为 会显示其他信息inoremap <expr> <Down> pumvisible() ? "\<C-n>" : "\<Down>"inoremap <expr> <Up> pumvisible() ? "\<C-p>" : "\<Up>"inoremap <expr> <PageDown> pumvisible() ? "\<PageDown>\<C-p>\<C-n>" : "\<PageDown>"inoremap <expr> <PageUp> pumvisible() ? "\<PageUp>\<C-p>\<C-n>" : "\<PageUp>"""youcompleteme 默认tab s-tab 和自动补全冲突let g:ycm_key_list_select_completion = ['<Down>']let g:ycm_key_list_previous_completion = ['<Up>']"let g:ycm_global_ycm_extra_conf='~/.ycm_extra_conf.py'" 开启 YCM 基于标签引擎let g:ycm_collect_identifiers_from_tag_files = 1" 从第2个键入字符就开始罗列匹配项let g:ycm_min_num_of_chars_for_completion=2" 禁止缓存匹配项,每次都重新生成匹配项let g:ycm_cache_omnifunc=0" 语法关键字补全let g:ycm_seed_identifiers_with_syntax = 1let g:ycm_confirm_extra_conf=0let g:ycm_key_invoke_completion = '<C-/>'" 在接受补全后不分裂出一个窗口显示接受的项set completeopt-=preview"force recomile with syntasticnnoremap <F5> :YcmForceCompileAndDiagnostics<CR>""在注释输入中也能补全let g:ycm_complete_in_comments = 1"在字符串输入中也能补全let g:ycm_complete_in_strings = 1"注释和字符串中的文字也会被收入补全let g:ycm_collect_identifiers_from_comments_and_strings = 0""设置error和warning的提示符,如果没有设置,ycm会以syntastic的g:syntastic_warning_symbol"和 g:syntastic_error_symbol 这两个为准let g:ycm_error_symbol='>>'let g:ycm_warning_symbol='>*'"设置跳转的快捷键,可以跳转到definition和declarationnnoremap <leader>gc :YcmCompleter GoToDeclaration<CR>nnoremap <leader>gf :YcmCompleter GoToDefinition<CR>nnoremap <leader>gg :YcmCompleter GoToDefinitionElseDeclaration<CR>
- 在.vimrc中添加
Plugin 'scrooloose/nerdtree'
- 安装插件,打开vim,执行
:PluginInstall
vimrc中添加如下内容。
" nerdtree设置map <F2> :NERDTreeToggle<CR>let NERDTreeIgnore=['\.o$', '\.ko$', '\.so$', '\.a$', '\.swp$', '\.bak$', '\~$']let NERDTreeSortOrder=['\/$', 'Makefile', '\.c$', '\.cc$', '\.cpp$', '\.h$', '*', '\~$']let NERDTreeMinimalUI=1let NERDTreeQuitOnOpen=1