在ubuntu16下安装vim的YCM补全

一:安装vim插件管理Vundle
git clone http://github.com/gmarik/vundle.git ~/.vim/bundle/Vundle.vim
没有的目录需要自行创建:mkdir -p ~/.vim/bundle


接着在.vimrc中加入 
set nocompatible "关闭与vi的兼容模式,避免旧版本的bug
filetype off "必需
set rtp+=~/.vim/bundle/Vundle.vim "设置运行路径
call vundle#begin() "vundle初始化


Plugin 'gmarik/Vundle.vim' "应放在所有插件前面


call vundle#end() "每一行插件都应该在这行之前




安装插件需要进入VIM界面输入[:PluginInstall]
清理闲置未用的插件需输入[:PluginClean]


二:安装vim补全插件YouCompleteMe


准备工作:
1. 首先查看vim对于python的支持:vim --version |grep pthon
将对python3的支持切换成python2的支持
安装py2包:sudo apt-get install vim-nox-py2
切换支持的命令:sudo update-alternatives --config vim
手动切换py2和py3
2. 安装官方哪个文档上说的CMake和python3-dev
sudo apt-get install build-essential cmake
sudo apt-get install python-dev python3-dev
3. 安装YCM安装需要的Clang
sudo apt-get instal Clang


正式安装:
首先,我们要明确一点,这是个写程序时相当好用的插件,但是它超!级!大!
一般情况下直接通过vundle添加路径安装,但是vim界面并没有进度条,而且推出后又要重头开始!
这对耐心是极大的考验……


所以有另外一个方法,建议直接下载在~/.vim/bundle/下


1.git clone http://github.com/Valloric/YouCompleteMe.git ~/.vim/bundle/YouCompleteMe
2.cd ~/.vim/bundle/YouCompleteMe
3. git submodule update--init--recursive


下载完成后,添加Plugin 'Valloric/YouCompleteMe' ,保存后在vim界面执行:PluginInstall就能瞬间完成了:)
下载完成后,可以选择YCM支持的语言:
像是需要添加C语言支持的
1.cd ~/.vim/bundle/YouCompleteMe
2../install.py --clang-completer --system-libclang


如果都有需要,保证xbuild,go,tsserver,node,npm,cargo等工具包均安装完成后
1.cd ~/.vim/bundle/YouCompleteMe
2../insta;;/py --all


YVM配置
官方文档中有:
YCM looks for a .ycm_extra_conf.py file in the directory of the opened file or in any directory above it in the hierarchy (recursively); when the file is found, it is loaded (only once!) as a Python module. 
在~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/下可以找到.ycm_extra_conf.py这个文件,可以每次使用时把其复制到当前目录下,也可以在~/.vimrc中进行配置。


1 let g:ycm_global_ycm_extra_conf = ‘~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py’       "配置全局路径
2 let g:ycm_confirm_extra_conf=0   "每次直接加载该文件,不提示是否要加载


同时要补全语言,要对.ycm_extra_conf.py进行修改:
在flags下添加在flags下添加


1 ‘-isystem‘,
2 ‘/usr/include‘,
3 ‘-isystem‘,
4 ‘usr/include/c++/5.4.0‘
5 ‘-isystem‘,
6 ‘usr/include/x86_64-linux-gnu/c++‘,


并注释掉这一段:


1 try:
2      final_flags.remove( ‘-stdlib-libc++‘ )
3 except ValueError:
4      pass


至此,就可以使用YCM补全代码了。
Ubuntu 上使用 Vim 编辑器时,可以通过以下步骤启用自动补全代码功能: 1. 安装 Vim 插件管理器 Vundle: ```bash git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim ``` 2. 在 `~/.vimrc` 文件中添加以下内容: ```vim set nocompatible " be iMproved, required filetype off " required set rtp+=~/.vim/bundle/Vundle.vim call vundle#begin() Plugin 'Valloric/YouCompleteMe' call vundle#end() " required filetype plugin indent on " required let g:ycm_autoclose_preview_window_after_completion=1 let g:ycm_min_num_of_chars_for_completion=2 let g:ycm_collect_identifiers_from_comments_and_strings=1 let g:ycm_complete_in_strings=1 let g:ycm_complete_in_comments=1 let g:ycm_key_invoke_completion='<C-Space>' let g:ycm_confirm_extra_conf=0 let g:ycm_add_preview_to_completeopt=1 let g:ycm_show_diagnostics_ui=1 let g:ycm_global_ycm_extra_conf='~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py' let g:ycm_python_binary_path='/usr/bin/python3' let g:ycm_server_python_interpreter='/usr/bin/python3' let g:ycm_server_keep_logfiles=0 let g:ycm_server_log_level=3 ``` 3. 打开 Vim 并执行以下命令安装插件: ``` :PluginInstall ``` 4. 安装 YCM(YouCompleteMe)插件的依赖项: ```bash sudo apt install build-essential cmake python3-dev ``` 5. 在 `~/.vim/bundle/YouCompleteMe/` 目录下执行以下命令安装 YCM 插件: ```bash python3 install.py --clang-completer ``` 安装完成后,重新打开 Vim,在 C++ 文件中输入代码时,按下 `Ctrl + Space` 键即可触发自动补全代码功能。如果需要更改自动补全的选项,可以修改 `~/.vimrc` 文件中的配置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值