Vim 插件YouCompleteMe(YCM)安装

vim插件YouCompleteMe插件安装:
在ubuntu 18.04默认的vim版本是8.0,PluginInstall之后报不支持当前vim版本,所以在github上下载vim8.2的源码进行升级安装。
提示:一定要有耐心,此方法已经验证过几次,没有问题,大部分踩的坑都已经记录,good luck.
在这里插入图片描述

0x01:从github上下载或者clone vim源代码
a.网站下载zip

https://github.com/vim/vim
unzip vim-master
cd vim-master

b.linux 中直接clone代码

cd ~/Desktop
https://github.com/vim/vim.git
cd vim

0x02:Linux vim安装vundle:
1.从git上clone代码到本地 ~/.vim/bundle 文件夹下

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

curits@curits-virtual-machine:~/.vim/bundle$ pwd
/home/curits/.vim/bundle

2.只需要在 ~/.vimrc中添加"Plugin xxx",在vim 中输入:PluginInstall 就会从网络上下载插件并安装。

使用示例:

set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()

Plugin 'Valloric/YouCompleteMe "用以下载YCM文件

call vundle#end()

下载完成之后可以在以下文件夹看到下载好的插件:

curits@curits-virtual-machine:~/.vim/bundle$ pwd
/home/curits/.vim/bundle
curits@curits-virtual-machine:~/.vim/bundle$ ls
auto-pairs  gdbmgr  indentLine     nerdtree             syntastic  undotree     vim-airline-themes  vim-gutentags      vim-startify  YouCompleteMe
ctrlp.vim   grep    nerdcommenter  nerdtree-git-plugin  tagbar     vim-airline  vim-bufferline      vim-nerdtree-tabs  Vundle.vim

可以看到YCM源码已经下载好了,也可以选择从github上clone源代码,然后将文件拷贝到bundle文件夹下!!!!

0x03:编译VIM源码
基本步骤:

//先移除老版本的vim
sudo apt-get remove vim vim-runtime vim-tiny vim-common vim-scripts vim-doc
sudo ./configure --with-features=huge \
            --enable-multibyte \
            --enable-rubyinterp=yes \
            --enable-python3interp=yes \
            --with-python3-config-dir=$(python3-config --configdir) \
            --enable-perlinterp=yes \
            --enable-luainterp=yes \
            --enable-gui=gtk2 \
            --enable-cscope \
            --prefix=/usr/local
sudo make
sudo make install
//安装完一定记得重启以下终端!!!!!

检查vim基本属性是否正常:

curits@curits-virtual-machine:~/.vim/bundle$ vim --version
//vim 版本已经升级至8.2
VIM - Vi IMproved 8.2 (2019 Dec 12, compiled Jan  6 2021 14:35:42)
Included patches: 1-2301
Compiled by curits@curits-virtual-machine
Huge version without GUI.  Features included (+) or not (-):

curits@curits-virtual-machine:~$ vim --version | grep python
+comments          +libcall           -python            +visual
+conceal           +linebreak         +python3           +visualextra
Linking: gcc -L/usr/local/lib -Wl,--as-needed -o vim -lm -ltinfo -ldl -L/usr/lib/python3.6/config-3.6m-x86_64-linux-gnu -lpython3.6m -lpthread -ldl -lutil -lm

0x03:YCM安装

//先安装依赖包
sudo apt install build-essential cmake python3-dev
cd ~/.vim/bundle/YouCompleteMe
//下面这个命令更新子模块需要花很长时间耐心等待,一定要等clone完自己退出!!!
sudo git submodule update --init --recursive
//--clangd-completer 表示对C/C++的支持,如果要支持更多后跟参数 --all
sudo python3 install.py --clangd-completer
//配置文件拷贝
cp ~/.vim/bundle/YouCompleteMe/third_party/ycmd/examples/.ycm_extra_conf.py ~/.vim

0x04:报错以及解决
报错1:YouCompleteMe unavailable: requires Vim compiled with Python (3.6.0+) support.
报错原因:在编译Vim ./configure时没有加参数
解决办法:见前文Vim源码编译命令

报错2:YCM has dropped support for python2. You need to recompile it with python3 instead.
报错原因:当前系统默认的python版本为2.x,可以输入命令python检查当前默认内置python版本;
解决办法:设置本机Python的优先级

