1.环境

本文使用VMWare虚拟机进行实验,客户机系统是CentOS 7.2最小安装(CentOS-7-x86_64-Minimal-1511.iso)

最终实现效果:安装vim8 + python2.7(自带)+ ycm,实现C/C++的智能提示、补全、跳转。

2.需求

  • Git
  • GCC
  • CMake(3.10以上)
  • Vim
  • Vundle
  • YouCompleteMe
  • 各种依赖库

3.安装

3.1 下载安装必要的组件

yum install -y gcc gcc-c++ ruby ruby-devel lua lua-devel  \
ctags git python python-devel \
tcl-devel ncurses-devel \
perl perl-devel perl-ExtUtils-ParseXS \
perl-ExtUtils-CBuilder \
perl-ExtUtils-Embed

3.2 安装Vim8

访问https://github.com/vim/vim/releases,下载最新的release版本,我这里是vim-8.0.1645.tar.gz

然后解压配置编译安装

tar zxvf vim-8.0..tar.gz
cd vim-8.0. ./configure --with-features=huge \
--enable-multibyte \
--enable-rubyinterp=yes \
--enable-pythoninterp=yes \
--with-python-config-dir=/usr/lib64/python2./config \
--enable-perlinterp=yes \
--enable-luainterp=yes \
--enable-cscope \
--prefix=/usr/local make VIMRUNTIMEDIR=/usr/local/share/vim/vim80
make install

注意: --with-python-config-dir这项要看自己的实际路径,是python-devel带的目录

更改下系统编辑器

sudo update-alternatives --install /usr/bin/editor editor /usr/local/bin/vim
sudo update-alternatives --set editor /usr/local/bin/vim
sudo update-alternatives --install /usr/bin/vi vi /usr/local/bin/vim
sudo update-alternatives --set vi /usr/local/bin/vim

3.3 安装YouCompleteMe

3.3.1 下载Vundle

git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim

配置~/.vimrc文件,填入如下内容

set nocompatible              " be iMproved, required
filetype off " required " set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here') " let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'
Plugin 'Valloric/YouCompleteMe' " All of your Plugins must be added before the following line
call vundle#end() " required
filetype 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

3.3.2 下载YouCompleteMe

打开vim,输入如下命令

:PluginInstall

注意:这里很考验网速,可能会比较慢,下载好后,整个.vim文件夹大约150~250MB(根据时间版本不同)。

3.3.3 下载CMake(yum安装的2.8版本不行)

访问https://cmake.org/download/,我这里是cmake-3.10.3.tar.gz

解压编译安装

tar zxvf cmake-3.10..tar.gz
cd cmake-3.10.
./bootstrap && make && make install

3.3.4 编译YouCompleteMe(支持C/C++)

cd ~/.vim/bundle/YouCompleteMe/
./install.py --clang-completer

编译完成后,整个.vim文件夹大约350MB。

注意:如果使用--clang-completer选项,脚本会判断当前是否有upstream pre-build libclang.so库(不建议用系统的libclang):

如果有则用缓存的(见缓存位置);如果没有则会去下载库(见下载地址)。

看网络状况,可能会下载不全(sha256值不对),导致下面编译YouCompleteMe时出错。可以先用迅雷下载,再替换过去。

下载地址:https://dl.bintray.com/micbou/libclang/libclang-6.0.0-x86_64-linux-gnu-ubuntu-14.04.tar.bz2

缓存位置:.vim/bundle/YouCompleteMe/third_party/ycmd/clang_archives/

3.3.5 配置YouCompleteMe

编辑~/.vimrc,增加以下内容

let g:ycm_global_ycm_extra_conf='~/.ycm_extra_conf.py'  "设置全局配置文件的路径
let g:ycm_seed_identifiers_with_syntax= " 语法关键字补全
let g:ycm_confirm_extra_conf= " 打开vim时不再询问是否加载ycm_extra_conf.py配置
let g:ycm_key_invoke_completion = '<C-a>' " ctrl + a 触发补全,防止与其他插件冲突
set completeopt=longest,menu "让Vim的补全菜单行为与一般IDE一致(参考VimTip1228)
nnoremap <leader>jd :YcmCompleter GoToDefinitionElseDeclaration<CR> "定义跳转快捷键

第一行所需的.ycm_extra_conf.py,可以从自带的复制一份过来,再根据需要进行配置

cp .vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py ~

4.运行

现在运行vim已经可以对C++进行提示补全了(因为刚才是从cpp子目录下拷贝的配置样例),

打开~/.ycm_extra_conf.py可以看到其中flags变量,就是配置项目代码补全提示的

比如(C++项目)

flags = [
'-Wall',
'-Wextra',
'-Werror',
'-fexceptions',
'-DNDEBUG',
'-std=c++11',
'-x',
'c++',
'-isystem',
'/usr/include',
'-isystem',
'/usr/local/include',
'-I',
'/root/my_proj',
]

如果是C项目,需要调整

-std=c99,-x下面的参数改为c。

解析C/C++相关的符号提示(系统的、三方库的、自己的),需要配置-isystem,'-I',参数为头文件所在目录。

配置好后,用vim编辑一个C文件,

输入#include<会自动弹出提示窗

