Macduan's Vim的vimrc与YouCompleteMe配置

作为一个C++爱好者同时也是vi爱好者,我个人觉得vi就是写代码工具不是IDE所以只用少数几个插件,其中YouCompleteMe是我极力推荐的插件,YouCompleteMe主要是自动补全,还有跳转,类型显示等功能,与之前的基于规则补全插件不同的是YouCompleteMe是基于语义,从安装过程就可以看出来。我用vundle安装和管理插件,YCM的官方安装文档也是推荐vundle,但是我配置好vimrc之后用执行vundle的PlugInstall时发现速度巨慢,其实这个过程本质就是在.vim/bundle中执行。

Vundle的安装与配置

详细请参考其github

YouCompleteMe安装配置

1
git clone https://github.com/Valloric/YouCompleteMe.git

之后可以在YouCompleteMe目录中执行

1
git submodule update --init --recursive

后面的安装步骤非常简单,

1
2
3
4
sudo apt-get install build-essential cmake #Install development tools and CMake
sudo apt-get install python-dev python3-dev #Make sure you have Python headers installed
cd ~/.vim/bundle/YouCompleteMe
./install.py --clang-completer #Compiling YCM with semantic support for C-family languages

如果很懒在确认安装build-essential,cmake和python-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
#!/bin/bash

log() {
echo "$(date +'%Y-%m-%dT%H:%M:%S%z') INFO $0 | $@"
}

err() {
echo "$(date +'%Y-%m-%dT%H:%M:%S%z') ERROR $0 | $@" >&2
}

execute() {
if (! $@); then
err "Failt to $@"
exit 1
fi
}

log "Start to install YouCompleteMe"

execute 'cd ~/.vim/bundle/'
execute 'rm -rf YouCompleteMe'
execute 'git clone https://github.com/Valloric/YouCompleteMe.git'
execute 'cd YouCompleteMe'
execute 'git submodule update --init --recursive'
execute './install.py --clang-completer'

log "Success to install YouCompleteMe"

exit 0

我的环境是ubuntu-64bit,详细参考官方文档
需要注意的是YCM会默认往上级目录查找直到HOME目录去查找一个.ycm_extra_conf.py的文件(可以自己配置),在.vimrc中我指定了使用官方默认的,配置如下,

1
let g:ycm_global_ycm_extra_conf='/home/macduan/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py'

在具体的项目中,在项目的根目录中放一个.ycm_extra_conf.py,并且加上头文件目录的配置,比如,

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
flags = [
'-Wall',
'-Wextra',
'-Werror',
'-Wno-long-long',
'-Wno-variadic-macros',
'-fexceptions',
'-DNDEBUG',

...

'-I',
'/home/macduan/work/opensource/brpc/src',
'-I',
'/home/macduan/work/opensource/brpc/src/brpc',
'-I',
'/home/macduan/work/opensource/brpc/src/bthread',
'-I',
'/home/macduan/work/opensource/brpc/src/butil',
'-I',
'/home/macduan/work/opensource/brpc/src/bvar',
'-I',
'/home/macduan/work/opensource/brpc/src/json2pb',
'-I',
'/home/macduan/work/opensource/brpc/src/mcpack2pb']

Macduan‘s 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Vundle
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
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 'tpope/vim-fugitive'

" Powerline
Plugin 'Lokaltog/vim-powerline'

" plugin from http://vim-scripts.org/vim/scripts.html
" Plugin 'L9'
" Git plugin not hosted on GitHub
Plugin 'git://git.wincent.com/command-t.git'

" Powerline
" Plugin 'powerline/powerline'
Plugin 'vim-airline/vim-airline'
Plugin 'vim-airline/vim-airline-themes'

Plugin 'scrooloose/nerdtree'

" color scheme
Plugin 'sickill/vim-monokai'
Plugin 'junegunn/seoul256.vim'
Plugin 'tomasr/molokai'

"Cpp hight
Plugin 'octol/vim-cpp-enhanced-highlight'

" Doxygen
Plugin 'vim-scripts/DoxygenToolkit.vim'

" Taglist
Plugin 'vim-scripts/taglist.vim'

" YouCompleteMe
Plugin 'Valloric/YouCompleteMe' "git clone it from github is rather quicker than this

" git repos on your local machine (i.e. when working on your own plugin)
" Plugin 'file:///home/gmarik/path/to/plugin'

" Install L9 and avoid a Naming conflict if you've already installed a
" different version somewhere else.
" Plugin 'ascenator/L9', {'name': 'newL9'}

" 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

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Basic Configuration
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
set history=500 " Sets how many lines of history VIM has to remember

syntax on " Enable filetype plugins

" Colorcolumn
set cc=100

hi Search guibg=peru guifg=wheat

" Set to auto read when a file is changed from the outside
set autoread

" With a map leader it's possible to do extra key combinations
" like <leader>w saves the current file
let mapleader = ","
let g:mapleader = ","