//此时,我们只需要执行下面两条命令,改一下python 的优先级即可~
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 100
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 150
//验证方法
curtis@curtis-virtual-machine:/mnt/hgfs/share/write_code/query_runqueues$ python
Python 3.6.9 (default, Oct  8 2020, 12:12:24) 
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

报错3:在启动vim时YCM服务器没有开起来

The ycmd server SHUT DOWN (restart with ':YcmRestartServer'). Unexpect...ype ':YcmToggleLogs ycmd_46753_stderr_v24jwju3.log' to check the logs.

//解决办法
//python3 install.py --clangd-completer

报错4:也就是少一些库,看报错具体信息sudo apt-get install xxx

checking --with-tlib argument... empty: automatic terminal library selection
checking for tgetent in -ltinfo... no
checking for tgetent in -lncurses... no
checking for tgetent in -ltermlib... no
checking for tgetent in -ltermcap... no
checking for tgetent in -lcurses... no
no terminal library found
checking for tgetent()... configure: error: NOT FOUND!
      You need to install a terminal library; for example ncurses.
      Or specify the name of the library with --with-tlib.

//以上报错解决办法
sudo apt-get install libncurses5-dev

-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Error at CMakeLists.txt:232 (message):
  Your C++ compiler does NOT fully support C++17.

-- Configuring incomplete, errors occurred!
See also "/tmp/ycm_build_vd6sm42s/CMakeFiles/CMakeOutput.log".
ERROR: the build failed.

//以上报错解决办法,这个可能会导致gcc -m32 xxx.c编译报错,install 完把优先级设回去就好
sudo apt-get install g++-8
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 700 --slave /usr/bin/g++ g++ /usr/bin/g++-7
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 800 --slave /usr/bin/g++ g++ /usr/bin/g++-8

Traceback (most recent call last):
  File "/home/curits/.vim/bundle/YouCompleteMe/third_party/ycmd/build.py", line 1138, in BuildWatchdogModule
    import setuptools # noqa
ModuleNotFoundError: No module named 'setuptools'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/curits/.vim/bundle/YouCompleteMe/third_party/ycmd/build.py", line 1195, in <module>
    Main()
  File "/home/curits/.vim/bundle/YouCompleteMe/third_party/ycmd/build.py", line 1177, in Main
    DoCmakeBuilds( args )
  File "/home/curits/.vim/bundle/YouCompleteMe/third_party/ycmd/build.py", line 1170, in DoCmakeBuilds
    BuildWatchdogModule( args )
  File "/home/curits/.vim/bundle/YouCompleteMe/third_party/ycmd/build.py", line 1155, in BuildWatchdogModule
    p.join( lib_dir, 'watchdog' ) )
  File "/usr/lib/python3.6/shutil.py", line 315, in copytree
    names = os.listdir(src)
FileNotFoundError: [Errno 2] No such file or directory: 'src/watchdog'
//以上报错解决办法
sudo git submodule update --init --recursive


Unable to start the ycmd server. Path in 'g:ycm_server_python_interpreter' option does not point to python3+;
//原因,可能没有通过sudo apt-get install python去安装Python,导致YCM无法使用python,你可以尝试输入python看是否可以正常启动python而非python3!
//解决办法
sudo apt-get install python
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 100
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 150

0x04:配置Vimrc

vim ~/.vimrc
//添加如下代码
 :YcmCompleter GoToDeclaration<CR>
  nnoremap <leader>gf :YcmCompleter GoToDefinition<CR>
  nnoremap <leader>gg :YcmCompleter GoToDefinitionElseDeclaration<CR>
  let g:ycm_collect_identifiers_from_tags_files = 1 
  
  set completeopt=menu,menuone   
  let g:ycm_add_preview_to_completeopt = 0  " 关闭函数原型提示
  
  let g:ycm_show_diagnostics_ui = 0 " 关闭诊断信息
  let g:ycm_server_log_level = 'info'
  let g:ycm_min_num_identifier_candidate_chars = 2  " 两个字符触发 补全
  let g:ycm_collect_identifiers_from_comments_and_strings = 1 " 收集
  let g:ycm_complete_in_strings=1
  
  noremap <c-z> <NOP>
  let g:ycm_key_invoke_completion = '<c-z>'   " YCM 里触发语义补全有一个快捷键
  let g:ycm_max_num_candidates = 15  " 候选数量
  
  let g:ycm_semantic_triggers =  {
                          \ 'c,cpp,python,java,go,erlang,perl': ['re!\w{2}'],
                          \ 'cs,lua,javascript': ['re!\w{2}'],
                          \ }

