-.YoucompleteMe還有很多強大的功能,有興趣可以繼續探索。我們需要以下幾步
- 先檢查一下自己的虛擬機中是否有安裝python,用vim試一下1 :echo has('python') 如果得到結果為1 就說明有(其實有沒有都無所謂,再執行一遍安裝命令絕對沒錯)
- 安裝vundle,vundle是一款vim插件管理工具,使用它安裝youcomplete很簡單
1 git clone https://github.com/gmarik/Vundle.vim.git~/.vim/bundle/Vundle.vim
在vimrc中添加這樣的配置語句
set nocompatible filetype off set rtp+=~/.vim/bundle/Vundle.vim call vundle#begin() Plugin 'gmarik/Vundle.vim' Plugin 'Valloric/YouCompleteMe' call vundle#end() filetype plugin indent on
去github上clone一下youcompleteme的代碼
git clone https://github.com/Valloric/YouCompleteMe.git ~/.vim/bundle/YouCompleteMe
然后在vim里面安裝一下,執行
看到有DONE!顯示就好了
這一步坑了我好久,網上好多抄襲的全是apt命令和dnf命令,不太適合我的centos7,用yum就好了!
yum install automake gcc gcc-c++ kernel-devel cmake yum install python-devel python3-devel
不管安沒安裝python在這兩句命令執行之后你都有了
接下來就是編譯的重頭戲
cd ~/.vim/bundle/YouCompleteMe
如果需要支持C類的補全,用下面的命令。
./install.py --clang-completer
如果需要支持golang的補全,用
./install.py --gocode-completer
其他:
- C# support: install Mono and add
--omnisharp-completer when calling ./install.py. - Go support: install Go and add
--gocode-completer when calling ./install.py. - TypeScript support: install Node.js and npm then install the TypeScript SDK with
npm install -g typescript. - JavaScript support: install Node.js and npm and add
--tern-completer when calling ./install.py. - Rust support: install Rust and add
--racer-completer when calling ./install.py.
修改vimrc文件
let g:ycm_global_ycm_extra_conf = '~/.vim/bundle/YouCompleteMe/.ycm_extra_conf.py let g:ycm_seed_identifiers_with_syntax=1 " 語法關鍵字補全 let g:ycm_confirm_extra_conf=0 " 打開vim時不再詢問是否加載ycm_extra_conf.py配置 inoremap <expr> <CR> pumvisible() ? "\<C-y>" : "\<CR>" "回車即選中當前項 set completeopt=longest,menu "讓Vim的補全菜單行為與一般IDE一致(參考VimTip1228)
有可能.ycm_extra_conf.py這個文件會自動就有,也許會沒有,find一下,沒找到的話就自己vim修改一下,以下是該文件內容,復制之前,友情提示,在vim輸入
進入復制模式,這樣子復制之后格式就不會亂了~
import os import ycm_core flags = [ '-Wall', '-Wextra', '-Wno-long-long', '-Wno-variadic-macros', '-fexceptions', '-stdlib=libc++', '-std=c++11', '-x', 'c++', '-I', '.', '-isystem', '/usr/include', '-isystem', '/usr/local/include', '-isystem', '/Library/Developer/CommandLineTools/usr/include', '-isystem', '/Library/Developer/CommandLineTools/usr/bin/../lib/c++/v1', ] compilation_database_folder = '' if os.path.exists( compilation_database_folder ): database = ycm_core.CompilationDatabase( compilation_database_folder ) else: database = None SOURCE_EXTENSIONS = [ '.cpp', '.cxx', '.cc', '.c', '.m', '.mm' ] def DirectoryOfThisScript(): return os.path.dirname( os.path.abspath( __file__ ) ) def MakeRelativePathsInFlagsAbsolute( flags, working_directory ): if not working_directory: return list( flags ) new_flags = [] make_next_absolute = False path_flags = [ '-isystem', '-I', '-iquote', '--sysroot=' ] for flag in flags: new_flag = flag if make_next_absolute: make_next_absolute = False if not flag.startswith( '/' ): new_flag = os.path.join( working_directory, flag ) for path_flag in path_flags: if flag == path_flag: make_next_absolute = True break if flag.startswith( path_flag ): path = flag[ len( path_flag ): ] new_flag = path_flag + os.path.join( working_directory, path ) break if new_flag: new_flags.append( new_flag ) return new_flags def IsHeaderFile( filename ): extension = os.path.splitext( filename )[ 1 ] return extension in [ '.h', '.hxx', '.hpp', '.hh' ] def GetCompilationInfoForFile( filename ): if IsHeaderFile( filename ): basename = os.path.splitext( filename )[ 0 ] for extension in SOURCE_EXTENSIONS: replacement_file = basename + extension if os.path.exists( replacement_file ): compilation_info = database.GetCompilationInfoForFile( replacement_file ) if compilation_info.compiler_flags_: return compilation_info return None return database.GetCompilationInfoForFile( filename ) def FlagsForFile( filename, **kwargs ): if database: compilation_info = GetCompilationInfoForFile( filename ) if not compilation_info: return None final_flags = MakeRelativePathsInFlagsAbsolute( compilation_info.compiler_flags_, compilation_info.compiler_working_dir_ ) else: relative_to = DirectoryOfThisScript() final_flags = MakeRelativePathsInFlagsAbsolute( flags, relative_to ) return { 'flags': final_flags, 'do_cache': True }
在你的~/.vimrc的結尾添加:
let mapleader=","
nnoremap <leader>gc :YcmCompleter GoToDeclaration<CR>
nnoremap <leader>gd :YcmCompleter GoToDefinition<CR>
nnoremap <leader>gg :YcmCompleter GoToDefinitionElseDeclaration<CR>
需要跳轉到函數定義的時候,光標定位到函數,進入普通模式(狂按esc后的模式),輸入,gd
其實直接輸入,gg就可以了。
如果不添加上面這4句話,每次跳轉需要 普通模式下輸入 :YcmCompleter GoToDeclaration才可以,太長了。
其他命令 參考:
https://github.com/Valloric/YouCompleteMe#goto-commands
另外,前跳后跳就是Ctrl+o Ctrl+i
至此,基本上夠用了。記錄下。
(2)找到配置文件 .ycm_extra_conf.py
網上大多說這個文件在YouCompleteMe/cpp/ycm下面,但是YouCompleteMe下面就沒有cpp文件夾,其實它是在third_party/ycmd/cpp/ycm目錄下。
$ cd ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/
ls -a 即可看到。
(3)自行在YoucompleteMe/中創建cpp/ycm目錄,將 .ycm_extra_conf.py拷貝進去
$ cd ~/.vim/bundle/YouCompleteMe
$ mkdir cpp
$ mkdir cpp/ycm
$ cp third_party/ycmd/cpp/ycm/.ycm_extra_conf.py cpp/ycm/
4.修改.vimrc配置文件
將下面的內容添加到.vimrc里面
" 尋找全局配置文件
let g:ycm_global_ycm_extra_conf = '~/.vim/bundle/YouCompleteMe/cpp/ycm/.ycm_extra_conf.py'
" 禁用syntastic來對python檢查
let g:syntastic_ignore_files=[".*\.py$"]
" 使用ctags生成的tags文件
let g:ycm_collect_identifiers_from_tag_files = 1
" 開啟語義補全
" 修改對C語言的補全快捷鍵,默認是CTRL+space,修改為ALT+;未測出效果
"let g:ycm_key_invoke_completion = '<M-;>'
" 設置轉到定義處的快捷鍵為ALT+G,未測出效果
"nmap <M-g> :YcmCompleter GoToDefinitionElseDeclaration <C-R>=expand("<cword>")<CR><CR>
"關鍵字補全
"let g:ycm_seed_identifiers_with_syntax = 1
" 在接受補全后不分裂出一個窗口顯示接受的項
set completeopt-=preview
" 讓補全行為與一般的IDE一致
set completeopt=longest,menu
" 不顯示開啟vim時檢查ycm_extra_conf文件的信息
let g:ycm_confirm_extra_conf=0
" 每次重新生成匹配項,禁止緩存匹配項
let g:ycm_cache_omnifunc=0
" 在注釋中也可以補全
let g:ycm_complete_in_comments=1
" 輸入第一個字符就開始補全
let g:ycm_min_num_of_chars_for_completion=1
" 錯誤標識符
let g:ycm_error_symbol='>>'
" 警告標識符
let g:ycm_warning_symbol='>*'
" 不查詢ultisnips提供的代碼模板補全,如果需要,設置成1即可
" let g:ycm_use_ultisnips_completer=0
"
- 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
- 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
上面的內容中,除了第一句尋找全局配置文件,其他的語句可以根據自己的需要更改、刪除或添加。
注:如果沒有在第(3)步中自己創建cpp/ycm目錄拷貝.ycm_extra_conf.py文件,則需要將第一句中的路徑改為全局配置文件所在的具體路徑,如下:
let g:ycm_global_ycm_extra_conf = '~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py'
5.保存退出.vimrc ,打開一個C/C++源程序,體驗其自動補全效果。
還可以對C++的STL庫智能補全:

6.
(1)配合上面安裝的syntastic還可以語法檢測
‘>>’指出有語法錯誤,但是檢測速度太慢,沒什么大用。
自我感覺這個語法自動檢測很煩,可以禁用它:
進入 /bundle/YouCompleteMe/plugin
修改youcompleteme.vim中的:

將如上圖中的第141行的參數改為0就可以了。
(2)YcmDiags插件可以顯示錯誤或警告信息,可以設置F9為打開窗口的快捷鍵,在.vimrc中添加語句:
顯示效果:

7.添加頭文件
目前在include中,無法補全stdio.h等頭文件,我們需要將/usr/include添加進去。路徑添加到 ~/.vim/bundle/YouCompleteMe/cpp/ycm/.ycm_extra_conf.py 文件中的flags 數組中,每增加一個路徑,前面要寫’-isystem’。

以后需要boost庫等其他的補全,也需要將相應的路徑添加進去。
-.YoucompleteMe還有很多強大的功能,有興趣可以繼續探索。