vim-plug 安装 YouCompleteMe 自动补全插件

vim-plug 是一个Vim插件管理器,利用异步并行可以快速地安装、更新和卸载插件。它的安装和配置都非常简单,而且在操作过程中会给出很多易读的反馈信息,是一个相当友好精

安装vim-plug

 下载 plug.vim 文件,根据操作系统不同,放置在以下autoload目录中:

Linux: ~/.vim/autoload

 Windows: ~\vimfiles\autoload\plug.vim

目录结构如下,之后安装的插件将存放在plugged目录中:

在用户目录下创建 ~/.vimrc 配置文件,在 .vimrc 配置文件中增加 ymc 插件,在 vim 中运行 :source ~/.vimrc 重新加载 .vimrc 配置文件,再输入 :PlugInstall 会自动下载插件,示例如下:

call plug#begin('~/.vim/plugged')
" Shorthand notation for plugin
Plug 'Valloric/YouCompleteMe'
call plug#end()

 安装YMC 依赖包

//先安装依赖包
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

报错以及解决

报错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

配置.vimrc

在 .vimrc 后面添加配置信息,示例如下

  nnoremap <leader>gl :YcmCompleter GoToDeclaration<CR>
  nnoremap <leader>gl :YcmCompleter GoToDefinition<CR>
  nnoremap <leader>gg :YcmCompleter GoToDefinitionElseDeclaration<CR>
  let g:ycm_collect_identifiers_from_tags_files = 1

  let g:ycm_global_ycm_extra_conf='~/.vim/.ycm_extra_conf.py'
  let g:ycm_confirm_extra_conf=0
  let g:ycm_error_symbol='✗'
  let g:ycm_warning_symbol='✹'
  set completeopt=menu,menuone
  let g:ycm_add_preview_to_completeopt = 0  " 关闭函数原型提示

  let g:ycm_show_diagnostics_ui = 1 " 关闭诊断信息
  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}'],
                          \}

 参考资料:(50条消息) Vim 插件YouCompleteMe(YCM)安装_Configure-Handle的博客-CSDN博客

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值