结构体输入.时会提示成员,

结构体指针输入->时会提示成员

YouCompleteMe默认不会像IDE那样主动提示符号补全(如输入pri提示printf),想要补全时按Ctrl+A(见上面配置)。

以上弹出提示窗,按Ctrl+N、Ctrl+P上下选择补全。

符号跳转

光标定位到需要跳转的符号上,输入\jd(\是默认leader),实现跳转(该跳转有条件,不一定100%成功,可见官网说明

在printf上按\jd,跳转到定义

按Ctrl+o跳回之前的位置。

5.其他

实际使用每个项目单独一个.ycm_extra_conf.py配置,GitHub上有个项目可以自动生成配置文件, 有需要可以参考下。

CentOS7.2安装Vim8和YouCompleteMe的更多相关文章

  1. Centos7安装vim8.0 + YouCompleteMe

    更新yum sudo yum upgrade sduo yum update 下载git sudo yum install git 升级vim以及gcc 升级gcc sudo yum install ...

  2. 学以致用八---centos7.2 安装vim8+支持python3

    目的:打造基于python的vim环境 也是在地铁上突然产生的一个想法,平时都是在pycharm上练习python,但有时候会提示激活码过期,又得上网找激活码,够折腾的.那何不在linux环境下来搭建 ...

  3. ubuntu16.04下编译安装vim8.1

    之前写过一篇centos7下编译安装vim8.0的教程,ubuntu16.04相比centos7下安装过程不同在于依赖包名字的不同,其余都是一样.下面给出ubuntu16.04编译安装vim8.0需要 ...

  4. Ubuntu16.04安装vim8

    Ubuntu16.04安装vim8 在Ubuntu16.04下编译安装vim8,并配置vim-plug插件管理器,以及安装YouCompleteMe等插件. 安装依赖 sudo apt-get ins ...

  5. Ubuntu 16.04安装Vim8.0

    Ubuntu 16.04安装Vim8.0 https://www.aliyun.com/jiaocheng/131859.html sudo add-apt-repository ppa:jonath ...

  6. 在centos7上安装Jenkins

    在centos7上安装Jenkins 安装 添加yum repos,然后安装 sudo wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins ...

  7. CentOS7 Jenkins安装

    CentOS7 Jenkins安装 CentOS7 Jenkins安装 Download 从Jenkins下载apache-tomcat-8.0.18.tar.gz Install 安装 上传RPM文 ...

  8. 在 CentOS7 上安装 zookeeper-3.4.9 服务

    在 CentOS7 上安装 zookeeper-3.4.9 服务 1.创建 /usr/local/services/zookeeper 文件夹: mkdir -p /usr/local/service ...

  9. 在 CentOS7 上安装 MongoDB

    在 CentOS7 上安装 MongoDB 1 通过 SecureCRT 连接至 CentOS7 服务器: 2 进入到 /usr/local/ 目录: cd /usr/local 3 在当前目录下创建 ...

随机推荐

  1. NCCloud 指令示例

    http://ansrlab.cse.cuhk.edu.hk/software/nccloud/ Implementation of NCCloud in C++ (updated: August 2 ...

  2. docker管理工具推荐(linux和windows)

    1.windows. 下载dokcer toolbox即可 2.linux 推荐rancher.安装链接参考:http://www.kaimingwan.com/post/rong-qi-yu-ron ...

  3. 如何用Eclipse将普通的JavaWeb项目转为Maven项目

    最新自己的第一个项目差不多稳定运行之后 想着将项目转为Maven项目.于是参考网上成功的将自己的普通的项目转为了maven项目,现在记录一下: 0.普通的java项目的结构如下: 1.接下来开始进行正 ...

  4. MongoDB_单机版环境搭建

    linux上安装MogoDB http://www.runoob.com/mongodb/mongodb-linux-install.html 在https://www.mongodb.com/dow ...

  5. Mac快速查看隐藏文件

    使用终端 显示隐藏文件的最简单方法是使用终端.只要打开终端(位于应用程序--实用工具),将以下代码复制进去然后回车 defaults write com.apple.finder AppleShowA ...

  6. laravel 数据库配置

    数据库配置文件为项目根目录下的config/database.php //默认数据库为mysql 'default' => env('DB_CONNECTION', 'mysql'), 'mys ...

  7. ansible、zabbix、tcpdump

    Ansible 源码安装 https://blog.csdn.net/williamfan21c/article/details/53439307 Ansible安装过程中常遇到的错误 http:// ...

  8. NIO与传统IO的区别(形象比喻)[转]

    传统的socket IO中,需要为每个连接创建一个线程,当并发的连接数量非常巨大时,线程所占用的栈内存和CPU线程切换的开销将非常巨大.使用NIO,不再需要为每个线程创建单独的线程,可以用一个含有限数 ...

  9. oracle-统计员工x

    1. SELECTe.depid,avg(s.bonussalary+s.basesalary) AS avgsal from employ e,salary s where e.employId=s ...

  10. Ubuntu下Deb软件包相关安装与卸载

    安装deb软件包 sudo dpkg -i xxx.deb 删除软件包 sudo dpkg -r xxx.deb 连同配置文件一起删除 sudo dpkg -r --purge xxx.deb 查看软 ...