安装环境:Ubuntu 20.04
其他环境参考官网:YouCompleteMe
YouCompleteMe插件需要通过插件管理器进行加载,这里使用vundle插件管理器
创建目录mkdir -p ~/.vim/bundle && cd ~/.vim/bundle
克隆vundle插件管理器项目git clone --recursive https://gitclone.com/github.com/VundleVim/Vundle.vim.git
vim加载vundle插件管理器注:确保递归克隆完成,这里使用了镜像网站加速github克隆项目,也可直接访问gitclone搜索项目
cd ~ && vi .vimrc
配置如下
"去掉vim的一致性
set nocompatible
"关闭文件类型
filetype off
"设置包括vundle和初始化相关的runtime path
set rtp+=~/.vim/bundle/Vundle.vim/
call vundle#begin()
“加载vundle自身
Plugin 'VundleVim/Vundle.vim'
"加载YouCompleteMe,使用绝对路径
Plugin 'file:///home/username/.vim/bundle/YouCompleteMe'
call vundle#end()
filetype plugin indent on
编译安装YouCompleteMe注:加载YouCompleteMe插件的路径要根据自己的用户名设置
.vimrc文件已经配置好了插件的加载设置,这里进行编译安装YouCompleteMe插件
克隆YouCompleteMe项目cd ~/.vim/bundle/ && git clone --recursive https://gitclone.com/github.com/ycm-core/YouCompleteMe.git
安装编译链工具和依赖库注:由于github访问速度较慢,YouCompleteMe包含的许多第三方的插件可能克隆不成功,确保完整克隆项目是后面编译成功的基础
sudo apt install build-essential cmake vim python3-dev libclang1-10
编译安装YouCompleteMe使用系统libclang库的方式
cd ~/.vim/bundle/YouCompleteMe && python3 install.py --clang-completer --system-libclang --build-dir ./build
或使用clangd的方式
cd ~/.vim/bundle/YouCompleteMe && python3 install.py --clangd-completer --build-dir ./build
注:使用clangd的方式需要先安装clangd
编译过程中没有报错即可安装完成
配置YouCompleteMe除了在安装vundle插件管理器的配置中加载YouCompleteMe插件外,还需要对YouCompleteMe进行其他设置
首先在.ycm_extra_conf.py文件中对代码提示进行设置
cd ~ && vi .ycm_extra_conf.py
配置如下
from distutils.sysconfig import get_python_inc
import platform
import os.path as p
import subprocess
import ycm_core
DIR_OF_THIS_SCRIPT = p.abspath( p.dirname( __file__ ) )
DIR_OF_THIRD_PARTY = p.join( DIR_OF_THIS_SCRIPT, 'third_party' )
SOURCE_EXTENSIONS = [ '.cpp', '.cxx', '.cc', '.c', '.m', '.mm' ]
# These are the compilation flags that will be used in case there's no
# compilation database set (by default, one is not set).
# CHANGE THIS LIST OF FLAGS. YES, THIS IS THE DROID YOU HAVE BEEN LOOKING FOR.
flags = [
'-Wall',
#'-Wextra',
'-Werror',
'-Wno-long-long',
'-Wno-variadic-macros',
'-fexceptions',
'-DYCM_EXPORT=',
# THIS IS IMPORTANT! Without the '-x' 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 the '-x' flag.
# For a C project, you would set this to 'c' instead of 'c++'.
'-x',
'c++',
# You can add the specified directory to the search path for
# system include files.
#'-isystem',
#'/usr/include/c++/5',
# You can add the specified directory to the search path for
# include files.
#'-I',
#'/usr/include/gmock',
'-isystem',
'/usr/include',
'-isystem',
'/usr/local/include',
'-isystem',
'/usr/include/c++/9',
]
# Clang automatically sets the '-std=' flag to 'c++14' for MSVC 2015 or later,
# which is required for compiling the standard library, and to 'c++11' for older
# versions.
if platform.system() != 'Windows':
flags.append( '-std=c++11' )
# Set this to the absolute path to the folder (NOT the file!) containing the
# compile_commands.json file to use that instead of 'flags'. See here for
# more details: http://clang.llvm.org/docs/JSONCompilationDatabase.html
#
# You can get CMake to generate this file for you by adding:
# set( CMAKE_EXPORT_COMPILE_COMMANDS 1 )
# to your CMakeLists.txt file.
#
# Most projects will NOT need to set this to anything; you can just change the
# 'flags' list of compilation flags. Notice that YCM itself uses that approach.
compilation_database_folder = ''
if p.exists( compilation_database_folder ):
database = ycm_core.CompilationDatabase( compilation_database_folder )
else:
database = None
def IsHeaderFile( filename ):
extension = p.splitext( filename )[ 1 ]
return extension in [ '.h', '.hxx', '.hpp', '.hh' ]
def FindCorrespondingSourceFile( filename ):
if IsHeaderFile( filename ):
basename = p.splitext( filename )[ 0 ]
for extension in SOURCE_EXTENSIONS:
replacement_file = basename + extension
if p.exists( replacement_file ):
return replacement_file
return filename
def PathToPythonUsedDuringBuild():
try:
filepath = p.join( DIR_OF_THIS_SCRIPT, 'PYTHON_USED_DURING_BUILDING' )
with open( filepath ) as f:
return f.read().strip()
# We need to check for IOError for Python 2 and OSError for Python 3.
except ( IOError, OSError ):
return None
def Settings( **kwargs ):
language = kwargs[ 'language' ]
if language == 'cfamily':
# If the file is a header, try to find the corresponding source file and
# retrieve its flags from the compilation database if using one. This is
# necessary since compilation databases don't have entries for header files.
# In addition, use this source file as the translation unit. This makes it
# possible to jump from a declaration in the header file to its definition
# in the corresponding source file.
filename = FindCorrespondingSourceFile( kwargs[ 'filename' ] )
if not database:
return {'flags': flags,
'include_paths_relative_to_dir': DIR_OF_THIS_SCRIPT,
'override_filename': filename
}
compilation_info = database.GetCompilationInfoForFile( filename )
if not compilation_info.compiler_flags_:
return {}
# Bear in mind that compilation_info.compiler_flags_ does NOT return a
# python list, but a "list-like" StringVec object.
final_flags = list( compilation_info.compiler_flags_ )
# NOTE: This is just for YouCompleteMe; it's highly likely that your project
# does NOT need to remove the stdlib flag. DO NOT USE THIS IN YOUR
# ycm_extra_conf IF YOU'RE NOT 100% SURE YOU NEED IT.
try:
final_flags.remove( '-stdlib=libc++' )
except ValueError:
pass
return { 'flags': final_flags,
'include_paths_relative_to_dir': compilation_info.compiler_working_dir_,
'override_filename': filename
}
if language == 'python':
return { 'interpreter_path': PathToPythonUsedDuringBuild()
}
return {}
def PythonSysPath( **kwargs ):
sys_path = kwargs[ 'sys_path' ]
sys_path.insert( 0, DIR_OF_THIS_SCRIPT);
return sys_path
注1:在
flags
数组中添加了系统头文件路径,以支持头文件、函数提示,需要根据使用环境修改添加其他头文件路径注2:由于使用的是C/C++语言,所以对.ycm_extra_conf.py文件进行了修改,针对所有语言的完整配置文件参考
~/.vim/bundle/YouCompleteMe/third_party/ycmd/.ycm_extra_conf.py
配置.vimrc
vi ~/.vimrc
在配置vundle的基础上,在.vimrcfiletype plugin indent on
配置后添加如下配置
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 = ['', ''] " 映射按键, 没有这个会拦截掉tab, 导致其他插件的tab不能用.
let g:ycm_key_list_previous_completion = ['', '']
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='~/.ycm_extra_conf.py' " 配置文件
let g:ycm_show_diagnostics_ui = 0 " 禁用语法检查
inoremappumvisible() ? "\" : "\" | " 回车即选中当前项
nnoremap:YcmCompleter GoToDefinitionElseDeclaration| " 跳转到定义处
let g:ycm_min_num_of_chars_for_completion=2 " 从第2个键入字符就开始罗列匹配项
let g:ycm_add_preview_to_completeopt = 0
let g:ycm_server_log_level = 'info'
let g:ycm_min_num_identifier_candidate_chars = 2
let g:ycm_key_invoke_completion = '' " 插入模式Ctrl+Z快捷键手动触发
noremaplet g:ycm_semantic_triggers = {
\'c, cpp':['re!\w{2}'], " c,cpp文件两个字符触发提示
\}
let g:ycm_filetype_whitelist = { " 配置白名单,只对c,cpp文件进行代码提示
\"c":1,
\"cpp":1,
\}
加载插件打开vim,命令模式输入PluginInstall回车
等待插件加载完成
代码提示测试enjoy! 附:完整.vimrc配置注:
Plugin
关键字旁边变为实心圆点即表示加载完成
"去掉vi的一致性"
set nocompatible
" 必须
filetype off
"显示行号"
set number
"" 隐藏滚动条"
set guioptions-=r
set guioptions-=L
set guioptions-=b
"隐藏顶部标签栏"
"set showtabline=0
""设置字体"
"set guifont=Monaco:h13
syntax on "开启语法高亮"
let g:solarized_termcolors=256 "solarized主题设置在终端下的设置"
set background=dark "设置背景色"
set nowrap "设置不折行"
set fileformat=unix "设置以unix的格式保存文件"
set cindent "设置C样式的缩进格式"
set tabstop=4 "设置table长度"
set shiftwidth=4 "同上"
set showmatch "显示匹配的括号"
set scrolloff=5 "距离顶部和底部5行"
set laststatus=2 "命令行为两行"
set fenc=utf-8 "文件编码"
set backspace=2
set mouse=a "启用鼠标"
set selection=exclusive
set selectmode=mouse,key
set matchtime=5
set ignorecase "忽略大小写"
set incsearch
set hlsearch "高亮搜索项"
set noexpandtab "不允许扩展table"
set whichwrap+=<,>,h,l
set autoread
"set cursorline "突出显示当前行"
"set cursorcolumn "突出显示当前列"
set completeopt=menu,menuone
"=====================加载vundle、YouCompleteMe========================
"去掉vim的一致性
set nocompatible
"关闭文件类型
filetype off
"设置包括vundle和初始化相关的runtime path
set rtp+=~/.vim/bundle/Vundle.vim/
call vundle#begin()
“加载vundle自身
Plugin 'VundleVim/Vundle.vim'
"加载YouCompleteMe,使用绝对路径
Plugin 'file:///home/username/.vim/bundle/YouCompleteMe'
call vundle#end()
filetype plugin indent on
"===================配置YouCompleteMe=================================
let g:ycm_collect_identifiers_from_tags_files = 1 " 开启 YCM 基于标签引擎
let g:ycm_collect_identifiers_from_comments_and_strings = 1 " 注释与字符串中的内容也用于补全
let g:ycm_seed_identifiers_with_syntax = 1 " 语法关键字补全
let g:ycm_complete_in_comments = 1 " 注释中也开启代码提示
let g:ycm_confirm_extra_conf = 0 " 关闭加载.ycm_extra_conf.py提示
let g:ycm_key_list_select_completion = ['', ''] " 映射按键, 没有这个会拦截掉tab, 导致其他插件的tab不能用.
let g:ycm_key_list_previous_completion = ['', '']
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='~/.ycm_extra_conf.py' " 配置文件
let g:ycm_show_diagnostics_ui = 0 " 禁用语法检查
inoremappumvisible() ? "\" : "\" | " 回车即选中当前项
nnoremap:YcmCompleter GoToDefinitionElseDeclaration| " Ctrl+j跳转到定义处
let g:ycm_min_num_of_chars_for_completion=2 " 从第2个键入字符就开始罗列匹配项
let g:ycm_add_preview_to_completeopt = 0
let g:ycm_server_log_level = 'info'
let g:ycm_min_num_identifier_candidate_chars = 2
let g:ycm_key_invoke_completion = '' " 插入模式Ctrl+Z快捷键手动触发
noremaplet g:ycm_semantic_triggers = {
\'c, cpp':['re!\w{2}'], " c,cpp文件两个字符触发提示
\}
let g:ycm_filetype_whitelist = { " 配置白名单,只对c,cpp文件进行代码提示
\"c":1,
\"cpp":1,
\}
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