go-vim配置


一、環境准備:

  系統環境說明:

1 [root@docker golang]# cat /etc/redhat-release 
2 CentOS Linux release 7.2.1511 (Core) 
3 [root@docker golang]# uname -a
4 Linux docker 3.10.0-229.el7.x86_64 #1 SMP Fri Mar 6 11:36:42 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
5 [root@docker golang]# 

  准備一個go文件,用於觀察配置插件過程中的變化:

1 //hellogolang.go
2 package main
3 import "fmt"
4 func main() {
5         fmt.Println("Hello Golang!")
6 }

二、插件配置之路:

  1、Vundle.vim

1 #mkdir ~/.vim/bundle
2 #git clone https://github.com/gmarik/Vundle.vim.git ~/.vim/bundle/Vundle.vim  

    配置vimrc:創建~/.vimrc文件(如果你沒有這個文件的話),在文件頂部添加有關Vundle.vim的配置:

 1 set nocompatible              " be iMproved, required
 2 filetype off                  " required
 3 
 4 " set the runtime path to include Vundle and initialize
 5 set rtp+=~/.vim/bundle/Vundle.vim
 6 call vundle#begin()
 7 
 8 " let Vundle manage Vundle, required
 9 Plugin 'gmarik/Vundle.vim'
10 
11 " All of your Plugins must be added before the following line
12 call vundle#end()            " required
13 filetype plugin indent on    " required

  此時Vim僅安裝了Vundle.vim這一個插件。編輯hellogolang.go時與編輯普通文本文件無異,一切都還是Vim的默認屬性

  2、vim-go

    Vim-go是當前使用最為廣泛的用於搭建Golang開發環境的vim插件,這里我同樣使用vim-go作為核心和基礎進行環境搭建的。vim-go利 用開源Vim插件管理器安裝,gmarik/Vundle.vim是目前被推薦次數更多的Vim插件管理器,超過了pathogen。這里我們 就用vundle來作為

  Vim的插件管理工具。

     編輯~/.vimrc,在vundle#beginvundle#end間增加一行:

     Plugin 'fatih/vim-go'

     在Vim內執行:PluginInstall

 

       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並執行:PluginInstall

    安裝完后,下面的提示欄提示:

    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 yum 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'

 

  保存,退出vim

  打開vim,執行:PluginInstall

 

  編輯hellogolang.go,按照go.snip中的說明,我們輸入func后敲擊tab鍵,我們發現期待的:

 

  func name(params) type {
       
  }

 

  並沒有出現。反倒是YCM的下拉提示顯示在那里讓你選擇。似乎是ultisnips和YCM的鍵組合沖突了。ultisnips官方說明也的確如 此。ultisnips默認是用Tab展開snippet的,而YCM中的Tab用來選擇補齊項,我們可以通過設置來避免這些。

 

  我們在.vimrc中添加如下setting:

 

 1 " YCM settings
 2 let g:ycm_key_list_select_completion = ['', '']
 3 let g:ycm_key_list_previous_completion = ['']
 4 let g:ycm_key_invoke_completion = '<C-Space>'
 5 
 6 
 7 " UltiSnips setting
 8 let g:UltiSnipsExpandTrigger="<tab>"
 9 let g:UltiSnipsJumpForwardTrigger="<c-b>"
10 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文件

 1 下面是截至目前為止全量.vimrc文件的內容:
 2 
 3 set nocompatible              " be iMproved, required
 4 filetype off                  " required
 5 colorscheme molokai
 6 
 7 " set the runtime path to include Vundle and initialize
 8 set rtp+=~/.vim/bundle/Vundle.vim
 9 call vundle#begin()
10 
11 " let Vundle manage Vundle, required
12 Plugin 'gmarik/Vundle.vim'
13 Plugin 'fatih/vim-go'
14 Plugin 'Valloric/YouCompleteMe'
15 
16 Plugin 'SirVer/ultisnips'
17 
18 " All of your Plugins must be added before the following line
19 call vundle#end()            " required
20 filetype plugin indent on    " required
21 
22 " set mapleader
23 let mapleader = ","
24 
25 " vim-go custom mappings
26 au FileType go nmap <Leader>s <Plug>(go-implements)
27 au FileType go nmap <Leader>i <Plug>(go-info)
28 au FileType go nmap <Leader>gd <Plug>(go-doc)
29 au FileType go nmap <Leader>gv <Plug>(go-doc-vertical)
30 au FileType go nmap <leader>r <Plug>(go-run)
31 au FileType go nmap <leader>b <Plug>(go-build)
32 au FileType go nmap <leader>t <Plug>(go-test)
33 au FileType go nmap <leader>c <Plug>(go-coverage)
34 au FileType go nmap <Leader>ds <Plug>(go-def-split)
35 au FileType go nmap <Leader>dv <Plug>(go-def-vertical)
36 au FileType go nmap <Leader>dt <Plug>(go-def-tab)
37 au FileType go nmap <Leader>e <Plug>(go-rename)
38 
39 " vim-go settings
40 let g:go_fmt_command = "goimports"
41 
42 " YCM settings
43 let g:ycm_key_list_select_completion = ['', '']
44 let g:ycm_key_list_previous_completion = ['', '']
45 let g:ycm_key_invoke_completion = '<C-Space>'
46 
47 " UltiSnips settings
48 let g:UltiSnipsExpandTrigger="<tab>"
49 let g:UltiSnipsJumpForwardTrigger="<c-b>"
50 let g:UltiSnipsJumpBackwardTrigger="<c-z>"

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM