Linux Vim YCM配置

由于之前不小心把vim的配置删了,致使之前的那篇文章无法访问,但为了回应有关网友的回复,特此重新编辑了vim的ycm配置。😃

YCM全称 YouCompleteMe,是一个vim插件,但是却是一个能够使代码自动补全的工具。虽然如此,但也还是无法与专用IDE相媲美,不过对于vim党来说却是一个十分有趣的插件。

安装前

要使VIM配置YCM,则必须确保Vim支持Python2和Python3,可以用 vim --version 查看是否支持。否则请自行从源代码安装vim。

我的系统是archlinux,vim版本为8.2

安装YCM(Vundle)

YCM github: https://github.com/ycm-core/YouCompleteMe

推荐通过 Vundle 安装 YCM,在此之前,需要安装 Vundle: https://github.com/VundleVim/Vundle.vim

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

之后可以编辑 ~/.vimrc 文件

1
2
3
4
5
6
7
8
9
10
set nocompatible
filetype off

set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
...
Plugin 'VundleVim/Vundle.vim'
...
call vundle#end()
filetype plugin indent on

一般来说,vundle常用用法如下

1
2
3
4
5
:PluginList       - 列举配置插件
:PluginInstall - 安装 ‘Plugin’ 之后的插件
:PluginUpdate - 更新插件
:PluginSearch foo - 寻找插件 foo并添加的本地配置
:PluginClean - 删除未被使用的插件

支持C/C++

要使YCM支持C语言的自动补全,还需要配置YCM,首先在 .vimrc 添加一行

1
2
3
4
5
call vundle#begin()
. . .
Plugin 'Valloric/YouCompleteMe’
. . .
call vundle#end()

进入vim后 :PluginInstall ,然后在进行编译YCM,当然还有 ./install.sh --all ,不过我就不怎么配置了。 注意还要安装 cmake

1
2
cd ~/.vim/bundle/YouCompleteMe
./install.sh --clang-completer

如果是Git YCM的,那么还需要在该目录下执行

1
2
git clone https://github.com/ycm-core/YouCompleteMe.git
git submodule update --init --recursive

下载安装libclang

由于我的系统是archlinux,所以可以直接

1
sudo pacman -S clang boost llvm-libs

其他linux系统比如Ubuntu,需要安装 llvm,clang,libclang,libboost。

编译构建 ycm_core 库

编译构建ycm_core库需要安装 cmake和python3-dev
创建一个用于存放编译过程的产生的文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# mkdir ~/ycm_build
# cd ~/ycm_build
# cmake -G "Unix Makefiles" -DUSE_SYSTEM_BOOST=ON -DUSE_SYSTEM_LIBCLANG=ON . ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp

输出:
-- The C compiler identification is GNU 9.1.0
-- The CXX compiler identification is GNU 9.1.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found PythonLibs: /usr/lib/libpython2.7.so (found suitable version "2.7.16", minimum required is "2.7")
-- Using libclang to provide semantic completion for C/C++/ObjC
-- Found Boost: /usr/include (found version "1.69.0") found components: filesystem regex
-- Using external libclang: /usr/lib/libclang.so.8
-- NOT using clang-tidy for static analysis.
-- Configuring done
-- Generating done
-- Build files have been written to: /home/joxrays/ycm_build

# cmake --build . --target ycm_core --config Release
--config Release 针对 Windows,对于Linux/Unix忽略
若无错误之后可看到 `Linking CXX shared library /home/joxrays/.vim/bundle/YouCompleteMe/third_party/ycmd/ycm_core.so`

至此YCM_CORE编译完成,接下来就可以

1
cp ~/.vim/bundle/YouCompleteMe/third_party/ycmd/examples/.ycm_extra_conf.py ~/.vim/

vim配置文件+插件

我先把我的vimrc配置文件贴出了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103

set nocp
set modelines=0
set backspace=2
syntax on
autocmd InsertLeave * se nocul
autocmd InsertEnter * se cul
set smartindent
set autoindent
set confirm
set tabstop=4
set softtabstop=4
set shiftwidth=4
set expandtab
set number
set history=50
set hlsearch
set incsearch
set gdefault
set encoding=utf-8
set fileencodings=utf-8,ucs-bom,shift-jis,gb18030,gbk,gb2312,cp936,utf-16,big5,euc-jp,latin1
set guifont=Menlo:h16:cANSI
set langmenu=zn_CN.UTF-8
set helplang=cn
set ruler
set laststatus=1
set showcmd
set scrolloff=3
set showmatch
set matchtime=5
set autowrite
set wildmenu
set linespace=2
set whichwrap=b,s,<,>,[,]
set foldenable
set cursorline
set magic
set ignorecase
set background=dark
set t_Co=256
set mouse=i
set backspace=indent,eol,start
set nocompatible

set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
Plugin 'VundleVim/Vundle.vim'
Plugin 'Valloric/YouCompleteMe'
Plugin 'altercation/vim-colors-solarized'
Plugin 'scrooloose/nerdtree'
Plugin 'scrooloose/nerdcommenter'
Plugin 'majutsushi/tagbar'
Plugin 'whatyouhide/vim-gotham'
Plugin 'vim-airline/vim-airline'
Plugin 'vim-airline/vim-airline-themes'

call vundle#end()

filetype plugin indent on

autocmd CursorMovedI,InsertLeave * if pumvisible() == 0|silent! pclose|endif
autocmd InsertLeave * if pumvisible() == 0|pclose|endif
set completeopt=longest,menu
highlight Pmenu guibg=darkgrey guifg=black
highlight PmenuSel guibg=lightgrey guifg=black
inoremap <expr> <Down> pumvisible() ? "\<C-n>" : "\<Down>"
inoremap <expr> <Up> pumvisible() ? "\<C-p>" : "\<Up>"
inoremap <expr> <PageDown> pumvisible() ? "\<PageDown>\<C-p>\<C-n>" : "\<PageDown>"
inoremap <expr> <PageUp> pumvisible() ? "\<PageUp>\<C-p>\<C-n>" : "\<PageUp>"
inoremap <expr> <CR> pumvisible() ? "\<C-y>" : "\<CR>"

let g:ycm_key_list_select_completion = ['<Down>']
let g:ycm_key_list_previous_completion = ['<Up>']

let g:ycm_goto_buffer_command = 'horizontal-split'
let g:ycm_server_python_interpreter='/usr/bin/python2'
let g:ycm_global_ycm_extra_conf = '~/.vim/.ycm_extra_conf.py'
let g:airline#extensions#tabline#enabled = 1
let g:ycm_seed_identifiers_with_syntax = 1
let g:ycm_complete_in_strings = 1
let g:ycm_collect_identifiers_from_tags_files = 1
let g:ycm_complete_in_comments = 1
let g:ycm_min_num_of_chars_for_completion = 2
let g:ycm_cache_omnifunc=0
let g:ycm_confirm_extra_conf=0


syntax enable
"colorscheme solarized
"colorscheme gotham
colorscheme jellybeans
let g:solarized_termcolors=256


let g:NERDTreeDirArrowExpandable = '▸'
let g:NERDTreeDirArrowCollapsible = '▾'
map <C-b> :NERDTreeToggle<CR>


map <C-m> :Tagbar<CR>
let g:tagbar_ctags_bin='ctags'
let g:tagbar_width=30

之后只需在 vim 中 :PluginInstall 安装插件,可能需要点时间…

taglist是一个用于显示定位程序中各种符号的插件。这里我使用的是tagbar,它是一个taglist的替代品,要是有tagbar还需要生成 tags 文件,生成方法可以参考 https://www.vim.org/scripts/script.php?script_id=2358

首先要从该网站下载 cpp_src.tar.bz2 ,解压后是一个 cpp_src目录,然后执行一下命令

1
2
ctags -R --c++-kinds=+p --fields=+iaS --extra=+q --language-force=C++ cpp_src
mv tags ~/.vim

而对于Vim颜色配置,比如本例中 jellybeans,则

1
2
3
4
mkdir -p ~/.vim/colors
cd ~/.vim/colors
curl -O https://raw.githubusercontent.com/nanotech/jellybeans.vim/master/colors/jellybeans.vim
然后在 .vimrc 中设置 colorscheme jellybeans

注意,如果 let g:ycm_global_ycm_extra_conf = '~/.vim/.ycm_extra_conf.py' 中 默认的 .ycm_extra_conf.py ,可能会出现一些莫名其妙的错误,那么可以下载该文件替换原有的 **.ycm_extra_conf.py **
https://raw.githubusercontent.com/theodelrieu/dotfiles/master/.ycm_extra_conf.py

1
2
cd ~/.vim/
curl -O https://raw.githubusercontent.com/theodelrieu/dotfiles/master/.ycm_extra_conf.py

结尾

基本上YCM配置到此结束了,也没有想象中的那么难啦~
现在vim支持Python和C/C++代码补全了
最后贴一张测试图:

YCM VIM

有啥问题可联系我哈~

bye


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!