1.通过vundle安装并下载ycm源码
2.下载clang-llvm http://releases.llvm.org/ eg:clang+llvm-3.9.0-x86_64-opensuse13.2.tar
3.编译
mkdir ycm_build
cd ycm_build
#指定clang
cmake -G "Unix Makefiles" -DUSE_SYSTEM_BOOST=OFF DEXTERNAL_LIBCLANG_PATH=/export/software/clang+llvm-3.9.0-x86_64-opensuse13.2/lib/ . ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp
#指定llvm
cmake -G "Unix Makefiles" -DPATH_TO_LLVM_ROOT=/export/software/clang+llvm-3.9.0-x86_64-opensuse13.2/ . ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp
4.配置 .ycm_extra_conf.py
cp ~/.vim/bundle/YouCompleteMe/third_party/ycmd/examples/.ycm_extra_conf.py ~/.vim/
# flags 配置
flags = [
'-Wall',
'-Wextra',
'-Werror',
'-fexceptions',
'-DNDEBUG',
# THIS IS IMPORTANT! Without a "-std=<something>" flag, clang won't know which
# language to use when compiling headers. So it will guess. Badly. So C++
# headers will be compiled as C headers. You don't want that so ALWAYS specify
# a "-std=<something>".
# For a C project, you would set this to something like 'c99' instead of
# 'c++11'.
'-std=c++11',
# ...and the same thing goes for the magic -x option which specifies the
# language that the files to be compiled are written in. This is mostly
# relevant for c++ headers.
# For a C project, you would set this to 'c' instead of 'c++'.
'-x',
'c++',
'-isystem',
'/usr/include',
'-isystem',
'/usr/local/include',
'-isystem',
'/usr/include/c++/4.8.5',
]
or 根据工程项目需要自定义:
import os
import sys
import subprocess
def GetCompileFlags():
return [
'-x', 'c++',
# '-std=c++0x',
'-std=c++11',
'-Wall',
'-Wextra',
'-Wno-deprecated-register',
'-fno-omit-frame-pointer',
'-D_AD_SERVER_V2_',
'-D_FILE_OFFSET_BITS=64',
'-D_LARGE_FILE',
'-DARITHMETIC_RIGHT_SHIFT',
'-DBOOST_NO_CXX11_NULLPTR',
'-DBOOST_FILESYSTEM_DEPRECATED',
'-DCRC64_KEYID=1',
'-DCURL_STATICLIB',
'-DHAVE_CONFIG_H',
'-DHAVE_NETDB_H',
'-DHAVE_NETINET_IN_H',
'-DNDEBUG',
'-DSIGNED_RIGHT_SHIFT_IS'
]
def GetGccIncludes():
process = subprocess.Popen(['sh', '-c', '`gcc -print-prog-name=cc1plus` -v /dev/null'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
_, stderr = process.communicate()
process.poll()
lines = stderr.split('\n')
return [os.path.normpath(x.strip()) for x in lines if x.startswith(' ')]
def GetSystemIncludeFlags():
include_dirs = GetGccIncludes()
include_dir_flags = []
for x in include_dirs:
include_dir_flags.append('-isystem')
include_dir_flags.append(x)
return include_dir_flags
def GetProjectIncludeFlags():
project_dir = os.path.dirname(os.path.realpath(__file__))
#工程中需要inlclude的路径,例如blade中extra_incs部分
project_includes = [
'/thirdparty',
'/build64_debug',
]
return ['-I' + project_dir + x for x in project_includes]
def FlagsForFile(filename, **kwargs):
flags = GetCompileFlags() + GetSystemIncludeFlags() + GetProjectIncludeFlags()
return {
'flags': flags,
'do_cache': True
}
.vimrc配置:
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' "git in vim
Plugin 'kien/ctrlp.vim' "find file
Plugin 'dyng/ctrlsf.vim' "cpp hpp switch
Plugin 'bling/vim-airline'
Plugin 'vim-airline/vim-airline-themes' "airline themes
Plugin 'rking/ag.vim' "find content
Plugin 'majutsushi/tagbar' "tagbar
Plugin 'octol/vim-cpp-enhanced-highlight' "c++ syntax highlight
Plugin 'magic-dot-files/TagHighlight' "tagHightLight
Plugin 'nathanaelkane/vim-indent-guides' "indent
Plugin 'scrooloose/nerdtree' "tree window
Plugin 'jiangmiao/auto-pairs' "自动补全配对的(),{}等
Plugin 'tpope/vim-surround' "自动增加、替换配对符的插件
Plugin 'tpope/vim-commentary' "快速注释代码插件
Plugin 'godlygeek/tabular' "代码、注释、表格对齐 useage: Tab /=
"""""""以下和YCM二选一
"Plugin 'SirVer/ultisnips' "代码片段补全引擎
""Plugin 'honza/vim-snippets' "代码片段补全模板
Plugin 'ervandew/supertab' "supertab
Plugin 'Valloric/YouCompleteMe' "基于语义的代码补全
Plugin 'minrui-hust/color-cpp.vim' "增强ycm 对c++的语法高亮 vim > 8.0
"plugin on vim-scripts repos
" All of your Plugins must be added before the following line
call vundle#end() " required
filetype plugin indent on " required
" 通用设置
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
let mapleader = ";" " 定义<leader>键
filetype on " 设置开启文件类型侦测
filetype plugin on " 设置加载对应文件类型的插件
" set noeb " 关闭错误的提示
syntax enable " 开启语法高亮功能
syntax on " 自动语法高亮
set t_Co=256 " 开启256色支持
set cmdheight=1 " 设置命令行的高度
set showcmd " select模式下显示选中的行数
set ruler " 总是显示光标位置
set laststatus=2 " 总是显示状态栏
set number " 开启行号显示
set cursorline " 高亮显示当前行
set whichwrap+=<,>,h,l " 设置光标键跨行
set ttimeoutlen=0 " 设置<ESC>键响应时间
set virtualedit=block,onemore " 允许光标出现在最后一个字符的后面
autocmd BufWritePost $MYVIMRC source $MYVIMRC " 让配置变更立即生效
"colorscheme molokai "色彩主题
colorscheme neon "色彩主题
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 代码缩进和排版
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
set autoindent " 设置自动缩进
set cindent " 设置使用C/C++语言的自动缩进方式
set cinoptions=g0,:0,N-s,(0 " 设置C/C++语言的具体缩进方式
set smartindent " 智能的选择对其方式
filetype indent on " 自适应不同语言的智能缩进
set expandtab " 将制表符扩展为空格
set tabstop=4 " 设置编辑时制表符占用空格数
set shiftwidth=4 " 设置格式化时制表符占用空格数
set softtabstop=4 " 设置4个空格为制表符
set smarttab " 在行和段开始处使用制表符
"set nowrap " 禁止折行
set wrap " 折行
set backspace=2 " 使用回车键正常处理indent,eol,start等
set sidescroll=10 " 设置向右滚动字符数
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 搜索设置
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
set hlsearch " 高亮显示搜索结果
set incsearch " 开启实时搜索功能
set ignorecase " 搜索时大小写不敏感
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 缓存设置
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
set nobackup " 设置不备份
" set noswapfile " 禁止生成临时文件
set autoread " 文件在vim之外修改过,自动重新读入
set autowrite " 设置自动保存
set confirm " 在处理未保存或只读文件的时候,弹出确认
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 编码设置
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
set langmenu=zh_CN.UTF-8
set helplang=cn
set termencoding=utf-8
set encoding=utf8
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"plugin configue
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"airline
let g:airline#extensions#tabline#enabled = 1
"ctrlp
let g:ctrlp_map = '<Leader>p' "<Leader>p搜索当前目录下文件
let g:ctrlp_cmd = 'CtrlP'
nmap <Leader>f :CtrlPMRUFiles<CR> "<Leader>f搜索MRU文件
nmap <Leader>b :CtrlPBuffer<CR> "<Leader>b显示缓冲区文件,并可通过序号进行跳转
"设置搜索时忽略的文件
let g:ctrlp_custom_ignore = {
\ 'dir': '\v[\/]\.(git|hg|svn|rvm)$',
\ 'file': '\v\.(exe|so|dll|zip|tar|tar.gz|pyc)$',
\ }
let g:ctrlp_working_path_mode = 0
let g:ctrlp_match_window_bottom = 1
let g:ctrlp_max_height = 15 "修改QuickFix窗口显示的最大条目数
let g:ctrlp_match_window_reversed = 0
let g:ctrlp_mruf_max = 500 "设置MRU最大条目数为500
let g:ctrlp_follow_symlinks = 1
let g:ctrlp_by_filename = 1 "默认使用全路径搜索,置1后按文件名搜索,准确率会有所提高,可以用<C-d>进行切换
let g:ctrlp_regexp = 0 "默认不使用正则表达式,置1改为默认使用正则表达式,可以用<C-r>进行切换
let g:ctrlp_line_prefix = '♪ ' "默认不使用正则表达式,置1改为默认使用正则表达式,可以用<C-r>进行切换
"vim-indent-guides
let g:indent_guides_auto_colors = 0
autocmd VimEnter,Colorscheme * :hi IndentGuidesOdd guibg=red ctermbg=3
autocmd VimEnter,Colorscheme * :hi IndentGuidesEven guibg=green ctermbg=4
let g:indent_guides_enable_on_vim_startup=1 " 随 vim 自启动
let g:indent_guides_start_level=2 " 从第二层开始可视化显示缩进
let g:indent_guides_guide_size=1 " 色块宽度
nmap <silent> <Leader>i <Plug>IndentGuidesToggle " 快捷键 i 开/关缩进可视化
"set UltiSnips
"let g:UltiSnipsExpandTrigger="<leader><tab>"
"let g:UltiSnipsJumpForwardTrigger="<leader><tab>"
"let g:UltiSnipsJumpBackwardTrgger="<leader><tab>"
"let g:UltiSnipsListSnippets="<c-e>"
"tagbar
nmap <F9> :TagbarToggle<CR>
"ag
" The Silver Searcher
if executable('ag')
" Use ag over grep
set grepprg=ag\ --nogroup\ --nocolor
" Use ag in CtrlP for listing files. Lightning fast and respects .gitignore
let g:ctrlp_user_command = 'ag %s -l --nocolor -g ""'
" ag is fast enough that CtrlP doesn't need to cache
let g:ctrlp_use_caching = 0
endif
" bind K to grep word under cursor
nnoremap K :grep! "\b<C-R><C-W>\b"<CR>:cw<CR>
nnoremap \ :Ag<SPACE>
"ycm_extra_conf
let g:ycm_global_ycm_extra_conf='~/.vim/.ycm_extra_conf.py'
"let g:ycm_global_ycm_extra_conf='~/.vim/bundle/YouCompleteMe/cpp/ycm/.ycm_extra_conf.py'
"let g:ycm_collect_identifiers_from_tag_files = 0 "让YouCompleteMe同时利用原来的ctags"
" 允许 vim 加载 .ycm_extra_conf.py 文件,不再提示
"let g:ycm_confirm_extra_conf=0
" 补全内容不以分割子窗口形式出现,只显示补全列表
set completeopt-=preview
" 从第一个键入字符就开始罗列匹配项
let g:ycm_min_num_of_chars_for_completion=1
" 禁止缓存匹配项,每次都重新生成匹配项
let g:ycm_cache_omnifunc=0
" 语法关键字补全
let g:ycm_seed_identifiers_with_syntax=1
" YCM 补全菜单配色
" 菜单
highlight Pmenu ctermfg=2 ctermbg=3 guifg=#005f87 guibg=#EEE8D5
" 选中项
highlight PmenuSel ctermfg=2 ctermbg=3 guifg=#AFD700 guibg=#106900
" 补全功能在注释中同样有效
let g:ycm_complete_in_comments=1
nnoremap <leader>gl :YcmCompleter GoToDeclaration<CR>
nnoremap <leader>gf :YcmCompleter GoToDefinition<CR>
nnoremap <leader>gg :YcmCompleter GoToDefinitionElseDeclaration<CR>
augroup filetype
autocmd! BufRead,BufNewFile BUILD set filetype=blade
augroup end
可能需要预装的软件:
- ctag
ctag yum install ctags (redhat 系列
ctags -R --c++-kinds=+px --fields=+iaS --extra=+q
若要加入系统函数或全局变量的tag标签,则需执行:
ctags -I __THROW --file-scope=yes --langmap=c:+.h --languages=c,c++ --links=yes --c-kinds=+p --fields=+S -R -f ~/.vim/systags /usr/include /usr/local/include
~/.vim/systags /usr/include /usr/local/include
并且在~/.vimrc中添加(亦可用上面描述的手动加入的方式):
set tags+=~/.vim/systags
这样,便可以享受系统库函数名补全、原型预览等功能了。
2. vim8
参考:https://www.cnblogs.com/nidey/p/8657016.html
语法高亮:
1. cpp stl ,https://www.vim.org/scripts/script.php?script_id=2224,https://www.vim.org/scripts/script.php?script_id=1640
参考文章:https://valloric.github.io/YouCompleteMe/#freebsdopenbsd
https://www.cnblogs.com/zl-graduate/p/5777711.html