跳到主要內容

[Linux] Vundle 安裝 YouCompleteMe for Vim

YCM 的安裝看似很難,但其實很簡單。
這篇記錄一下最直接乾脆的安裝方式。
當然步驟還是有點多,但忍耐一下,裝一次就上手了。

簡單記錄步驟:
  1. 先確定有沒有 ~/.vim/bundle 這個資料夾,沒有的話就建立一個。
    mkdir -p ~/.vim/bundle
  2. git clone https://github.com/gmarik/vundle.git ~/.vim/bundle/vundle
  3. 在~/.vimrc 內設定套件安裝路徑,也可以依照預設的路徑設定:

    call vundle#begin()
    " alternatively, pass a path where Vundle should install plugins
    "call vundle#begin('~/some/path/here')
  4. 在 .vimrc 內加入 Plugin 'Valloric/YouCompleteMe'
  5. 存檔並執行 :source %,然後執行 :PluginInstall
  6. 如果之後想要移除套件,就在 .vimrc 內將套件名字註解,例如可以把 Plugin 'Valloric/YouCompleteMe' 這行註解掉,然後存檔,重新進入 vim 執行 :PluginClean 即可。

若出現以下錯誤訊息:

The ycmd server SHUT DOWN (restart with ':YcmRestartServer'). YCM ... YCM before using it . Follow the instruction in the documentation.

這個訊息不要理它。

下一步要做的,就是直接依照官方說明來安裝:
  1. cd ~/.vim/bundle/YouCompleteMe/
  2. 我是64位元電腦,在準備執行下一步 python3 ./install.py --all 之前,要先確保以下套件安裝完畢:
    mono-complete, xbuilder, gocode, python3-dev, vim-gocomplete, gccgo-go, npm, cargo
    反正就是看等一下執行python3 ./install.py -all 結果顯示缺什麼套件,就安裝那些套件,然後重跑一次python3 ./install.py --all
  3. 執行 python ./install.py --all,這樣可以替所有程式語言安裝YCM的功能,當然你可以選擇只要安裝特定語言的自動補全,我是選擇全部語言都支援。
  4. 大功告成。
對了,可以在 ~/.vimrc 內,加入以下幾行來關閉 YCM 的語法檢查,免得有時候 YCM 語法檢查會擾亂工作:

let g:ycm_show_diagnostics_ui=0 
let g:ycm_enable_diagnostic_signs=0
let g:ycm_enable_diagnostic_highlighting=0

[錯誤訊息排除]
目前我遇到的錯誤訊息有:
開啟 Vim 時,出現:

AttributeError: 'module' object has no attribute 'FlagsForFile'

解決方法參考:這篇文章

將以下程式碼輸入到 .ycm_extra_conf.py 的最尾端:

import ycm_core
flags = [
    '-Wall',  
    '-Wextra',  
    '-Werror', 
    '-Wno-long-long', 
    '-Wno-variadic-macros', 
    '-fexceptions',  
    '-ferror-limit=10000',  
    '-DNDEBUG', 
    '-std=c99',  
    '-xc',  
    '-isystem/usr/include/',  
    ]
SOURCE_EXTENSIONS = [
                         '.cpp',
                         '.cxx',
                         '.cc',
                         '.c',
                         ]

def FlagsForFile( filename, **kwargs ):  
    return {
        'flags': flags,  
        'do_cache': True  
    }


然後在 .vimrc 內加入:

 let g:ycm_global_ycm_extra_conf = "~/.vim/bundle/YouCompleteMe/.ycm_extra_conf.py"

然後再重新啟動 vim 一次就可以了。

參考資料1
參考資料2





留言

這個網誌中的熱門文章

[Linux] Elementary OS 字體調校

用 gesetting 取得 elementary os 的等寬字體(這也是終端機默認字體): gsettings get org.gnome.desktop.interface monospace-font-name 會顯示目前字型跟字體大小: Roboto Mono 10 設定字體大小: gsettings set org.gnome.desktop.interface monospace-font-name 'Roboto Mono 12' 可以微調 text-scaling-factor: gsettings set org.gnome.desktop.interface text-scaling-factor <value>

[心得] 復古、老派的程式設計之路

最近看到 John Carmack 2018 年的貼文中的幾段話: I’m not a Unix geek.  I get around ok, but I am most comfortable developing in Visual Studio on Windows.  I thought a week of full immersion work in the old school Unix style would be interesting, even if it meant working at a slower pace.  It was sort of an adventure in retro computing — this was fvwm and vi.  Not vim, actual BSD vi. In the end, I didn’t really explore the system all that much, with 95% of my time in just the basic vi / make / gdb operations.  I appreciated the good man pages, as I tried to do everything within the self contained system, without resorting to internet searches.  Seeing references to 30+ year old things like Tektronix terminals was amusing. In the spirit of my retro theme, I had printed out several of Yann LeCun’s old papers and was considering doing everything completely off line, as if I was actually in a mountain cabin somewhere, but I wound up watching a lot of the Stanford CS231N lectur...

[C++] 用 stringstream 分割同時包含 ASCII 和 Unicode 中文的字串

假設有一個檔案,內容包含中文,也包含 ASCII 字元(皆是可顯示字元)。 如果這個檔案格式為 UTF-8,則 ASCII 碼佔用 1 個 byte,中文字佔用 3 個 byte。且代表中文的這 3 個 byte,必定是負值。 (其他語言的文字,未必是用 3 byte 儲存,但繁體中文佔用了 3 byte,UTF-8 的特性就是 byte 數是變動的) 所以 stringstream 用 UTF-8 的 ASCII 的可顯示字元,來當作 delimiter分割字串,是很安全的,例如用 ASCII 的 "空白" 來將以下的檔案,每一行輸入都拆解成兩個獨立字串: apple 蘋果 橘子 哈哈 兔子 rabbit 可以這樣寫: ifstream inputFile(fileName.c_str());  string a, b, line; while(getline(inputFile, line)) {     stringstream tkn(line);     tkn >> a >> b;         cout << " [Debug] a == " << a << endl;     cout << " [Debug] b == " << b << endl; } inputFile.close(); 輸出會是: [Debug] a == apple [Debug] b == 蘋果 [Debug] a == 橘子 [Debug] b == 哈哈 [Debug] a == 兔子 [Debug] b == rabbit 一般我很少直接採用 Unicode 編碼來處理字串,較常採用 UTF-8 。因為 UTF-8 的前 128 個編碼,跟 ASCII 編碼是完全相同的,如此方便程式碼可以相容 ASCII 編碼,同時又可以處理寬字元。許多支持多國語言的程式,預設都用 UTF-8 就是這個緣故。 注意在 Window 系統記事本若將檔案存成 UT...