【Linux基础编程】Tools——YouCompleteMe

01、文章目录

02、YouCompleteMe介绍

YouCompleteMe是一款非常强大的vim代码自动补全插件,使用简单方便,可以通过YouCompleteMe项目主页的这幅动图感受下它的强大。

在这里插入图片描述
都知道Linux系统写程序一般都用的vim,但是vim是没有这么强大的功能的,所以,这个时候我们就要用一些工具来完善这个编译器。

03、YouCompleteMe下载方式

在gitHub上面下载对应的插件

deng@itcast:~$ git clone --recursive git://github.com/Valloric/YouCompleteMe

如果执行该命令没报错, 就ok了. 但是中途有可能会断掉, 可以 cd 到 YouCompleteMe, 然后反复用如下命令再次进行下载:

deng@itcast:~$ git submodule update --init --recursive

这里我介绍一个网址:https://www.jianshu.com/p/75cde8a80fd7,这里面包括了Mac OS X、Ubuntu、Centos、ArchLinux等不同的Linux版本对应的下载方法,仅供参考!

04、YouCompleteMe安装方法

deng@itcast:~/.vim/bundle/YouCompleteMe$ pwd
/home/deng/.vim/bundle/YouCompleteMe
deng@itcast:~/.vim/bundle/YouCompleteMe$ ./install.py --clang-completer 

编译结果如下:

