首頁 > 軟體

Ubuntu 16.04 Vim YouCompleteMe自動補全的安裝設定與使用

2020-06-16 17:21:04

Ubuntu 16.04 vim YouCompleteMe自動補全的安裝設定與使用

1 下載

git clone --recursive git://github.com/Valloric/YouCompleteMe

如果執行該命令沒報錯, 就ok了. 但是中途有可能會斷掉, 可以 cd 到 YouCompleteMe, 然後反復用如下命令再次進行下載:

git submodule update --init --recursive 

又一回我是反復執行了很多次這個命令才下載全了的.

2 安裝

cd ~/.vim/bundle/YouCompleteMe
./install.py --clang-completer  # 不需要sudo

成功的log如下:

-- [download 99% complete]
-- [download 100% complete]
Using libclang to provide semantic completion for C/C++/ObjC
Using external libclang: /tmp/ycm_build.CKY92s/clang+llvm-3.8.0-x86_64-linux-gnu-ubuntu-14.04/lib/libclang.so.3.8
-- Found PythonInterp: /usr/bin/python2.7 (found version "2.7.11") 
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE  
-- Configuring done
-- Generating done
-- Build files have been written to: /tmp/ycm_build.CKY92s
Scanning dependencies of target BoostParts
[  0%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/regex/src/regex_raw_buffer.cpp.o
[  0%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/regex/src/regex_debug.cpp.o
[  1%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/regex/src/w32_regex_traits.cpp.o
[  2%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/regex/src/wide_posix_api.cpp.o
...
[ 80%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/thread/src/pthread/once_atomic.cpp.o
[ 82%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/thread/src/pthread/thread.cpp.o
[ 83%] Linking CXX static library libBoostParts.a
[ 83%] Built target BoostParts
Scanning dependencies of target ycm_core
[ 83%] Building CXX object ycm/CMakeFiles/ycm_core.dir/PythonSupport.cpp.o
[ 84%] Building CXX object ycm/CMakeFiles/ycm_core.dir/Result.cpp.o
[ 85%] Building CXX object ycm/CMakeFiles/ycm_core.dir/Utils.cpp.o
[ 85%] Building CXX object ycm/CMakeFiles/ycm_core.dir/ClangCompleter/Documentation.cpp.o
[ 86%] Building CXX object ycm/CMakeFiles/ycm_core.dir/ClangCompleter/TranslationUnitStore.cpp.o
[ 88%] Building CXX object ycm/CMakeFiles/ycm_core.dir/ClangCompleter/ClangCompleter.cpp.o
[ 88%] Building CXX object ycm/CMakeFiles/ycm_core.dir/ClangCompleter/TranslationUnit.cpp.o
[ 88%] Building CXX object ycm/CMakeFiles/ycm_core.dir/ClangCompleter/ClangHelpers.cpp.o
[ 89%] Building CXX object ycm/CMakeFiles/ycm_core.dir/ClangCompleter/Range.cpp.o
[ 90%] Building CXX object ycm/CMakeFiles/ycm_core.dir/ClangCompleter/ClangUtils.cpp.o
[ 91%] Building CXX object ycm/CMakeFiles/ycm_core.dir/ClangCompleter/CompletionData.cpp.o
[ 91%] Building CXX object ycm/CMakeFiles/ycm_core.dir/ClangCompleter/CompilationDatabase.cpp.o
[ 92%] Building CXX object ycm/CMakeFiles/ycm_core.dir/CandidateRepository.cpp.o
[ 94%] Building CXX object ycm/CMakeFiles/ycm_core.dir/IdentifierDatabase.cpp.o
[ 94%] Building CXX object ycm/CMakeFiles/ycm_core.dir/LetterNode.cpp.o
[ 95%] Building CXX object ycm/CMakeFiles/ycm_core.dir/versioning.cpp.o
[ 95%] Building CXX object ycm/CMakeFiles/ycm_core.dir/Candidate.cpp.o
[ 96%] Building CXX object ycm/CMakeFiles/ycm_core.dir/IdentifierUtils.cpp.o
[ 97%] Building CXX object ycm/CMakeFiles/ycm_core.dir/ycm_core.cpp.o
[ 97%] Building CXX object ycm/CMakeFiles/ycm_core.dir/IdentifierCompleter.cpp.o
[ 98%] Building CXX object ycm/CMakeFiles/ycm_core.dir/LetterNodeListMap.cpp.o
[100%] Building CXX object ycm/CMakeFiles/ycm_core.dir/CustomAssert.cpp.o
[100%] Linking CXX shared library /home/wuyt/mutils/debian_lenny_cfg/vim_cfg/.vim/bundle/YouCompleteMe/third_party/ycmd/ycm_core.so
[100%] Built target ycm_core

3 設定

.vimrc中加入:

" YouCompleteMe
set runtimepath+=~/.vim/bundle/YouCompleteMe
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 = ['<c-n>', '<Down>']  " 對映按鍵, 沒有這個會攔截掉tab, 導致其他外掛的tab不能用.
let g:ycm_key_list_previous_completion = ['<c-p>', '<Up>']
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='~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py'
let g:ycm_show_diagnostics_ui = 0                           " 禁用語法檢查
inoremap <expr> <CR> pumvisible() ? "<C-y>" : "<CR>" |            " 回車即選中當前項
nnoremap <c-j> :YcmCompleter GoToDefinitionElseDeclaration<CR>|     " 跳轉到定義處
"let g:ycm_min_num_of_chars_for_completion=2                 " 從第2個鍵入字元就開始羅列匹配項

其中 ~/.vim/bundle/YouCompleteMe 為下載 YouCompleteMeu 的路徑.

4 使用

  • 預設當鍵入兩個字母之後, 則啟動補全. 可以通過該變數調整: g:ycm_min_num_of_chars_for_completion
  • 也可以使用原有的<c-x><c-o>來補全, ycm將其功能增強了.
  • 按照本文設定, 可用<c-j>跳轉到定義處.
  • 預設設定<TAB>會選擇補全內容, 本文的設定將其遮蔽了, 為了不會與snipmate等需要tab的衝突, 選擇補全改為了.

將Vim改造為強大的IDE—Vim整合Ctags/Taglist/Cscope/Winmanager/NERDTree/OmniCppComplete  http://www.linuxidc.com/Linux/2017-02/140282.htm

Vim入門基礎知識集錦  http://www.linuxidc.com/Linux/2017-02/140903.htm

Vim入門基礎教學 http://www.linuxidc.com/Linux/2017-02/140279.htm

把Vim打造成優秀的C++ IDE  http://www.linuxidc.com/Linux/2016-06/132262.htm

Ubuntu 14.04升級Vim7.4到8.0  http://www.linuxidc.com/Linux/2016-11/136816.htm

Vim安裝youcompleteme自動補全外掛  http://www.linuxidc.com/Linux/2016-11/137665.htm

Linux Vim編輯器使用簡單講解  http://www.linuxidc.com/Linux/2016-12/138930.htm

本文永久更新連結地址http://www.linuxidc.com/Linux/2017-02/141088.htm


IT145.com E-mail:sddin#qq.com