" Smart way to move between windows
map <C-j> <C-W>j
map <C-k> <C-W>k
map <C-h> <C-W>h
map <C-l> <C-W>l

" color scheme
syntax enable
" colorscheme monokai
" colo seoul256
" let g:seoul256_srgb = 1
colorscheme molokai
" let g:rehash256 = 1
let g:molokai_original = 1 " use original monokai

" these part refer http://harttle.land/2013/11/08/vim-config.html
set tabstop=4 " Number of spaces that a <Tab> in the file counts for.

set shiftwidth=4 " Number of spaces to use for each step of (auto)indent.

set expandtab " Use the appropriate number of spaces to insert a <Tab>.
" Spaces are used in indents with the '>' and '<' commands
" and when 'autoindent' is on. To insert a real tab when
" 'expandtab' is on, use CTRL-V <Tab>.

set softtabstop=4

set smarttab " When on, a <Tab> in front of a line inserts blanks
" according to 'shiftwidth'. 'tabstop' is used in other
" places. A <BS> will delete a 'shiftwidth' worth of space
" at the start of the line.

set showcmd " Show (partial) command in status line.

set number " Show line numbers.

set showmatch " When a bracket is inserted, briefly jump to the matching
" one. The jump is only done if the match can be seen on the
" screen. The time to show the match can be set with
" 'matchtime'.

set hlsearch " When there is a previous search pattern, highlight all
" its matches.

set incsearch " While typing a search command, show immediately where the
" so far typed pattern matches.

set ignorecase " Ignore case in search patterns.

set smartcase " Override the 'ignorecase' option if the search pattern
" contains upper case characters.

set backspace=2 " Influences the working of <BS>, <Del>, CTRL-W
" and CTRL-U in Insert mode. This is a list of items,
" separated by commas. Each item allows a way to backspace
" over something.

set autoindent " Copy indent from current line when starting a new line
" (typing <CR> in Insert mode or when using the "o" or "O"
" command).

set textwidth=100 " Maximum width of text that is being inserted. A longer
" line will be broken after white space to get this width.

set formatoptions=c,q,r,t " This is a sequence of letters which describes how
" automatic formatting is to be done.
"
" letter meaning when present in 'formatoptions'
" ------ ---------------------------------------
" c Auto-wrap comments using textwidth, inserting
" the current comment leader automatically.
" q Allow formatting of comments with "gq".
" r Automatically insert the current comment leader
" after hitting <Enter> in Insert mode.
" t Auto-wrap text using textwidth (does not apply
" to comments)

set ruler " Show the line and column number of the cursor position,
" separated by a comma.

set background=dark " When set to "dark", Vim will try to use colors that look
" good on a dark background. When set to "light", Vim will
" try to use colors that look good on a light background.
" Any other value is illegal.

set mouse=a " Enable the use of the mouse.

filetype plugin indent on
syntax on

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" vim-airline/vim-airline
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
" These lines setup the environment to show graphics and colors correctly.
set nocompatible
set t_Co=256
" Always show the status line
set laststatus=2
" Format the status line
" set statusline=%F%m%r%h\ %w\ \ CWD:\ %r%{getcwd()}%h\ \ \ Line:\ %l\ \ Column:\ %c
let g:airline#extensions#tabline#enabled = 1
" let g:airline#extensions#tabline#left_sep = ' '
" let g:airline#extensions#tabline#left_alt_sep = '>'

let g:cpp_class_scope_highlight = 1
let g:cpp_member_variable_highlight = 1
let g:cpp_class_decl_highlight = 1

" NERDTree
map <C-n> :NERDTreeToggle<CR>

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" doxygen
" Dox DoxAuthor DoxLic DoxUndoc
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
let g:DoxygenToolkit_briefTag_pre="@brief"
let g:DoxygenToolkit_paramTag_pre="@Param "
let g:DoxygenToolkit_returnTag="@Returns "
let g:DoxygenToolkit_authorName="duanmeng@outlook.com"
let g:DoxygenToolkit_licenseTag="@License I don't care"
let g:DoxygenToolkit_commentType = "C++"

let g:ycm_global_ycm_extra_conf='/home/macduan/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py'
let g:ycm_collect_identifiers_from_tag_files=1
let g:ycm_seed_identifiers_with_syntax=1
let g:ycm_confirm_extra_conf=0
let g:ycm_key_invoke_completion='<C-/>'
let g:ycm_goto_buffer_command='new-or-existing-tab' "open new tabe when jump to definition
let g:ycm_autoclose_preview_window_after_completion=1 " auto-close preview window after select a completion string
let g:ycm_autoclose_preview_window_after_insertion=1 " close preview window after leaving insert mode
nnoremap <leader>jd :YcmCompleter GoToDefinitionElseDeclaration<CR>
nnoremap <leader>jdf :YcmCompleter GoToDefinition<CR>
nnoremap <leader>jdc :YcmCompleter GoToDeclaration<CR>
nnoremap <leader>st :YcmCompleter GetType<CR>
nnoremap <F5> :YcmForceCompileAndDiagnostics<CR>