-- [download 99% complete]
-- [download 100% complete]
Using libclang to provide semantic completion for C/C++/ObjC
Using external libclang: /tmp/ycm_build.CKY92s/clang+llvm-3.8.0-x86_64-linux-gnu-ubuntu-14.04/lib/libclang.so.3.8
-- Found PythonInterp: /usr/bin/python2.7 (found version "2.7.11") 
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE  
-- Configuring done
-- Generating done
-- Build files have been written to: /tmp/ycm_build.CKY92s
Scanning dependencies of target BoostParts
[  0%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/regex/src/regex_raw_buffer.cpp.o
[  0%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/regex/src/regex_debug.cpp.o
[  1%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/regex/src/w32_regex_traits.cpp.o
[  2%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/regex/src/wide_posix_api.cpp.o
...
[ 80%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/thread/src/pthread/once_atomic.cpp.o
[ 82%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/thread/src/pthread/thread.cpp.o
[ 83%] Linking CXX static library libBoostParts.a
[ 83%] Built target BoostParts
Scanning dependencies of target ycm_core
[ 83%] Building CXX object ycm/CMakeFiles/ycm_core.dir/PythonSupport.cpp.o
[ 84%] Building CXX object ycm/CMakeFiles/ycm_core.dir/Result.cpp.o
[ 85%] Building CXX object ycm/CMakeFiles/ycm_core.dir/Utils.cpp.o
[ 85%] Building CXX object ycm/CMakeFiles/ycm_core.dir/ClangCompleter/Documentation.cpp.o
[ 86%] Building CXX object ycm/CMakeFiles/ycm_core.dir/ClangCompleter/TranslationUnitStore.cpp.o
[ 88%] Building CXX object ycm/CMakeFiles/ycm_core.dir/ClangCompleter/ClangCompleter.cpp.o
[ 88%] Building CXX object ycm/CMakeFiles/ycm_core.dir/ClangCompleter/TranslationUnit.cpp.o
[ 88%] Building CXX object ycm/CMakeFiles/ycm_core.dir/ClangCompleter/ClangHelpers.cpp.o
[ 89%] Building CXX object ycm/CMakeFiles/ycm_core.dir/ClangCompleter/Range.cpp.o
[ 90%] Building CXX object ycm/CMakeFiles/ycm_core.dir/ClangCompleter/ClangUtils.cpp.o
[ 91%] Building CXX object ycm/CMakeFiles/ycm_core.dir/ClangCompleter/CompletionData.cpp.o
[ 91%] Building CXX object ycm/CMakeFiles/ycm_core.dir/ClangCompleter/CompilationDatabase.cpp.o
[ 92%] Building CXX object ycm/CMakeFiles/ycm_core.dir/CandidateRepository.cpp.o
[ 94%] Building CXX object ycm/CMakeFiles/ycm_core.dir/IdentifierDatabase.cpp.o
[ 94%] Building CXX object ycm/CMakeFiles/ycm_core.dir/LetterNode.cpp.o
[ 95%] Building CXX object ycm/CMakeFiles/ycm_core.dir/versioning.cpp.o
[ 95%] Building CXX object ycm/CMakeFiles/ycm_core.dir/Candidate.cpp.o
[ 96%] Building CXX object ycm/CMakeFiles/ycm_core.dir/IdentifierUtils.cpp.o
[ 97%] Building CXX object ycm/CMakeFiles/ycm_core.dir/ycm_core.cpp.o
[ 97%] Building CXX object ycm/CMakeFiles/ycm_core.dir/IdentifierCompleter.cpp.o
[ 98%] Building CXX object ycm/CMakeFiles/ycm_core.dir/LetterNodeListMap.cpp.o
[100%] Building CXX object ycm/CMakeFiles/ycm_core.dir/CustomAssert.cpp.o
[100%] Linking CXX shared library /home/wuyt/mutils/debian_lenny_cfg/vim_cfg/.vim/bundle/YouCompleteMe/third_party/ycmd/ycm_core.so
[100%] Built target ycm_core

如果报Python相关的错误就执行如下命令

deng@itcast:~$ sudo apt install build-essential cmake python3-dev

05、YouCompleteMe配置方法

在~/.vimrc文件中添加如下内容

" YouCompleteMe
set runtimepath+=~/.vim/bundle/YouCompleteMe
let g:ycm_collect_identifiers_from_tags_files = 1           " 开启 YCM 基于标签引擎
let g:ycm_collect_identifiers_from_comments_and_strings = 1 " 注释与字符串中的内容也用于补全
let g:syntastic_ignore_files=[".*\.py$"]
let g:ycm_seed_identifiers_with_syntax = 1                  " 语法关键字补全
let g:ycm_complete_in_comments = 1
let g:ycm_confirm_extra_conf = 0
let g:ycm_key_list_select_completion = ['<c-n>', '<Down>']  " 映射按键, 没有这个会拦截掉tab, 导致其他插件的tab不能用.
let g:ycm_key_list_previous_completion = ['<c-p>', '<Up>']
let g:ycm_complete_in_comments = 1                          " 在注释输入中也能补全
let g:ycm_complete_in_strings = 1                           " 在字符串输入中也能补全
let g:ycm_collect_identifiers_from_comments_and_strings = 1 " 注释和字符串中的文字也会被收入补全
let g:ycm_global_ycm_extra_conf='~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py'
let g:ycm_show_diagnostics_ui = 0                           " 禁用语法检查
inoremap <expr> <CR> pumvisible() ? "\<C-y>" : "\<CR>" |            " 回车即选中当前项
nnoremap <c-j> :YcmCompleter GoToDefinitionElseDeclaration<CR>|     " 跳转到定义处
"let g:ycm_min_num_of_chars_for_completion=2                 " 从第2个键入字符就开始罗列匹配项

其中 ~/.vim/bundle/YouCompleteMe 为下载 YouCompleteMeu 的路径.

06、YouCompleteMe的使用

  • 默认当键入两个字母之后, 则启动补全. 可以通过该变量调整: g:ycm_min_num_of_chars_for_completion
  • 也可以使用原有的``来补全, ycm将其功能增强了.
  • 按照本文配置, 可用``跳转到定义处.
  • 默认配置``会选择补全内容, 本文的配置将其屏蔽了, 为了不会与snipmate等需要tab的冲突, 选择补全改为了.

07、总结

这里引用了简书的一篇方法:https://www.jianshu.com/p/75cde8a80fd7,侵权即删!

YouCompleteMe工具的介绍到这里就先告一段落了。这里个人建议用简书那篇文章的方法,比较多,都适配过,但是可能也需要多次重复下载,一次下载成功,我还没下载成功过。方法都在上面了。

版权声明:转载请注明出处,谢谢!

### 回答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
发出的红包

打赏作者

Cain Xcy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值