vim golang開發環境搭建
vim是非常強大的編輯器,最近在寫go的時候,想嘗試下在vim中進行開發,在網上找了不少教程之後,還是遇到了不少的問題,就來說一下我的安裝過程.
- 安裝vim包管理器
$ git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
- 安裝vim-go
$ git clone https://github.com/fatih/vim-go.git ~/.vim/bundle/vim-go
-
安裝外掛
將以下內容複製到 ~/.vimrc檔案中
set nocompatible " be iMproved, requiredfiletype off " required " set the runtime path to include Vundle and initializeset rtp+=~/.vim/bundle/Vundle.vimcall vundle#begin() " let Vundle manage Vundle, requiredPlugin 'gmarik/Vundle.vim'Plugin 'fatih/vim-go'Plugin 'Valloric/YouCompleteme' " All of your Plugins must be added before the following linecall vundle#end() " requiredfiletype plugin indent on " required
在vim命令模式下輸入PluginInstall
:PluginInstall
-
安裝vim-go下go的一些工具
在vim命令模式下輸入GoInstallBinaries安裝一些vim-go外掛所需要的一些工具,這些工具會被放到$GOBIN目錄下
:GoInstallBinaries
安裝好後,執行vim開啟vim,如果沒有任何提示錯誤的資訊,那麼恭喜,vim下go的開發環境就已經搭建好了.
可惜有時候並不是那麼一帆風順的,下面就講講我所遇到的問題及解決辦法
排錯
- YCM提示vim版本過低
#解除安裝老版本vim $ sudo apt remove vim-tiny vim-common vim-gui-common vim-nox #安裝新版本 $ git clone https://github.com/vim/vim.git $ cd vim $ ./configure --with-features=huge \ --enable-multibyte \ --enable-rubyinterp=yes \ --enable-pythoninterp=yes \ --with-python-config-dir=/usr/lib/python2.7/config \ --enable-python3interp=yes \ --with-python3-config-dir=/usr/lib/python3.5/config \ --enable-perlinterp=yes \ --enable-luainterp=yes \ --enable-gui=gtk2 \ --enable-cscope \ --prefix=/usr/local make VIMRUNTIMEDIR=/usr/local/share/vim/vim81
with-python-config-dir為/usr/lib/python*/下以config開頭的目錄,一般還有一些系統相關的資訊.Ubuntu下with-python-config-dir 或者with-python3-config-dir只需要配置一個就可以了,如果兩個都配置,可能會導致失敗的.
然後執行vim時,提示錯誤資訊為:
YouCompleteMe unavailable: requires Vim compiled with Python (2.7.1+ or 3.4+) support.
$ vim --version | grep python +comments+libcall+python/dyn+visualextra +conceal+linebreak+python3/dyn+viminfo $ python --version Python 2.7.12
明明現在已經有python的依賴的,而且版本也是對的,最終檢查了以後,發現這個vim是我從github上下載的原始碼壓縮包解壓出來的,然後用git拉取原始碼後重新編譯安裝了以後,這個問題就解決了.
YCM server shutdown
重新安裝了以後,執行vim提示的資訊為:
The ycmd server SHUT DOWN (restart with :YcmRestartServer)
然後按照提示重啟也沒什麼用,執行
:YcmToggleLogs ycmd_51731_stderr_8r5f09tg.log
發現並沒有任何的日誌資訊.然後確定了一下YCM也是最新的程式碼,最後通過網上查詢資料,在YCM的主目錄下執行
$ git submodule update --init --recursive
等待一些第三方的包下載完成後,需要重新安裝YCM,這個時候需要cmake,如果沒有需要先安裝,然後執行install.py 或者 install.sh指令碼完成安裝
#如果沒有cmake才需要執行下面第一個命令 $ sudo apt install cmake $ ./install.sh
等到安裝完成之後,重新開啟vim就沒有任何的錯誤資訊了,至此,vim下的go開發環境就已經搭建好了,好好享受吧~~.
其實遇到問題並不可怕,可怕的是沒有任何錯誤資訊,只要就錯誤資訊,我們通過檢視錯誤資訊就可以解決了.