Golang開發環境搭建-Vim篇
雖說sublimetext3+gosublime+gocode是目前較為 流行的Golang開發環境組合,但作為一名VIMer,沒有一套得心應手的Vim for Golang dev心里總是過不去的。Golang雖然年輕,但即便是從Go 1版本發布(2012年3月28日)算起,掐指算來也有小三年了。全世界的開發者已經為Golang貢獻了較為成熟的Vim插件了。有了這些插件,搭建出 一套高效的Golang開發環境還是不難的,網上也有大量的資料可以參考,其中就有vim-go作者自己發表的一篇文章《Go development environment for Vim》。不過看別人 寫的與自己搭建體驗的還是有大不同的,於是想來想去還是把整個過程記錄下來。
一、一個干凈的環境
找個干凈的基礎環境,方便確認每個搭建步驟后的效果:
Ubuntu 14.04 x86_64
vim version 7.4.52
go version go1.4beta1 linux/amd64
再准備一個編輯Go源碼的測試源文件:
//hellogolang.go
package main
import "fmt"
func main() {
fmt.Println("Hello Golang!")
}
用於驗證每個搭建步驟后的變化。
二、嚴格按照vim-go的官方說明逐一搭建
Vim-go是當前使用最為廣泛的用於搭建Golang開發環境的vim插件,這里我同樣使用vim-go作為核心和基礎進行環境搭建的。vim-go利 用開源Vim插件管理器安裝,gmarik/Vundle.vim是目前被推薦次數更多的Vim插件管理器,超過了pathogen。這里我們 就用vundle來作為Vim的插件管理工具。
1、安裝Vundle.vim
Vundle.vim的安裝步驟如下:
mkdir ~/.vim/bundle
git clone https://github.com/gmarik/Vundle.vim.git ~/.vim/bundle/Vundle.vim
創建~/.vimrc文件(如果你沒有這個文件的話),在文件頂部添加有關Vundle.vim的配置:
set nocompatible " be iMproved, required
filetype off " required
" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" let Vundle manage Vundle, required
Plugin 'gmarik/Vundle.vim'
" All of your Plugins must be added before the following line
call vundle#end() " required
filetype plugin indent on " required
此時Vim僅安裝了Vundle.vim這一個插件。編輯hellogolang.go時與編輯普通文本文件無異,一切都還是Vim的默認屬性。
2、安裝Vim-go
編輯~/.vimrc,在vundle#begin和vundle#end間增加一行:
Plugin 'fatih/vim-go'
在Vim內執行 luginInstall,
Vundle.vim會在左側打開一個Vundle Installer Preview子窗口,窗口下方會提示:“Processing 'fatih/vim-go'”,待安裝完畢后,提示信息變 成“Done!”。
這時,我們可以看到.vim/bundle下多了一個vim-go文件夾:
$ ls .vim/bundle/
vim-go/ Vundle.vim/
此時,再次編輯hellogolang.go,語法高亮有了, 保存時自動format(利用$GOBIN/gofmt)也有了,但其他高級功能,比如自動import缺失的 package、自動補齊仍然沒有,我們還要繼續安裝一些東東。
3、安裝go.tools Binaries
vim-go安裝說明中提到所有必要的binary需要先安裝好,比如gocode、godef、goimports等。
通過:GoInstallBinaries,這些vim-go依賴的二進制工具將會自動被下載,並被安裝到$GOBIN下或$GOPATH/bin下。(這個工具需要依賴git或hg,需要提前安裝到你的OS中。)
:GoInstallBinaries的執行是交互式的,你需要回車確認:
vim-go: gocode not found. Installing github.com/nsf/gocode to folder /home/tonybai/go/bin
vim-go: goimports not found. Installing code.google.com/p/go.tools/cmd/goimports to folder /home/tonybai/go/bin/
vim-go: godef not found. Installing code.google.com/p/rog-go/exp/cmd/godef to folder /home/tonybai/go/bin/
vim-go: oracle not found. Installing code.google.com/p/go.tools/cmd/oracle to folder /home/tonybai/go/bin/
vim-go: gorename not found. Installing code.google.com/p/go.tools/cmd/gorename to folder /home/tonybai/go/bin/
vim-go: golint not found. Installing github.com/golang/lint/golint to folder /home/tonybai/go/bin/
vim-go: errcheck not found. Installing github.com/kisielk/errcheck to folder /home/tonybai/go/bin/
不過這些代碼多在code.google.com上托管,因此由於眾所周知的原因,vim-go的自動安裝很可能以失敗告終,這樣就需要你根據上 面日志中提到的各個工具的源碼地址逐一去下載並本地安裝。無法搭梯子的,可以通過http://gopm.io 下載相關包。
安裝后,$GOBIN下的新增Binaries如下:
-rwxr-xr-x 1 tonybai tonybai 5735552 11?? 7 11:03 errcheck*
-rwxr-xr-x 1 tonybai tonybai 9951008 11?? 7 10:33 gocode*
-rwxr-xr-x 1 tonybai tonybai 5742800 11?? 7 11:07 godef*
-rwxr-xr-x 1 tonybai tonybai 4994120 11?? 7 11:00 goimports*
-rwxr-xr-x 1 tonybai tonybai 5750152 11?? 7 11:03 golint*
-rwxr-xr-x 1 tonybai tonybai 6381832 11?? 7 11:01 gorename*
-rwxr-xr-x 1 tonybai tonybai 2954392 11?? 7 10:38 gotags*
-rwxr-xr-x 1 tonybai tonybai 9222856 11?? 7 11:01 oracle*
安裝好這些Binaries后,我們來看看哪些特性被支持了。
再次編輯hellogolang.go:
- 新起一行輸入fmt.,然后ctrl+x, ctrl+o,Vim 會彈出補齊提示下拉框,不過並非實時跟隨的那種補齊,這個補齊是由gocode提供的。
– 輸入一行代碼:time.Sleep(time.Second),執行:GoImports,Vim會自動導入time包。
– 將光標移到Sleep函數上,執行:GoDef或命令模式下敲入gd,Vim會打開$GOROOT/src/time/sleep.go中 的Sleep函數的定義。執行:b 1返回到hellogolang.go。
– 執行:GoLint,運行golint在當前Go源文件上。
– 執行:GoDoc,打開當前光標對應符號的Go文檔。
– 執行:GoVet,在當前目錄下運行go vet在當前Go源文件上。
– 執行:GoRun,編譯運行當前main package。
– 執行:GoBuild,編譯當前包,這取決於你的源文件,GoBuild不產生結果文件。
– 執行:GoInstall,安裝當前包。
– 執行:GoTest,測試你當前路徑下地_test.go文件。
– 執行:GoCoverage,創建一個測試覆蓋結果文件,並打開瀏覽器展示當前包的情況。
– 執行:GoErrCheck,檢查當前包種可能的未捕獲的errors。
– 執行:GoFiles,顯示當前包對應的源文件列表。
– 執行:GoDeps,顯示當前包的依賴包列表。
– 執行:GoImplements,顯示當前類型實現的interface列表。
– 執行:GoRename [to],將當前光標下的符號替換為[to]。
三、其他插件
到目前為止,我們還有若干特性沒能實現,重點是:
– 實時跟隨的代碼補齊
– Code Snippet support
1、安裝YCM(Your Complete Me)
在~/.vimrc中添加一行:
Plugin 'Valloric/YouCompleteMe'
保存退出后,再打開~/.vimrc並執行 luginInstall。
安裝完后,下面的提示欄提示:
ycm_client_support.[so|pyd|dll] and ycm_core.[so|pyd|dll] not detected; you need to compile YCM before using it. Read the docs!
似乎YCM是用了C++編寫的模塊對性能進行優化了,於是需要手工編譯YCM的support庫。步驟如下:
sudo apt-get install build-essential cmake python-dev
cd ~/.vim/bundle/YouCompleteMe
./install.sh
構建(編譯C++很慢,需要耐心的等一會)ok后,再打開hellogolang.go,逐字的實時補全功能就具備了!Cool!
2、安裝 UltiSnips
Vim-go默認是用ultisnips引擎插件,但這個插件需要單獨安裝。
同樣,我們利用vundle來安裝它,在~/.vimrc中添加一行:
Plugin 'SirVer/ultisnips'
snippet和snippet引擎是分開的。ultisnips是引擎,vim-go的go snippet定義在這里
https://github.com/fatih/vim-go/blob/master/gosnippets/snippets/go.snip
編輯hellogolang.go,按照go.snip中的說明,我們輸入func后敲擊tab鍵,我們發現期待的:
func name(params) type {
}
並沒有出現。反倒是YCM的下拉提示顯示在那里讓你選擇。似乎是ultisnips和YCM的鍵組合沖突了。ultisnips官方說明也的確如 此。ultisnips默認是用Tab展開snippet的,而YCM中的Tab用來選擇補齊項,我們可以通過設置來避免這些。
我們在.vimrc中添加如下setting:
" YCM settings
let g:ycm_key_list_select_completion = ['', '']
let g:ycm_key_list_previous_completion = ['']
let g:ycm_key_invoke_completion = '<C-Space>'
" UltiSnips setting
let g:UltiSnipsExpandTrigger="<tab>"
let g:UltiSnipsJumpForwardTrigger="<c-b>"
let g:UltiSnipsJumpBackwardTrigger="<c-z>"
這樣讓YCM通過回車和向下的箭頭來做list item正向選擇,通過向上箭頭做反向選擇。通過ctrl+space來原地觸發補齊提示。
而ultisnips則是用tab做snippet展開,ctrl+b正向切換占位符,ctrl+z反向切換占位符。
3、安裝molokai theme
Molokai theme是TextMate的theme的vim port,看着截圖挺不錯的,於是也安裝了一下。
mkdir ~/.vim/colors
下載或copy https://github.com /fatih/molokai/blob/master/colors/molokai.vim到~/.vim /colors目錄下
在.vimrc添加一行:colorscheme molokai
四、.vimrc
前面講到了vim-go有許多命令,在:xx模式下執行多顯不便,於是你可以定義一些Mappings,比如:
" set mapleader
let mapleader = ","
" vim-go custom mappings
au FileType go nmap <Leader>s <Plug>(go-implements)
au FileType go nmap <Leader>i <Plug>(go-info)
au FileType go nmap <Leader>gd <Plug>(go-doc)
au FileType go nmap <Leader>gv <Plug>(go-doc-vertical)
au FileType go nmap <leader>r <Plug>(go-run)
au FileType go nmap <leader>b <Plug>(go-build)
au FileType go nmap <leader>t <Plug>(go-test)
au FileType go nmap <leader>c <Plug>(go-coverage)
au FileType go nmap <Leader>ds <Plug>(go-def-split)
au FileType go nmap <Leader>dv <Plug>(go-def-vertical)
au FileType go nmap <Leader>dt <Plug>(go-def-tab)
au FileType go nmap <Leader>e <Plug>(go-rename)
這樣我們在命令模式下,輸入<,>+<r>就是運行 當前main包,以此類推。
另外下面這個配置使得我們在save file時既可以格式化代碼,又可以自動插入包導入語句(或刪除不用的包導入語句)。
" vim-go settings
let g:go_fmt_command = "goimports"
到這里,我們的Vim Golang開發環境就基本搭建好了。snippet+實時補齊讓你Coding如飛!
五、附錄:.vimrc文件
下面是截至目前為止全量.vimrc文件的內容:
set nocompatible " be iMproved, required
filetype off " required
colorscheme molokai
" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" let Vundle manage Vundle, required
Plugin 'gmarik/Vundle.vim'
Plugin 'fatih/vim-go'
Plugin 'Valloric/YouCompleteMe'
Plugin 'SirVer/ultisnips'
" All of your Plugins must be added before the following line
call vundle#end() " required
filetype plugin indent on " required
" set mapleader
let mapleader = ","
" vim-go custom mappings
au FileType go nmap <Leader>s <Plug>(go-implements)
au FileType go nmap <Leader>i <Plug>(go-info)
au FileType go nmap <Leader>gd <Plug>(go-doc)
au FileType go nmap <Leader>gv <Plug>(go-doc-vertical)
au FileType go nmap <leader>r <Plug>(go-run)
au FileType go nmap <leader>b <Plug>(go-build)
au FileType go nmap <leader>t <Plug>(go-test)
au FileType go nmap <leader>c <Plug>(go-coverage)
au FileType go nmap <Leader>ds <Plug>(go-def-split)
au FileType go nmap <Leader>dv <Plug>(go-def-vertical)
au FileType go nmap <Leader>dt <Plug>(go-def-tab)
au FileType go nmap <Leader>e <Plug>(go-rename)
" vim-go settings
let g:go_fmt_command = "goimports"
" YCM settings
let g:ycm_key_list_select_completion = ['', '']
let g:ycm_key_list_previous_completion = ['', '']
let g:ycm_key_invoke_completion = '<C-Space>'
" UltiSnips settings
let g:UltiSnipsExpandTrigger="<tab>"
let g:UltiSnipsJumpForwardTrigger="<c-b>"
let g:UltiSnipsJumpBackwardTrigger="<c-z>"
六、Mac OS X下Vim配置
1、MacVim替換
Mac OS X下的配置方法稍有不同,因為Mac下系統自帶的Vim是7.3版本,YCM要求Vim 7.3.584+版本,因此我們需要安裝MacVim以替代自帶的Vim,目前MacVim最新版本是version 7.4.258,完全滿足要求。在這里https://github.com/b4winckler/macvim/releases可以下載到最新的MacVim,下載后的MacVim可以通過如下步驟替換原Vim。
原Vim安裝到/usr/bin/vim下。
MacVim解壓后如下:
[tony@tonydeair ~/Downloads/MacVim-snapshot-73]$ls
MacVim.app/ README.txt mvim*
我們執行以下步驟即可完成vim替換工作:
sudo mv /usr/bin/vim /usr/bin/vim.bak //備份一下原vim
cp mvim /usr/local/bin/
sudo ln -s /usr/local/bin/mvim /usr/bin/vim
2、插件安裝和配置
按照上面Linux Vim的插件安裝步驟和配置方法我們來配置MacVim,配置后,我們發現除了molokai的colorscheme沒有生效外,其余插件工作均正常。而所有.go文件打開,均無molokai方案的顏色高亮,甚至連一般的顏色高亮都沒有了。經過不斷調試,發現了一個解決方法,在~/.vimrc中添加幾行代碼即可:
syntax on
au BufRead,BufNewFile *.go set filetype=go
colorscheme molokai
但這幾行配置代碼如果放在~/.vimrc的前面,則UltiSnips會無法工作,我將其移到~/.vimrc文件的末尾,這樣就不存在沖突了 (看來.vimrc的插件配置的先后順序會對插件功能的正常使用有影響)。漂亮的molokai colorscheme也會展現出來!
© 2014, bigwhite. 版權所有.