效果展示,代码可以自动补全:
在这里插入图片描述

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: CentOS 7 中的 Vim 插件 youcompleteme安装步骤如下: 1. 安装必要的依赖包: ``` sudo yum install cmake gcc-c++ python3-devel ``` 2. 安装 Vim 和 Vundle(一个 Vim 插件管理器): ``` sudo yum install vim git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim ``` 3. 在 Vim 配置文件中添加以下内容: ``` 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' " The following are examples of different formats supported. " Keep Plugin commands between vundle#begin/end. " plugin on GitHub repo 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 ``` 4. 在 Vim安装插件: 打开 Vim,输入`:PluginInstall`,回车即可。 5. 安装 YouCompleteMe: ``` cd ~/.vim/bundle/YouCompleteMe python3 install.py --all ``` 6. 重启 Vim,即可使用 YouCompleteMe 插件了。 注意:如果你需要 C++ 补全功能,还需要安装 Clang,详情请参考 YouCompleteMe 的官方文档。 ### 回答2: 在CentOS 7上安装vim插件YouCompleteMe,您可以按照以下步骤进行操作: 1. 首先,确保您的CentOS 7系统已经安装vim和cmake。如果没有安装,可以使用以下命令安装: ``` sudo yum install vim cmake ``` 2. 安装YouCompleteMe插件依赖的一些软件包。打开终端,运行以下命令: ``` sudo yum install python-devel python3-devel ``` 3. 下载YouCompleteMe插件的源代码。您可以在GitHub的YouCompleteMe项目页面上找到源代码的下载链接。使用git命令克隆源代码库到本地: ``` git clone https://github.com/ycm-core/YouCompleteMe.git ~/.vim/bundle/YouCompleteMe ``` 4. 切换到插件的目录,并构建插件。进入YouCompleteMe目录: ``` cd ~/.vim/bundle/YouCompleteMe ``` 5. 使用下面的命令来构建YouCompleteMe插件: ``` python3 install.py --clangd-completer ``` 6. 构建完成后,启动vim编辑器。在vim中,输入以下命令来安装插件的管理工具vim-plug(如果您已经安装了其他插件管理工具,您可以使用该工具): ``` curl -fLo ~/.vim/autoload/plug.vim --create-dirs \ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim ``` 7. 编辑您的vim配置文件,并按照以下示例添加YouCompleteMe插件的配置: ``` call plug#begin('~/.vim/plugged') Plug 'ycm-core/YouCompleteMe' call plug#end() ``` 8. 保存并退出vim配置文件后,重新打开vim并执行以下命令,安装插件: ``` :PlugInstall ``` 9. 插件安装完成后,您可以使用YouCompleteMe插件来提供自动补全和代码建议。 ### 回答3: 在CentOS 7上安装YouCompleteMe插件,需要进行以下步骤: 1. 首先,确保已经安装vim和python-devel: ``` sudo yum install vim python-devel ``` 2. 安装CMake(用于编译YouCompleteMe的依赖项): ``` sudo yum install cmake ``` 3. 使用git克隆YouCompleteMe仓库: ``` git clone https://github.com/ycm-core/YouCompleteMe.git ~/.vim/bundle/YouCompleteMe ``` 4. 进入YouCompleteMe目录,并编译插件: ``` cd ~/.vim/bundle/YouCompleteMe python3 install.py --clang-completer ``` 这里使用了`--clang-completer`选项来启用C/C++代码补全。如果需要其他语言的补全支持,可以查阅YouCompleteMe的文档并添加相应的选项。 5. 最后,编辑vim配置文件,添加YouCompleteMe插件的设置: ``` vi ~/.vimrc ``` 在文件中添加以下内容: ``` set nocompatible set rtp+=~/.vim/bundle/Vundle.vim call vundle#begin() Plugin 'VundleVim/Vundle.vim' Plugin 'Valloric/YouCompleteMe' call vundle#end() ``` 6. 保存并退出vim,然后重新打开vim。运行`:PluginInstall`命令,安装YouCompleteMe插件: ``` :PluginInstall ``` 这样,YouCompleteMe插件就成功安装到CentOS 7下的vim中了。您可以根据需要自定义配置,以适应您的开发环境。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值