[关闭]
@searcher2xiao 2015-07-20T16:35:51.000000Z 字数 5529 阅读 8157

CentOS 7 + vim + ycm (clang) + NERDTree

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整出来了,做个记录。下面的内容都是参考别人的成果,具体链接在小标题中都有给出,如有不明白之处,请按图索骥。既然都是别人写的,自己的部分只有几张图片和几个输出信息,那这篇博文自然也是转载的了。

一、安装clang(http://www.noevil.me/archives/900.htm

1、工具准备

系统首先得有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

2、获取svn源码

安装svn,用于下载clang源码。

  1. yum install svn

3、下载源码

新建目录,进入工作

  1. svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm
  2. cd llvm/tools
  3. svn co http://llvm.org/svn/llvm-project/cfe/trunk clang
  4. cd ../..
  5. cd llvm/tools/clang/tools
  6. svn co http://llvm.org/svn/llvm-project/clang-tools-extra/trunk extra
  7. cd ../../../..
  8. cd llvm/projects
  9. svn co http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
  10. cd ../..

编译

  1. mkdir build
  2. cd build
  3. ../llvm/configure --enable-optimized --enable-targets=host-only
  4. make
  5. make install

查看编译是否成功

  1. [root@localhost build]#clang --version
  2. clang version 3.7.0 (trunk 242040)
  3. Target: x86_64-unknown-linux-gun
  4. Thread model: posix
  5. [root@localhost build]#clang++ --version
  6. clang version 3.7.0 (trunk 242040)
  7. Target: x86_64-unknown-linux-gun
  8. Thread model: posix

二、vim设置(http://www.cnblogs.com/voidy/p/4637683.html

1、主题设置

编码工作中偏爱Sublime Text2的背景色,因此找了个和这个差不多的,名为monokai。喜欢的朋友可以到这里下载。下面说说怎么对vim主题进行设置。

  • 创建文件夹.vim(如有则跳过)
  1. mkdir ~/.vim
  • 在.vim下创建colors目录,将下载的主题文件*.vim放到这个目录下
  • 编辑.vimrc文件,在其中加入两行代码:
  1. vim ~/.vimrc
  1. syntax enable
  2. colorscheme monokai
  • 效果如下
    effect1

2、安装插件管理器-Vundle

  • 下载Vundle
  1. git clone https://github.com/gmarik/Vundle.vim.git ~/.vim/bundle/Vundle.vim
  • 参考~/.vim/bundle/Vundle.vim/README.md配置vimrc,配置如下:
  1. ```vim
  2. set nocompatible " be iMproved, required
  3. filetype off " required
  4. "
  5. " set the runtime path to include Vundle and initialize
  6. set rtp+=~/.vim/bundle/Vundle.vim
  7. call vundle#begin()
  8. " alternatively, pass a path where Vundle should install plugins
  9. "call vundle#begin('~/some/path/here')
  10. "
  11. " let Vundle manage Vundle, required
  12. Plugin 'gmarik/Vundle.vim'
  13. "
  14. " The following are examples of different formats supported.
  15. " Keep Plugin commands between vundle#begin/end.
  16. " plugin on GitHub repo
  17. Plugin 'tpope/vim-fugitive'
  18. " plugin from http://vim-scripts.org/vim/scripts.html
  19. Plugin 'L9'
  20. " Git plugin not hosted on GitHub
  21. Plugin 'git://git.wincent.com/command-t.git'
  22. " git repos on your local machine (i.e. when working on your own plugin)
  23. Plugin 'file:///home/gmarik/path/to/plugin'
  24. " The sparkup vim script is in a subdirectory of this repo called vim.
  25. " Pass the path to set the runtimepath properly.
  26. Plugin 'rstacruz/sparkup', {'rtp': 'vim/'}
  27. " Avoid a name conflict with L9
  28. Plugin 'user/L9', {'name': 'newL9'}
  29. "
  30. " All of your Plugins must be added before the following line
  31. call vundle#end() " required
  32. filetype plugin indent on " required
  33. " To ignore plugin indent changes, instead use:
  34. "filetype plugin on
  35. "
  36. " Brief help
  37. " :PluginList - lists configured plugins
  38. " :PluginInstall - installs plugins; append `!` to update or just :PluginUpdate
  39. " :PluginSearch foo - searches for foo; append `!` to refresh local cache
  40. " :PluginClean - confirms removal of unused plugins; append `!` to auto-approve removal
  41. "
  42. " see :h vundle for more details or wiki for FAQ
  43. " Put your non-Plugin stuff after this line

这里简单说明一下如何使用这个插件管理器,首先把想要安装的插件放在这两行代码之间

  1. call vundle#begin()
  2. call vundle#end()

其中第12行是必装的Plugin,这样就可以用Vundle来管理Vundle自己。用法在36至40行有简单的说明。

3、安装插件YCM(http://blog.csdn.net/leaf5022/article/details/21290509#comments

  • 在.vimrc中添加
  1. Plugin 'Valloric/YouCompleteMe'
  • 安装插件,打开vim,执行
  1. :PluginInstall
  • 创建文件夹放置编译文件
  1. cd ~
  2. mkdir ycm_build
  3. cd ycm_build
  • 生成Makefile
  1. cmake -G "Unix Makefiles" -DEXTERNAL_LIBCLANG_PATH=/usr/local/lib/libclang.so ~/.vim/bundle/YouCompleteMe/cpp
  • make
  1. make ycm_support_libs

4、YCM配置

vimrc中添加如下内容。

  1. " ycm setting
  2. " vim的补全菜单行为与一般IDE一致
  3. set completeopt=longest,menu
  4. "离开插入模式后自动关闭预览窗口
  5. autocmd InsertLeave * if pumvisible() == 0|pclose|endif
  6. "回车即选中当前项
  7. inoremap <expr> <CR> pumvisible()?"\<C-y>":"<CR>"
  8. "上下左右键的行为 会显示其他信息
  9. inoremap <expr> <Down> pumvisible() ? "\<C-n>" : "\<Down>"
  10. inoremap <expr> <Up> pumvisible() ? "\<C-p>" : "\<Up>"
  11. inoremap <expr> <PageDown> pumvisible() ? "\<PageDown>\<C-p>\<C-n>" : "\<PageDown>"
  12. inoremap <expr> <PageUp> pumvisible() ? "\<PageUp>\<C-p>\<C-n>" : "\<PageUp>"
  13. "
  14. "youcompleteme 默认tab s-tab 和自动补全冲突
  15. let g:ycm_key_list_select_completion = ['<Down>']
  16. let g:ycm_key_list_previous_completion = ['<Up>']
  17. "
  18. let g:ycm_global_ycm_extra_conf='~/.ycm_extra_conf.py'
  19. " 开启 YCM 基于标签引擎
  20. let g:ycm_collect_identifiers_from_tag_files = 1
  21. " 从第2个键入字符就开始罗列匹配项
  22. let g:ycm_min_num_of_chars_for_completion=2
  23. " 禁止缓存匹配项,每次都重新生成匹配项
  24. let g:ycm_cache_omnifunc=0
  25. " 语法关键字补全
  26. let g:ycm_seed_identifiers_with_syntax = 1
  27. let g:ycm_confirm_extra_conf=0
  28. let g:ycm_key_invoke_completion = '<C-/>'
  29. " 在接受补全后不分裂出一个窗口显示接受的项
  30. set completeopt-=preview
  31. "force recomile with syntastic
  32. nnoremap <F5> :YcmForceCompileAndDiagnostics<CR>
  33. "
  34. "在注释输入中也能补全
  35. let g:ycm_complete_in_comments = 1
  36. "在字符串输入中也能补全
  37. let g:ycm_complete_in_strings = 1
  38. "注释和字符串中的文字也会被收入补全
  39. let g:ycm_collect_identifiers_from_comments_and_strings = 0
  40. "
  41. "设置errorwarning的提示符,如果没有设置,ycm会以syntasticg:syntastic_warning_symbol
  42. "和 g:syntastic_error_symbol 这两个为准
  43. let g:ycm_error_symbol='>>'
  44. let g:ycm_warning_symbol='>*'
  45. "设置跳转的快捷键,可以跳转到definitiondeclaration
  46. nnoremap <leader>gc :YcmCompleter GoToDeclaration<CR>
  47. nnoremap <leader>gf :YcmCompleter GoToDefinition<CR>
  48. nnoremap <leader>gg :YcmCompleter GoToDefinitionElseDeclaration<CR>

5、安装NERDTree

  • 在.vimrc中添加
  1. Plugin 'scrooloose/nerdtree'
  • 安装插件,打开vim,执行
  1. :PluginInstall

6、配置NERDTree

vimrc中添加如下内容。

  1. " nerdtree设置
  2. map <F2> :NERDTreeToggle<CR>
  3. let NERDTreeIgnore=['\.o$', '\.ko$', '\.so$', '\.a$', '\.swp$', '\.bak$', '\~$']
  4. let NERDTreeSortOrder=['\/$', 'Makefile', '\.c$', '\.cc$', '\.cpp$', '\.h$', '*', '\~$']
  5. let NERDTreeMinimalUI=1
  6. let NERDTreeQuitOnOpen=1
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注